You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using Nancy;
|
|
|
|
|
using Nancy.Authentication.Basic;
|
|
|
|
|
using Nancy.Bootstrapper;
|
|
|
|
|
using NzbDrone.Api.Extensions;
|
|
|
|
|
using NzbDrone.Api.Extensions.Pipelines;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Authentication
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (!context.Request.IsApiRequest() &&
|
|
|
|
|
context.CurrentUser == null &&
|
|
|
|
|
_authenticationService.Enabled)
|
|
|
|
|
{
|
|
|
|
|
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|