2013-05-23 05:10:02 +03:00
|
|
|
using Nancy;
|
|
|
|
using Nancy.Authentication.Basic;
|
|
|
|
using Nancy.Bootstrapper;
|
2013-09-21 01:19:48 +03:00
|
|
|
using NzbDrone.Api.Extensions;
|
|
|
|
using NzbDrone.Api.Extensions.Pipelines;
|
2013-05-23 05:10:02 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Api.Authentication
|
|
|
|
{
|
2013-09-21 01:19:48 +03:00
|
|
|
public class EnableBasicAuthInNancy : IRegisterNancyPipeline
|
2013-05-23 05:10:02 +03:00
|
|
|
{
|
|
|
|
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 10:31:02 +03:00
|
|
|
|
2013-09-24 01:31:50 +03:00
|
|
|
if (!context.Request.IsApiRequest() && !_authenticationService.IsAuthenticated(context))
|
2013-05-23 05:10:02 +03:00
|
|
|
{
|
|
|
|
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|