2021-11-06 20:44:26 +00:00
|
|
|
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())
|
2022-05-23 21:00:26 +00:00
|
|
|
.AddRequirements(new BypassableDenyAnonymousAuthorizationRequirement());
|
|
|
|
|
2021-11-06 20:44:26 +00:00
|
|
|
return Task.FromResult(policy.Build());
|
|
|
|
}
|
|
|
|
|
|
|
|
return FallbackPolicyProvider.GetPolicyAsync(policyName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|