1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Api/Authentication/AuthenticationService.cs

59 lines
1.5 KiB
C#
Raw Normal View History

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