New: Forms authentication

This commit is contained in:
Mark McDowall 2015-01-25 18:03:21 -08:00
commit 3c756348eb
35 changed files with 707 additions and 81 deletions

View file

@ -2,14 +2,16 @@
using System.Linq;
using Nancy;
using Nancy.Authentication.Basic;
using Nancy.Authentication.Forms;
using Nancy.Security;
using NzbDrone.Api.Extensions;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Authentication;
using NzbDrone.Core.Configuration;
namespace NzbDrone.Api.Authentication
{
public interface IAuthenticationService : IUserValidator
public interface IAuthenticationService : IUserValidator, IUserMapper
{
bool IsAuthenticated(NancyContext context);
}
@ -17,37 +19,49 @@ namespace NzbDrone.Api.Authentication
public class AuthenticationService : IAuthenticationService
{
private readonly IConfigFileProvider _configFileProvider;
private readonly IUserService _userService;
private static readonly NzbDroneUser AnonymousUser = new NzbDroneUser { UserName = "Anonymous" };
private static String API_KEY;
public AuthenticationService(IConfigFileProvider configFileProvider)
public AuthenticationService(IConfigFileProvider configFileProvider, IUserService userService)
{
_configFileProvider = configFileProvider;
_userService = userService;
API_KEY = configFileProvider.ApiKey;
}
public IUserIdentity Validate(string username, string password)
{
if (!Enabled)
if (_configFileProvider.AuthenticationMethod == AuthenticationType.None)
{
return AnonymousUser;
}
if (_configFileProvider.Username.Equals(username) &&
_configFileProvider.Password.Equals(password))
var user = _userService.FindUser(username, password);
if (user != null)
{
return new NzbDroneUser { UserName = username };
return new NzbDroneUser { UserName = user.Username };
}
return null;
}
private bool Enabled
public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
{
get
if (_configFileProvider.AuthenticationMethod == AuthenticationType.None)
{
return _configFileProvider.AuthenticationEnabled;
return AnonymousUser;
}
var user = _userService.FindUser(identifier);
if (user != null)
{
return new NzbDroneUser { UserName = user.Username };
}
return null;
}
public bool IsAuthenticated(NancyContext context)
@ -59,13 +73,13 @@ namespace NzbDrone.Api.Authentication
return ValidApiKey(apiKey);
}
if (_configFileProvider.AuthenticationMethod == AuthenticationType.None)
{
return true;
}
if (context.Request.IsFeedRequest())
{
if (!Enabled)
{
return true;
}
if (ValidUser(context) || ValidApiKey(apiKey))
{
return true;
@ -74,7 +88,12 @@ namespace NzbDrone.Api.Authentication
return false;
}
if (!Enabled)
if (context.Request.IsLoginRequest())
{
return true;
}
if (context.Request.IsContentRequest())
{
return true;
}