sonarr-repo-only/NzbDrone.Api/Authentication/EnableBasicAuthInNancy.cs

38 lines
1.2 KiB
C#
Raw Normal View History

using Nancy;
using Nancy.Authentication.Basic;
using Nancy.Bootstrapper;
2013-09-20 22:19:48 +00:00
using NzbDrone.Api.Extensions;
using NzbDrone.Api.Extensions.Pipelines;
namespace NzbDrone.Api.Authentication
{
2013-09-20 22:19:48 +00:00
public class EnableBasicAuthInNancy : IRegisterNancyPipeline
{
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;
2013-09-20 07:31:02 +00:00
2013-09-20 22:19:48 +00:00
if (!context.Request.IsApiRequest() &&
2013-09-20 07:31:02 +00:00
context.CurrentUser == null &&
_authenticationService.Enabled)
{
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
}
return response;
}
}
}