You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.5 KiB
51 lines
1.5 KiB
6 years ago
|
using System;
|
||
10 years ago
|
using Nancy;
|
||
|
using Nancy.Authentication.Forms;
|
||
|
using Nancy.Extensions;
|
||
|
using Nancy.ModelBinding;
|
||
10 years ago
|
using NzbDrone.Core.Configuration;
|
||
10 years ago
|
|
||
4 years ago
|
namespace Prowlarr.Http.Authentication
|
||
10 years ago
|
{
|
||
10 years ago
|
public class AuthenticationModule : NancyModule
|
||
10 years ago
|
{
|
||
5 years ago
|
private readonly IAuthenticationService _authService;
|
||
10 years ago
|
private readonly IConfigFileProvider _configFileProvider;
|
||
10 years ago
|
|
||
5 years ago
|
public AuthenticationModule(IAuthenticationService authService, IConfigFileProvider configFileProvider)
|
||
10 years ago
|
{
|
||
5 years ago
|
_authService = authService;
|
||
10 years ago
|
_configFileProvider = configFileProvider;
|
||
5 years ago
|
Post("/login", x => Login(this.Bind<LoginResource>()));
|
||
|
Get("/logout", x => Logout());
|
||
10 years ago
|
}
|
||
|
|
||
|
private Response Login(LoginResource resource)
|
||
|
{
|
||
5 years ago
|
var user = _authService.Login(Context, resource.Username, resource.Password);
|
||
10 years ago
|
|
||
|
if (user == null)
|
||
|
{
|
||
6 years ago
|
var returnUrl = (string)Request.Query.returnUrl;
|
||
|
return Context.GetRedirect($"~/login?returnUrl={returnUrl}&loginFailed=true");
|
||
10 years ago
|
}
|
||
|
|
||
|
DateTime? expiry = null;
|
||
|
|
||
|
if (resource.RememberMe)
|
||
|
{
|
||
|
expiry = DateTime.UtcNow.AddDays(7);
|
||
|
}
|
||
|
|
||
8 years ago
|
return this.LoginAndRedirect(user.Identifier, expiry, _configFileProvider.UrlBase + "/");
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
private Response Logout()
|
||
|
{
|
||
5 years ago
|
_authService.Logout(Context);
|
||
|
|
||
10 years ago
|
return this.LogoutAndRedirect(_configFileProvider.UrlBase + "/");
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|