2013-09-21 01:19:48 +03:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2013-09-20 10:31:02 +03:00
|
|
|
using Nancy;
|
|
|
|
using Nancy.Bootstrapper;
|
2013-09-21 01:19:48 +03:00
|
|
|
using NzbDrone.Api.Extensions;
|
|
|
|
using NzbDrone.Api.Extensions.Pipelines;
|
2013-09-20 10:31:02 +03:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Authentication
|
|
|
|
{
|
2013-09-21 01:19:48 +03:00
|
|
|
public class EnableStatelessAuthInNancy : IRegisterNancyPipeline
|
2013-09-20 10:31:02 +03:00
|
|
|
{
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
|
|
|
|
|
|
public EnableStatelessAuthInNancy(IConfigFileProvider configFileProvider)
|
|
|
|
{
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Register(IPipelines pipelines)
|
|
|
|
{
|
|
|
|
pipelines.BeforeRequest.AddItemToEndOfPipeline(ValidateApiKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Response ValidateApiKey(NancyContext context)
|
|
|
|
{
|
|
|
|
Response response = null;
|
2013-09-21 01:19:48 +03:00
|
|
|
|
|
|
|
if (!RuntimeInfo.IsProduction && context.Request.IsLocalRequest())
|
2013-09-20 10:31:02 +03:00
|
|
|
{
|
|
|
|
return response;
|
|
|
|
}
|
2013-09-21 01:19:48 +03:00
|
|
|
|
|
|
|
var apiKey = context.Request.Headers.Authorization;
|
2013-09-20 10:31:02 +03:00
|
|
|
|
2013-09-21 01:19:48 +03:00
|
|
|
if (context.Request.IsApiRequest() &&
|
|
|
|
(String.IsNullOrWhiteSpace(apiKey) || !apiKey.Equals(_configFileProvider.ApiKey)))
|
2013-09-20 10:31:02 +03:00
|
|
|
{
|
|
|
|
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|