diff --git a/src/NzbDrone.Api/Authentication/AuthenticationService.cs b/src/NzbDrone.Api/Authentication/AuthenticationService.cs
index b71b7a07e..997e3a93b 100644
--- a/src/NzbDrone.Api/Authentication/AuthenticationService.cs
+++ b/src/NzbDrone.Api/Authentication/AuthenticationService.cs
@@ -1,6 +1,10 @@
-using Nancy;
+using System;
+using System.Linq;
+using Nancy;
using Nancy.Authentication.Basic;
using Nancy.Security;
+using NzbDrone.Api.Extensions;
+using NzbDrone.Common;
using NzbDrone.Core.Configuration;
namespace NzbDrone.Api.Authentication
@@ -15,10 +19,12 @@ namespace NzbDrone.Api.Authentication
{
private readonly IConfigFileProvider _configFileProvider;
private static readonly NzbDroneUser AnonymousUser = new NzbDroneUser { UserName = "Anonymous" };
+ private static String API_KEY;
public AuthenticationService(IConfigFileProvider configFileProvider)
{
_configFileProvider = configFileProvider;
+ API_KEY = configFileProvider.ApiKey;
}
public IUserIdentity Validate(string username, string password)
@@ -47,9 +53,71 @@ namespace NzbDrone.Api.Authentication
public bool IsAuthenticated(NancyContext context)
{
- if (context.CurrentUser == null && _configFileProvider.AuthenticationEnabled) return false;
+ var apiKey = GetApiKey(context);
- return true;
+ if (context.Request.IsApiRequest())
+ {
+ return ValidApiKey(apiKey);
+ }
+
+ if (context.Request.IsFeedRequest())
+ {
+ if (!Enabled)
+ {
+ return true;
+ }
+
+ if (ValidUser(context) || ValidApiKey(apiKey))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ if (!Enabled)
+ {
+ return true;
+ }
+
+ if (ValidUser(context))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ private bool ValidUser(NancyContext context)
+ {
+ if (context.CurrentUser != null) return true;
+
+ return false;
+ }
+
+ private bool ValidApiKey(string apiKey)
+ {
+ if (API_KEY.Equals(apiKey)) return true;
+
+ return false;
+ }
+
+ private string GetApiKey(NancyContext context)
+ {
+ var apiKeyHeader = context.Request.Headers["X-Api-Key"].FirstOrDefault();
+ var apiKeyQueryString = context.Request.Query["ApiKey"];
+
+ if (!apiKeyHeader.IsNullOrWhiteSpace())
+ {
+ return apiKeyHeader;
+ }
+
+ if (apiKeyQueryString.HasValue)
+ {
+ return apiKeyQueryString.Value;
+ }
+
+ return context.Request.Headers.Authorization;
}
}
}
diff --git a/src/NzbDrone.Api/Authentication/EnableBasicAuthInNancy.cs b/src/NzbDrone.Api/Authentication/EnableAuthInNancy.cs
similarity index 75%
rename from src/NzbDrone.Api/Authentication/EnableBasicAuthInNancy.cs
rename to src/NzbDrone.Api/Authentication/EnableAuthInNancy.cs
index c5622eb75..8ded9e32b 100644
--- a/src/NzbDrone.Api/Authentication/EnableBasicAuthInNancy.cs
+++ b/src/NzbDrone.Api/Authentication/EnableAuthInNancy.cs
@@ -1,16 +1,15 @@
using Nancy;
using Nancy.Authentication.Basic;
using Nancy.Bootstrapper;
-using NzbDrone.Api.Extensions;
using NzbDrone.Api.Extensions.Pipelines;
namespace NzbDrone.Api.Authentication
{
- public class EnableBasicAuthInNancy : IRegisterNancyPipeline
+ public class EnableAuthInNancy : IRegisterNancyPipeline
{
private readonly IAuthenticationService _authenticationService;
- public EnableBasicAuthInNancy(IAuthenticationService authenticationService)
+ public EnableAuthInNancy(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
@@ -25,7 +24,7 @@ namespace NzbDrone.Api.Authentication
{
Response response = null;
- if (!context.Request.IsApiRequest() && !_authenticationService.IsAuthenticated(context))
+ if (!_authenticationService.IsAuthenticated(context))
{
response = new Response { StatusCode = HttpStatusCode.Unauthorized };
}
diff --git a/src/NzbDrone.Api/Authentication/EnableStatelessAuthInNancy.cs b/src/NzbDrone.Api/Authentication/EnableStatelessAuthInNancy.cs
deleted file mode 100644
index e6aaeae27..000000000
--- a/src/NzbDrone.Api/Authentication/EnableStatelessAuthInNancy.cs
+++ /dev/null
@@ -1,65 +0,0 @@
-using System;
-using System.Linq;
-using Nancy;
-using Nancy.Bootstrapper;
-using NzbDrone.Api.Extensions;
-using NzbDrone.Api.Extensions.Pipelines;
-using NzbDrone.Common;
-using NzbDrone.Core.Configuration;
-
-namespace NzbDrone.Api.Authentication
-{
- public class EnableStatelessAuthInNancy : IRegisterNancyPipeline
- {
- private static String API_KEY;
-
- public EnableStatelessAuthInNancy(IConfigFileProvider configFileProvider)
- {
- API_KEY = configFileProvider.ApiKey;
- }
-
- public void Register(IPipelines pipelines)
- {
- pipelines.BeforeRequest.AddItemToEndOfPipeline(ValidateApiKey);
- }
-
- public Response ValidateApiKey(NancyContext context)
- {
- Response response = null;
-
- var apiKey = GetApiKey(context);
-
- if ((context.Request.IsApiRequest() || context.Request.IsFeedRequest()) && !ValidApiKey(apiKey))
- {
- response = new Response { StatusCode = HttpStatusCode.Unauthorized };
- }
-
- return response;
- }
-
- private bool ValidApiKey(string apiKey)
- {
- if (!API_KEY.Equals(apiKey)) return false;
-
- return true;
- }
-
- private string GetApiKey(NancyContext context)
- {
- var apiKeyHeader = context.Request.Headers["X-Api-Key"].FirstOrDefault();
- var apiKeyQueryString = context.Request.Query["ApiKey"];
-
- if (!apiKeyHeader.IsNullOrWhiteSpace())
- {
- return apiKeyHeader;
- }
-
- if (apiKeyQueryString.HasValue)
- {
- return apiKeyQueryString.Value;
- }
-
- return context.Request.Headers.Authorization;
- }
- }
-}
\ No newline at end of file
diff --git a/src/NzbDrone.Api/NzbDrone.Api.csproj b/src/NzbDrone.Api/NzbDrone.Api.csproj
index e6d6a32c9..dd8950919 100644
--- a/src/NzbDrone.Api/NzbDrone.Api.csproj
+++ b/src/NzbDrone.Api/NzbDrone.Api.csproj
@@ -85,8 +85,7 @@
Properties\SharedAssemblyInfo.cs
-
-
+