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