1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 04:22:30 +01:00
Radarr/NzbDrone.Api/Authentication/EnableBasicAuthInNancy.cs
2013-07-14 00:00:50 -07:00

38 lines
1.1 KiB
C#

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;
if (context.CurrentUser == null && _authenticationService.Enabled)
{
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
}
return response;
}
}
}