2013-05-22 05:55:53 +00:00
|
|
|
|
using Nancy.Authentication.Basic;
|
2013-05-22 00:58:57 +00:00
|
|
|
|
using Nancy.Security;
|
2013-05-23 05:12:01 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2013-05-22 00:58:57 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Authentication
|
|
|
|
|
{
|
2013-05-23 02:10:02 +00:00
|
|
|
|
public interface IAuthenticationService : IUserValidator
|
|
|
|
|
{
|
2013-07-14 07:00:50 +00:00
|
|
|
|
bool Enabled { get; }
|
2013-05-23 02:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class AuthenticationService : IAuthenticationService
|
2013-05-22 00:58:57 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
2013-05-23 02:10:02 +00:00
|
|
|
|
private static readonly NzbDroneUser AnonymousUser = new NzbDroneUser { UserName = "Anonymous" };
|
|
|
|
|
|
2013-05-22 00:58:57 +00:00
|
|
|
|
|
2013-05-23 02:10:02 +00:00
|
|
|
|
public AuthenticationService(IConfigFileProvider configFileProvider)
|
2013-05-22 00:58:57 +00:00
|
|
|
|
{
|
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-23 02:10:02 +00:00
|
|
|
|
|
2013-05-22 00:58:57 +00:00
|
|
|
|
public IUserIdentity Validate(string username, string password)
|
|
|
|
|
{
|
2013-07-14 07:00:50 +00:00
|
|
|
|
if (!Enabled)
|
2013-05-22 05:55:53 +00:00
|
|
|
|
{
|
2013-05-23 02:10:02 +00:00
|
|
|
|
return AnonymousUser;
|
2013-05-22 05:55:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-14 07:00:50 +00:00
|
|
|
|
if (_configFileProvider.Username.Equals(username) &&
|
|
|
|
|
_configFileProvider.Password.Equals(password))
|
2013-05-22 00:58:57 +00:00
|
|
|
|
{
|
2013-05-22 05:55:53 +00:00
|
|
|
|
return new NzbDroneUser { UserName = username };
|
2013-05-22 00:58:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-07-14 07:00:50 +00:00
|
|
|
|
|
|
|
|
|
public bool Enabled
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _configFileProvider.AuthenticationEnabled;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-22 00:58:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|