Radarr/src/Radarr.Http/Authentication/AuthenticationService.cs

143 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using Nancy;
2013-09-23 22:31:50 +00:00
using Nancy.Authentication.Basic;
2015-01-26 02:03:21 +00:00
using Nancy.Authentication.Forms;
2013-05-22 00:58:57 +00:00
using Nancy.Security;
using NzbDrone.Common.Extensions;
2015-01-26 02:03:21 +00:00
using NzbDrone.Core.Authentication;
using NzbDrone.Core.Configuration;
2018-11-23 07:03:32 +00:00
using Radarr.Http.Extensions;
2013-05-22 00:58:57 +00:00
2018-11-23 07:03:32 +00:00
namespace Radarr.Http.Authentication
2013-05-22 00:58:57 +00:00
{
2015-01-26 02:03:21 +00:00
public interface IAuthenticationService : IUserValidator, IUserMapper
{
2013-09-23 22:31:50 +00:00
bool IsAuthenticated(NancyContext context);
}
public class AuthenticationService : IAuthenticationService
2013-05-22 00:58:57 +00:00
{
2015-01-26 02:03:21 +00:00
private readonly IUserService _userService;
private static readonly NzbDroneUser AnonymousUser = new NzbDroneUser { UserName = "Anonymous" };
private static string API_KEY;
private static AuthenticationType AUTH_METHOD;
2015-01-26 02:03:21 +00:00
public AuthenticationService(IConfigFileProvider configFileProvider, IUserService userService)
2013-05-22 00:58:57 +00:00
{
2015-01-26 02:03:21 +00:00
_userService = userService;
API_KEY = configFileProvider.ApiKey;
AUTH_METHOD = configFileProvider.AuthenticationMethod;
2013-05-22 00:58:57 +00:00
}
public IUserIdentity Validate(string username, string password)
{
if (AUTH_METHOD == AuthenticationType.None)
2013-05-22 05:55:53 +00:00
{
return AnonymousUser;
2013-05-22 05:55:53 +00:00
}
2015-01-26 02:03:21 +00:00
var user = _userService.FindUser(username, password);
if (user != null)
2013-05-22 00:58:57 +00:00
{
2015-01-26 02:03:21 +00:00
return new NzbDroneUser { UserName = user.Username };
2013-05-22 00:58:57 +00:00
}
return null;
}
2013-07-14 07:00:50 +00:00
2015-01-26 02:03:21 +00:00
public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
2013-07-14 07:00:50 +00:00
{
if (AUTH_METHOD == AuthenticationType.None)
2013-07-14 07:00:50 +00:00
{
2015-01-26 02:03:21 +00:00
return AnonymousUser;
2013-07-14 07:00:50 +00:00
}
2015-01-26 02:03:21 +00:00
var user = _userService.FindUser(identifier);
if (user != null)
{
return new NzbDroneUser { UserName = user.Username };
}
return null;
2013-07-14 07:00:50 +00:00
}
2013-09-23 22:31:50 +00:00
public bool IsAuthenticated(NancyContext context)
{
var apiKey = GetApiKey(context);
2013-09-23 22:31:50 +00:00
if (context.Request.IsApiRequest())
{
return ValidApiKey(apiKey);
}
if (AUTH_METHOD == AuthenticationType.None)
{
2015-01-26 02:03:21 +00:00
return true;
}
2015-01-26 02:03:21 +00:00
if (context.Request.IsFeedRequest())
{
if (ValidUser(context) || ValidApiKey(apiKey))
{
return true;
}
return false;
}
2015-01-26 02:03:21 +00:00
if (context.Request.IsLoginRequest())
{
return true;
}
if (context.Request.IsContentRequest())
{
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;
2013-09-23 22:31:50 +00:00
}
2013-05-22 00:58:57 +00:00
}
}