2021-10-21 20:04:19 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-08-27 21:29:16 +00:00
|
|
|
using NLog;
|
2015-01-26 02:03:21 +00:00
|
|
|
using NzbDrone.Core.Authentication;
|
2013-05-23 05:12:01 +00:00
|
|
|
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
|
2013-05-23 02:10:02 +00:00
|
|
|
{
|
2021-10-21 20:04:19 +00:00
|
|
|
void LogUnauthorized(HttpRequest context);
|
|
|
|
User Login(HttpRequest request, string username, string password);
|
|
|
|
void Logout(HttpContext context);
|
2013-05-23 02:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2019-08-27 21:29:16 +00:00
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
2015-02-02 19:54:49 +00:00
|
|
|
private static AuthenticationType AUTH_METHOD;
|
2013-05-23 02:10:02 +00:00
|
|
|
|
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;
|
2015-02-02 19:54:49 +00:00
|
|
|
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)
|
2019-08-27 21:29:16 +00:00
|
|
|
{
|
|
|
|
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);
|
2019-08-27 21:29:16 +00:00
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
LogFailure(request, username);
|
2019-08-27 21:29:16 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
public void Logout(HttpContext context)
|
2019-08-27 21:29:16 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2019-08-27 21:29:16 +00:00
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
public void LogUnauthorized(HttpRequest context)
|
2019-08-27 21:29:16 +00:00
|
|
|
{
|
2021-10-21 20:04:19 +00:00
|
|
|
_authLogger.Info("Auth-Unauthorized ip {0} url '{1}'", context.GetRemoteIP(), context.Path);
|
2019-08-27 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
private void LogInvalidated(HttpRequest context)
|
2019-08-27 21:29:16 +00:00
|
|
|
{
|
2020-05-05 18:14:07 +00:00
|
|
|
_authLogger.Info("Auth-Invalidated ip {0}", context.GetRemoteIP());
|
2019-08-27 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
private void LogFailure(HttpRequest context, string username)
|
2019-08-27 21:29:16 +00:00
|
|
|
{
|
2020-05-05 18:14:07 +00:00
|
|
|
_authLogger.Warn("Auth-Failure ip {0} username '{1}'", context.GetRemoteIP(), username);
|
2019-08-27 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
private void LogSuccess(HttpRequest context, string username)
|
2019-08-27 21:29:16 +00:00
|
|
|
{
|
2020-05-05 18:14:07 +00:00
|
|
|
_authLogger.Info("Auth-Success ip {0} username '{1}'", context.GetRemoteIP(), username);
|
2019-08-27 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
private void LogLogout(HttpRequest context, string username)
|
2019-08-27 21:29:16 +00:00
|
|
|
{
|
2020-05-05 18:14:07 +00:00
|
|
|
_authLogger.Info("Auth-Logout ip {0} username '{1}'", context.GetRemoteIP(), username);
|
2019-08-27 21:29:16 +00:00
|
|
|
}
|
2013-05-22 00:58:57 +00:00
|
|
|
}
|
|
|
|
}
|