mirror of
https://github.com/Sonarr/Sonarr
synced 2024-12-27 18:28:19 +00:00
39 lines
No EOL
1.2 KiB
C#
39 lines
No EOL
1.2 KiB
C#
using Nancy;
|
|
using Nancy.Authentication.Basic;
|
|
using Nancy.Bootstrapper;
|
|
using NzbDrone.Common.Model;
|
|
|
|
namespace NzbDrone.Api.Authentication
|
|
{
|
|
public interface IEnableBasicAuthInNancy
|
|
{
|
|
void Register(IPipelines pipelines);
|
|
}
|
|
|
|
public class EnableBasicAuthInNancy : IEnableBasicAuthInNancy
|
|
{
|
|
private readonly IAuthenticationService _authenticationService;
|
|
|
|
public EnableBasicAuthInNancy(IAuthenticationService authenticationService)
|
|
{
|
|
_authenticationService = authenticationService;
|
|
}
|
|
|
|
public void Register(IPipelines pipelines)
|
|
{
|
|
pipelines.EnableBasicAuthentication(new BasicAuthenticationConfiguration(_authenticationService, "NzbDrone"));
|
|
pipelines.BeforeRequest.AddItemToEndOfPipeline(RequiresAuthentication);
|
|
}
|
|
|
|
private Response RequiresAuthentication(NancyContext context)
|
|
{
|
|
Response response = null;
|
|
if (context.CurrentUser == null && _authenticationService.AuthenticationType != AuthenticationType.Anonymous)
|
|
{
|
|
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
|
|
}
|
|
|
|
return response;
|
|
}
|
|
}
|
|
} |