1
0
mirror of https://github.com/Radarr/Radarr.git synced 2026-04-18 21:35:51 -04:00

New: Use native dotnet host and DryIoc

(cherry picked from commit d6170dbfedf27a6218afe242a0fae2eb8b368aec)
(cherry picked from commit 7fe36a7e9222e830f4920e09a85115df0bdbf190)
This commit is contained in:
ta264
2021-10-21 21:04:26 +01:00
committed by Qstick
parent 2d53ec24f8
commit 06e2d5b3c3
77 changed files with 940 additions and 5133 deletions
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NzbDrone.Core.Configuration;
namespace Radarr.Http.Authentication
{
@@ -17,17 +18,20 @@ namespace Radarr.Http.Authentication
public string HeaderName { get; set; }
public string QueryName { get; set; }
public string ApiKey { get; set; }
}
public class ApiKeyAuthenticationHandler : AuthenticationHandler<ApiKeyAuthenticationOptions>
{
private readonly string _apiKey;
public ApiKeyAuthenticationHandler(IOptionsMonitor<ApiKeyAuthenticationOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock)
ISystemClock clock,
IConfigFileProvider config)
: base(options, logger, encoder, clock)
{
_apiKey = config.ApiKey;
}
private string ParseApiKey()
@@ -56,7 +60,7 @@ namespace Radarr.Http.Authentication
return Task.FromResult(AuthenticateResult.NoResult());
}
if (Options.ApiKey == providedApiKey)
if (_apiKey == providedApiKey)
{
var claims = new List<Claim>
{
@@ -2,7 +2,6 @@ using System;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using NzbDrone.Core.Authentication;
using NzbDrone.Core.Configuration;
namespace Radarr.Http.Authentication
{
@@ -13,53 +12,37 @@ namespace Radarr.Http.Authentication
return authenticationBuilder.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(name, options);
}
public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder authenticationBuilder)
public static AuthenticationBuilder AddBasic(this AuthenticationBuilder authenticationBuilder, string name)
{
return authenticationBuilder.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>(AuthenticationType.Basic.ToString(), options => { });
return authenticationBuilder.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>(name, options => { });
}
public static AuthenticationBuilder AddNoAuthentication(this AuthenticationBuilder authenticationBuilder)
public static AuthenticationBuilder AddNone(this AuthenticationBuilder authenticationBuilder, string name)
{
return authenticationBuilder.AddScheme<AuthenticationSchemeOptions, NoAuthenticationHandler>(AuthenticationType.None.ToString(), options => { });
return authenticationBuilder.AddScheme<AuthenticationSchemeOptions, NoAuthenticationHandler>(name, options => { });
}
public static AuthenticationBuilder AddAppAuthentication(this IServiceCollection services, IConfigFileProvider config)
public static AuthenticationBuilder AddAppAuthentication(this IServiceCollection services)
{
var authBuilder = services.AddAuthentication(config.AuthenticationMethod.ToString());
if (config.AuthenticationMethod == AuthenticationType.Basic)
{
authBuilder.AddBasicAuthentication();
}
else if (config.AuthenticationMethod == AuthenticationType.Forms)
{
authBuilder.AddCookie(AuthenticationType.Forms.ToString(), options =>
return services.AddAuthentication()
.AddNone(AuthenticationType.None.ToString())
.AddBasic(AuthenticationType.Basic.ToString())
.AddCookie(AuthenticationType.Forms.ToString(), options =>
{
options.AccessDeniedPath = "/login?loginFailed=true";
options.LoginPath = "/login";
options.ExpireTimeSpan = TimeSpan.FromDays(7);
})
.AddApiKey("API", options =>
{
options.HeaderName = "X-Api-Key";
options.QueryName = "apikey";
})
.AddApiKey("SignalR", options =>
{
options.HeaderName = "X-Api-Key";
options.QueryName = "access_token";
});
}
else
{
authBuilder.AddNoAuthentication();
}
authBuilder.AddApiKey("API", options =>
{
options.HeaderName = "X-Api-Key";
options.QueryName = "apikey";
options.ApiKey = config.ApiKey;
});
authBuilder.AddApiKey("SignalR", options =>
{
options.HeaderName = "X-Api-Key";
options.QueryName = "access_token";
options.ApiKey = config.ApiKey;
});
return authBuilder;
}
}
}
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Authentication;
using NzbDrone.Core.Configuration;
namespace Radarr.Http.Authentication
@@ -35,14 +36,14 @@ namespace Radarr.Http.Authentication
{
new Claim("user", user.Username),
new Claim("identifier", user.Identifier.ToString()),
new Claim("UiAuth", "true")
new Claim("AuthType", AuthenticationType.Forms.ToString())
};
var authProperties = new AuthenticationProperties
{
IsPersistent = resource.RememberMe == "on"
};
await HttpContext.SignInAsync(new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies", "user", "identifier")), authProperties);
await HttpContext.SignInAsync(AuthenticationType.Forms.ToString(), new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies", "user", "identifier")), authProperties);
return Redirect("/");
}
@@ -51,7 +52,7 @@ namespace Radarr.Http.Authentication
public async Task<IActionResult> Logout()
{
_authService.Logout(HttpContext);
await HttpContext.SignOutAsync();
await HttpContext.SignOutAsync(AuthenticationType.Forms.ToString());
return Redirect("/");
}
}
@@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Authentication;
namespace Radarr.Http.Authentication
{
@@ -58,7 +59,7 @@ namespace Radarr.Http.Authentication
{
new Claim("user", user.Username),
new Claim("identifier", user.Identifier.ToString()),
new Claim("UiAuth", "true")
new Claim("AuthType", AuthenticationType.Basic.ToString())
};
var identity = new ClaimsIdentity(claims, "Basic", "user", "identifier");
@@ -5,13 +5,13 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NzbDrone.Core.Authentication;
namespace Radarr.Http.Authentication
{
public class NoAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public NoAuthenticationHandler(IAuthenticationService authService,
IOptionsMonitor<AuthenticationSchemeOptions> options,
public NoAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock)
@@ -24,7 +24,7 @@ namespace Radarr.Http.Authentication
var claims = new List<Claim>
{
new Claim("user", "Anonymous"),
new Claim("UiAuth", "true")
new Claim("AuthType", AuthenticationType.None.ToString())
};
var identity = new ClaimsIdentity(claims, "NoAuth", "user", "identifier");
@@ -0,0 +1,39 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using NzbDrone.Core.Configuration;
namespace NzbDrone.Http.Authentication
{
public class UiAuthorizationPolicyProvider : IAuthorizationPolicyProvider
{
private const string POLICY_NAME = "UI";
private readonly IConfigFileProvider _config;
public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }
public UiAuthorizationPolicyProvider(IOptions<AuthorizationOptions> options,
IConfigFileProvider config)
{
FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider(options);
_config = config;
}
public Task<AuthorizationPolicy> GetDefaultPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();
public Task<AuthorizationPolicy> GetFallbackPolicyAsync() => FallbackPolicyProvider.GetFallbackPolicyAsync();
public Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
{
if (policyName.Equals(POLICY_NAME, StringComparison.OrdinalIgnoreCase))
{
var policy = new AuthorizationPolicyBuilder(_config.AuthenticationMethod.ToString())
.RequireAuthenticatedUser();
return Task.FromResult(policy.Build());
}
return FallbackPolicyProvider.GetPolicyAsync(policyName);
}
}
}