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

89 lines
2.6 KiB
C#
Raw Normal View History

2021-10-21 20:04:19 +00:00
using Microsoft.AspNetCore.Http;
using NLog;
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
{
2021-10-21 20:04:19 +00:00
public interface IAuthenticationService
{
2021-10-21 20:04:19 +00:00
void LogUnauthorized(HttpRequest context);
User Login(HttpRequest request, string username, string password);
void Logout(HttpContext context);
}
public class AuthenticationService : IAuthenticationService
2013-05-22 00:58:57 +00:00
{
2019-12-22 22:08:53 +00:00
private static readonly Logger _authLogger = LogManager.GetLogger("Auth");
private readonly IUserService _userService;
private static AuthenticationType AUTH_METHOD;
2019-09-01 09:28:07 +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;
AUTH_METHOD = configFileProvider.AuthenticationMethod;
2013-05-22 00:58:57 +00:00
}
2021-10-21 20:04:19 +00:00
public User Login(HttpRequest request, string username, string password)
{
if (AUTH_METHOD == AuthenticationType.None)
{
return null;
}
var user = _userService.FindUser(username, password);
if (user != null)
{
2021-10-21 20:04:19 +00:00
LogSuccess(request, username);
return user;
}
2021-10-21 20:04:19 +00:00
LogFailure(request, username);
return null;
}
2021-10-21 20:04:19 +00:00
public void Logout(HttpContext context)
{
if (AUTH_METHOD == AuthenticationType.None)
{
return;
}
2021-10-21 20:04:19 +00:00
if (context.User != null)
2019-12-22 22:08:53 +00:00
{
2021-10-21 20:04:19 +00:00
LogLogout(context.Request, context.User.Identity.Name);
2019-12-22 22:08:53 +00:00
}
2013-09-23 22:31:50 +00:00
}
2021-10-21 20:04:19 +00:00
public void LogUnauthorized(HttpRequest context)
{
2021-10-21 20:04:19 +00:00
_authLogger.Info("Auth-Unauthorized ip {0} url '{1}'", context.GetRemoteIP(), context.Path);
}
2021-10-21 20:04:19 +00:00
private void LogInvalidated(HttpRequest context)
{
_authLogger.Info("Auth-Invalidated ip {0}", context.GetRemoteIP());
}
2021-10-21 20:04:19 +00:00
private void LogFailure(HttpRequest context, string username)
{
_authLogger.Warn("Auth-Failure ip {0} username '{1}'", context.GetRemoteIP(), username);
}
2021-10-21 20:04:19 +00:00
private void LogSuccess(HttpRequest context, string username)
{
_authLogger.Info("Auth-Success ip {0} username '{1}'", context.GetRemoteIP(), username);
}
2021-10-21 20:04:19 +00:00
private void LogLogout(HttpRequest context, string username)
{
_authLogger.Info("Auth-Logout ip {0} username '{1}'", context.GetRemoteIP(), username);
}
2013-05-22 00:58:57 +00:00
}
}