2013-05-23 05:10:02 +03:00
|
|
|
using Nancy;
|
|
|
|
using Nancy.Authentication.Basic;
|
|
|
|
using Nancy.Bootstrapper;
|
|
|
|
|
|
|
|
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;
|
2013-07-14 10:00:50 +03:00
|
|
|
if (context.CurrentUser == null && _authenticationService.Enabled)
|
2013-05-23 05:10:02 +03:00
|
|
|
{
|
|
|
|
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|