mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 05:53:33 -07:00
Added API key authentication
This commit is contained in:
parent
689f27bee6
commit
57fdbe6e08
11 changed files with 114 additions and 15 deletions
50
NzbDrone.Api/Authentication/EnableStatelessAuthInNancy.cs
Normal file
50
NzbDrone.Api/Authentication/EnableStatelessAuthInNancy.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using System.Linq;
|
||||
using Nancy;
|
||||
using Nancy.Bootstrapper;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Core.Configuration;
|
||||
|
||||
namespace NzbDrone.Api.Authentication
|
||||
{
|
||||
public interface IEnableStatelessAuthInNancy
|
||||
{
|
||||
void Register(IPipelines pipelines);
|
||||
}
|
||||
|
||||
public class EnableStatelessAuthInNancy : IEnableStatelessAuthInNancy
|
||||
{
|
||||
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;
|
||||
var apiKey = context.Request.Headers["ApiKey"].FirstOrDefault();
|
||||
|
||||
if (!RuntimeInfo.IsProduction &&
|
||||
(context.Request.UserHostAddress.Equals("localhost") ||
|
||||
context.Request.UserHostAddress.Equals("127.0.0.1") ||
|
||||
context.Request.UserHostAddress.Equals("::1")))
|
||||
{
|
||||
return response;
|
||||
}
|
||||
|
||||
if (context.Request.Path.StartsWith("/api/") &&
|
||||
(apiKey == null || !apiKey.Equals(_configFileProvider.ApiKey)))
|
||||
{
|
||||
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue