2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.Globalization;
|
2021-11-11 13:03:08 +01:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
2023-01-06 14:18:07 +01:00
|
|
|
using System.Security.Claims;
|
2017-09-13 08:47:34 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
|
|
|
using BTCPayServer.Abstractions.Models;
|
2023-03-20 02:46:46 +01:00
|
|
|
using BTCPayServer.Client;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Data;
|
|
|
|
using BTCPayServer.Events;
|
2021-04-20 07:06:32 +02:00
|
|
|
using BTCPayServer.Fido2;
|
|
|
|
using BTCPayServer.Fido2.Models;
|
2023-03-20 02:46:46 +01:00
|
|
|
using BTCPayServer.Filters;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Logging;
|
2017-09-13 08:47:34 +02:00
|
|
|
using BTCPayServer.Models.AccountViewModels;
|
|
|
|
using BTCPayServer.Services;
|
2021-04-20 07:06:32 +02:00
|
|
|
using Fido2NetLib;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-11-11 13:03:08 +01:00
|
|
|
using Microsoft.AspNetCore.Routing;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-11-11 13:03:08 +01:00
|
|
|
using NBitcoin.DataEncoders;
|
2021-04-20 07:06:32 +02:00
|
|
|
using Newtonsoft.Json.Linq;
|
2018-08-25 13:28:46 +02:00
|
|
|
using NicolasDorier.RateLimits;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
2019-10-12 13:35:30 +02:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
|
2022-01-07 04:32:00 +01:00
|
|
|
public class UIAccountController : Controller
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
private readonly SignInManager<ApplicationUser> _signInManager;
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly RoleManager<IdentityRole> _RoleManager;
|
|
|
|
readonly Configuration.BTCPayServerOptions _Options;
|
2019-05-02 14:01:08 +02:00
|
|
|
private readonly BTCPayServerEnvironment _btcPayServerEnvironment;
|
2022-05-24 06:18:16 +02:00
|
|
|
readonly SettingsRepository _SettingsRepository;
|
2021-04-20 07:06:32 +02:00
|
|
|
private readonly Fido2Service _fido2Service;
|
2021-11-11 13:03:08 +01:00
|
|
|
private readonly LnurlAuthService _lnurlAuthService;
|
|
|
|
private readonly LinkGenerator _linkGenerator;
|
2021-12-24 09:27:00 +01:00
|
|
|
private readonly UserLoginCodeService _userLoginCodeService;
|
2020-03-13 11:47:22 +01:00
|
|
|
private readonly EventAggregator _eventAggregator;
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly ILogger _logger;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2022-05-24 06:18:16 +02:00
|
|
|
public PoliciesSettings PoliciesSettings { get; }
|
2021-11-22 09:16:08 +01:00
|
|
|
public Logs Logs { get; }
|
|
|
|
|
2022-01-07 04:32:00 +01:00
|
|
|
public UIAccountController(
|
2017-10-27 10:53:04 +02:00
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
RoleManager<IdentityRole> roleManager,
|
|
|
|
SignInManager<ApplicationUser> signInManager,
|
2022-05-24 06:18:16 +02:00
|
|
|
PoliciesSettings policiesSettings,
|
2019-01-06 14:55:18 +01:00
|
|
|
SettingsRepository settingsRepository,
|
2019-05-02 14:01:08 +02:00
|
|
|
Configuration.BTCPayServerOptions options,
|
|
|
|
BTCPayServerEnvironment btcPayServerEnvironment,
|
2021-04-20 07:06:32 +02:00
|
|
|
EventAggregator eventAggregator,
|
2021-11-22 09:16:08 +01:00
|
|
|
Fido2Service fido2Service,
|
2021-12-24 09:27:00 +01:00
|
|
|
UserLoginCodeService userLoginCodeService,
|
2021-11-11 13:03:08 +01:00
|
|
|
LnurlAuthService lnurlAuthService,
|
|
|
|
LinkGenerator linkGenerator,
|
2021-11-22 09:16:08 +01:00
|
|
|
Logs logs)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
_userManager = userManager;
|
|
|
|
_signInManager = signInManager;
|
2022-05-24 06:18:16 +02:00
|
|
|
PoliciesSettings = policiesSettings;
|
2017-10-27 10:53:04 +02:00
|
|
|
_SettingsRepository = settingsRepository;
|
2022-05-24 06:18:16 +02:00
|
|
|
_RoleManager = roleManager;
|
2019-01-06 16:43:55 +01:00
|
|
|
_Options = options;
|
2019-05-02 14:01:08 +02:00
|
|
|
_btcPayServerEnvironment = btcPayServerEnvironment;
|
2021-04-20 07:06:32 +02:00
|
|
|
_fido2Service = fido2Service;
|
2021-11-11 13:03:08 +01:00
|
|
|
_lnurlAuthService = lnurlAuthService;
|
|
|
|
_linkGenerator = linkGenerator;
|
2021-12-24 09:27:00 +01:00
|
|
|
_userLoginCodeService = userLoginCodeService;
|
2020-03-13 11:47:22 +01:00
|
|
|
_eventAggregator = eventAggregator;
|
2021-11-22 09:16:08 +01:00
|
|
|
_logger = logs.PayServer;
|
|
|
|
Logs = logs;
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[TempData]
|
|
|
|
public string ErrorMessage
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
|
|
|
|
2023-03-20 02:46:46 +01:00
|
|
|
[HttpGet("/cheat/permissions")]
|
|
|
|
[HttpGet("/cheat/permissions/stores/{storeId}")]
|
|
|
|
[CheatModeRoute]
|
2023-04-10 04:07:03 +02:00
|
|
|
public async Task<IActionResult> CheatPermissions([FromServices] IAuthorizationService authorizationService, string storeId = null)
|
2023-03-20 02:46:46 +01:00
|
|
|
{
|
|
|
|
var vm = new CheatPermissionsViewModel();
|
|
|
|
vm.StoreId = storeId;
|
|
|
|
var results = new System.Collections.Generic.List<(string, Task<AuthorizationResult>)>();
|
|
|
|
foreach (var p in Policies.AllPolicies.Concat(new[] { Policies.CanModifyStoreSettingsUnscoped }))
|
|
|
|
{
|
|
|
|
results.Add((p, authorizationService.AuthorizeAsync(User, storeId, p)));
|
|
|
|
}
|
|
|
|
await Task.WhenAll(results.Select(r => r.Item2));
|
|
|
|
results = results.OrderBy(r => r.Item1).ToList();
|
|
|
|
vm.Permissions = results.Select(r => (r.Item1, r.Item2.Result)).ToArray();
|
|
|
|
return View(vm);
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/login")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
2020-09-05 12:16:48 +02:00
|
|
|
public async Task<IActionResult> Login(string returnUrl = null, string email = null)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-07-15 10:18:30 +02:00
|
|
|
if (User.Identity.IsAuthenticated && string.IsNullOrEmpty(returnUrl))
|
2022-05-24 06:18:16 +02:00
|
|
|
return RedirectToLocal();
|
2017-10-27 10:53:04 +02:00
|
|
|
// Clear the existing external cookie to ensure a clean login process
|
|
|
|
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
|
|
|
|
|
2019-11-16 08:57:16 +01:00
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
SetInsecureFlags();
|
|
|
|
}
|
2020-02-29 06:15:14 +01:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
2021-12-24 09:27:00 +01:00
|
|
|
return View(nameof(Login), new LoginViewModel() { Email = email });
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 19:01:26 +01:00
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpPost("/login/code")]
|
2021-12-24 09:27:00 +01:00
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
[RateLimitsFilter(ZoneLimits.Login, Scope = RateLimitsScope.RemoteAddress)]
|
|
|
|
|
|
|
|
public async Task<IActionResult> LoginWithCode(string loginCode, string returnUrl = null)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(loginCode))
|
|
|
|
{
|
|
|
|
var userId = _userLoginCodeService.Verify(loginCode);
|
|
|
|
if (userId is null)
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(string.Empty,
|
|
|
|
"Login code was invalid");
|
|
|
|
return await Login(returnUrl, null);
|
2021-12-31 08:59:02 +01:00
|
|
|
}
|
|
|
|
var user = await _userManager.FindByIdAsync(userId);
|
2021-12-24 09:27:00 +01:00
|
|
|
|
|
|
|
_logger.LogInformation("User with ID {UserId} logged in with a login code.", user.Id);
|
|
|
|
await _signInManager.SignInAsync(user, false, "LoginCode");
|
2022-05-24 06:18:16 +02:00
|
|
|
return RedirectToLocal(returnUrl);
|
2021-12-24 09:27:00 +01:00
|
|
|
}
|
|
|
|
return await Login(returnUrl, null);
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpPost("/login")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
2018-08-25 13:28:46 +02:00
|
|
|
[RateLimitsFilter(ZoneLimits.Login, Scope = RateLimitsScope.RemoteAddress)]
|
2017-10-27 10:53:04 +02:00
|
|
|
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
|
|
|
|
{
|
2019-11-14 19:01:26 +01:00
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
2019-11-16 08:57:16 +01:00
|
|
|
return RedirectToAction("Login");
|
2019-11-14 19:01:26 +01:00
|
|
|
}
|
2021-12-24 09:27:00 +01:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
{
|
|
|
|
// Require the user to have a confirmed email before they can log on.
|
|
|
|
var user = await _userManager.FindByEmailAsync(model.Email);
|
|
|
|
if (user != null)
|
|
|
|
{
|
|
|
|
if (user.RequiresEmailConfirmation && !await _userManager.IsEmailConfirmedAsync(user))
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(string.Empty,
|
|
|
|
"You must have a confirmed email to log in.");
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 05:34:13 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
|
|
|
|
return View(model);
|
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-04-20 07:06:32 +02:00
|
|
|
var fido2Devices = await _fido2Service.HasCredentials(user.Id);
|
2021-11-11 13:03:08 +01:00
|
|
|
var lnurlAuthCredentials = await _lnurlAuthService.HasCredentials(user.Id);
|
2023-01-06 14:18:07 +01:00
|
|
|
if (!await _userManager.IsLockedOutAsync(user) && (fido2Devices || lnurlAuthCredentials))
|
2019-05-02 14:01:08 +02:00
|
|
|
{
|
|
|
|
if (await _userManager.CheckPasswordAsync(user, model.Password))
|
|
|
|
{
|
|
|
|
LoginWith2faViewModel twoFModel = null;
|
2020-02-29 06:15:14 +01:00
|
|
|
|
2019-05-02 14:01:08 +02:00
|
|
|
if (user.TwoFactorEnabled)
|
|
|
|
{
|
|
|
|
// we need to do an actual sign in attempt so that 2fa can function in next step
|
|
|
|
await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);
|
|
|
|
twoFModel = new LoginWith2faViewModel
|
|
|
|
{
|
|
|
|
RememberMe = model.RememberMe
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return View("SecondaryLogin", new SecondaryLoginViewModel()
|
|
|
|
{
|
|
|
|
LoginWith2FaViewModel = twoFModel,
|
2023-01-06 14:18:07 +01:00
|
|
|
LoginWithFido2ViewModel = fido2Devices ? await BuildFido2ViewModel(model.RememberMe, user) : null,
|
|
|
|
LoginWithLNURLAuthViewModel = lnurlAuthCredentials ? await BuildLNURLAuthViewModel(model.RememberMe, user) : null,
|
2019-05-02 14:01:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-29 06:15:14 +01:00
|
|
|
var incrementAccessFailedResult = await _userManager.AccessFailedAsync(user);
|
|
|
|
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
|
|
|
|
return View(model);
|
|
|
|
|
2019-05-02 14:01:08 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-29 06:15:14 +01:00
|
|
|
|
|
|
|
|
2018-09-12 13:36:44 +02:00
|
|
|
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);
|
2017-10-27 10:53:04 +02:00
|
|
|
if (result.Succeeded)
|
|
|
|
{
|
2021-04-15 04:19:06 +02:00
|
|
|
_logger.LogInformation($"User '{user.Id}' logged in.");
|
2022-05-24 06:18:16 +02:00
|
|
|
return RedirectToLocal(returnUrl);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
if (result.RequiresTwoFactor)
|
|
|
|
{
|
2019-05-02 14:01:08 +02:00
|
|
|
return View("SecondaryLogin", new SecondaryLoginViewModel()
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-05-02 14:01:08 +02:00
|
|
|
LoginWith2FaViewModel = new LoginWith2faViewModel()
|
|
|
|
{
|
|
|
|
RememberMe = model.RememberMe
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (result.IsLockedOut)
|
|
|
|
{
|
2021-04-15 04:19:06 +02:00
|
|
|
_logger.LogWarning($"User '{user.Id}' account locked out.");
|
2023-01-06 14:18:07 +01:00
|
|
|
return RedirectToAction(nameof(Lockout), new { user.LockoutEnd });
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, something failed, redisplay form
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
2021-04-20 07:06:32 +02:00
|
|
|
private async Task<LoginWithFido2ViewModel> BuildFido2ViewModel(bool rememberMe, ApplicationUser user)
|
|
|
|
{
|
2022-11-21 19:32:19 +01:00
|
|
|
if (_btcPayServerEnvironment.IsSecure(HttpContext))
|
2021-04-20 07:06:32 +02:00
|
|
|
{
|
|
|
|
var r = await _fido2Service.RequestLogin(user.Id);
|
|
|
|
if (r is null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new LoginWithFido2ViewModel()
|
|
|
|
{
|
|
|
|
Data = r,
|
|
|
|
UserId = user.Id,
|
|
|
|
RememberMe = rememberMe
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:03:08 +01:00
|
|
|
|
|
|
|
private async Task<LoginWithLNURLAuthViewModel> BuildLNURLAuthViewModel(bool rememberMe, ApplicationUser user)
|
|
|
|
{
|
2022-11-21 19:32:19 +01:00
|
|
|
if (_btcPayServerEnvironment.IsSecure(HttpContext))
|
2021-11-11 13:03:08 +01:00
|
|
|
{
|
|
|
|
var r = await _lnurlAuthService.RequestLogin(user.Id);
|
|
|
|
if (r is null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new LoginWithLNURLAuthViewModel()
|
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2021-11-11 13:03:08 +01:00
|
|
|
RememberMe = rememberMe,
|
|
|
|
UserId = user.Id,
|
|
|
|
LNURLEndpoint = new Uri(_linkGenerator.GetUriByAction(
|
2022-01-25 03:51:14 +01:00
|
|
|
action: nameof(UILNURLAuthController.LoginResponse),
|
|
|
|
controller: "UILNURLAuth",
|
2023-01-06 14:18:07 +01:00
|
|
|
values: new { userId = user.Id, action = "login", tag = "login", k1 = Encoders.Hex.EncodeData(r) }, Request.Scheme, Request.Host, Request.PathBase))
|
2021-11-11 13:03:08 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2021-11-11 13:03:08 +01:00
|
|
|
[HttpPost("/login/lnurlauth")]
|
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
public async Task<IActionResult> LoginWithLNURLAuth(LoginWithLNURLAuthViewModel viewModel, string returnUrl = null)
|
|
|
|
{
|
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
|
|
var user = await _userManager.FindByIdAsync(viewModel.UserId);
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
var errorMessage = string.Empty;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var k1 = Encoders.Hex.DecodeData(viewModel.LNURLEndpoint.ParseQueryString().Get("k1"));
|
|
|
|
if (_lnurlAuthService.FinalLoginStore.TryRemove(viewModel.UserId, out var storedk1) &&
|
|
|
|
storedk1.SequenceEqual(k1))
|
|
|
|
{
|
|
|
|
_lnurlAuthService.FinalLoginStore.TryRemove(viewModel.UserId, out _);
|
|
|
|
await _signInManager.SignInAsync(user, viewModel.RememberMe, "FIDO2");
|
|
|
|
_logger.LogInformation("User logged in.");
|
2022-05-24 06:18:16 +02:00
|
|
|
return RedirectToLocal(returnUrl);
|
2021-11-11 13:03:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errorMessage = "Invalid login attempt.";
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
errorMessage = e.Message;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelState.AddModelError(string.Empty, errorMessage);
|
|
|
|
return View("SecondaryLogin", new SecondaryLoginViewModel()
|
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2021-11-11 13:03:08 +01:00
|
|
|
LoginWithFido2ViewModel = (await _fido2Service.HasCredentials(user.Id)) ? await BuildFido2ViewModel(viewModel.RememberMe, user) : null,
|
|
|
|
LoginWithLNURLAuthViewModel = viewModel,
|
|
|
|
LoginWith2FaViewModel = !user.TwoFactorEnabled
|
|
|
|
? null
|
|
|
|
: new LoginWith2faViewModel()
|
|
|
|
{
|
|
|
|
RememberMe = viewModel.RememberMe
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2021-11-11 13:03:08 +01:00
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpPost("/login/fido2")]
|
2021-04-20 07:06:32 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
public async Task<IActionResult> LoginWithFido2(LoginWithFido2ViewModel viewModel, string returnUrl = null)
|
|
|
|
{
|
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
|
|
var user = await _userManager.FindByIdAsync(viewModel.UserId);
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
var errorMessage = string.Empty;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (await _fido2Service.CompleteLogin(viewModel.UserId, JObject.Parse(viewModel.Response).ToObject<AuthenticatorAssertionRawResponse>()))
|
|
|
|
{
|
|
|
|
await _signInManager.SignInAsync(user, viewModel.RememberMe, "FIDO2");
|
|
|
|
_logger.LogInformation("User logged in.");
|
2022-05-24 06:18:16 +02:00
|
|
|
return RedirectToLocal(returnUrl);
|
2021-04-20 07:06:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errorMessage = "Invalid login attempt.";
|
|
|
|
}
|
2021-04-28 09:22:09 +02:00
|
|
|
catch (Fido2VerificationException e)
|
2021-04-20 07:06:32 +02:00
|
|
|
{
|
|
|
|
errorMessage = e.Message;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelState.AddModelError(string.Empty, errorMessage);
|
|
|
|
viewModel.Response = null;
|
|
|
|
return View("SecondaryLogin", new SecondaryLoginViewModel()
|
|
|
|
{
|
|
|
|
LoginWithFido2ViewModel = viewModel,
|
2021-11-11 13:03:08 +01:00
|
|
|
LoginWithLNURLAuthViewModel = (await _lnurlAuthService.HasCredentials(user.Id)) ? await BuildLNURLAuthViewModel(viewModel.RememberMe, user) : null,
|
2019-05-02 14:01:08 +02:00
|
|
|
LoginWith2FaViewModel = !user.TwoFactorEnabled
|
|
|
|
? null
|
|
|
|
: new LoginWith2faViewModel()
|
|
|
|
{
|
|
|
|
RememberMe = viewModel.RememberMe
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/login/2fa")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
public async Task<IActionResult> LoginWith2fa(bool rememberMe, string returnUrl = null)
|
|
|
|
{
|
2019-11-14 19:01:26 +01:00
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
// Ensure the user has gone through the username & password screen first
|
|
|
|
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException($"Unable to load two-factor authentication user.");
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
|
|
|
2019-05-02 14:01:08 +02:00
|
|
|
return View("SecondaryLogin", new SecondaryLoginViewModel()
|
|
|
|
{
|
|
|
|
LoginWith2FaViewModel = new LoginWith2faViewModel { RememberMe = rememberMe },
|
2021-04-20 07:06:32 +02:00
|
|
|
LoginWithFido2ViewModel = (await _fido2Service.HasCredentials(user.Id)) ? await BuildFido2ViewModel(rememberMe, user) : null,
|
2021-11-11 13:03:08 +01:00
|
|
|
LoginWithLNURLAuthViewModel = (await _lnurlAuthService.HasCredentials(user.Id)) ? await BuildLNURLAuthViewModel(rememberMe, user) : null,
|
2019-05-02 14:01:08 +02:00
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpPost("/login/2fa")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
public async Task<IActionResult> LoginWith2fa(LoginWith2faViewModel model, bool rememberMe, string returnUrl = null)
|
|
|
|
{
|
2019-11-14 19:01:26 +01:00
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
|
|
|
}
|
|
|
|
|
2018-02-17 05:18:16 +01:00
|
|
|
var authenticatorCode = model.TwoFactorCode.Replace(" ", string.Empty, StringComparison.InvariantCulture).Replace("-", string.Empty, StringComparison.InvariantCulture);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
var result = await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, rememberMe, model.RememberMachine);
|
|
|
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
{
|
|
|
|
_logger.LogInformation("User with ID {UserId} logged in with 2fa.", user.Id);
|
2022-05-24 06:18:16 +02:00
|
|
|
return RedirectToLocal(returnUrl);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
else if (result.IsLockedOut)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("User with ID {UserId} account locked out.", user.Id);
|
2023-01-06 14:18:07 +01:00
|
|
|
return RedirectToAction(nameof(Lockout), new { user.LockoutEnd });
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Invalid authenticator code entered for user with ID {UserId}.", user.Id);
|
|
|
|
ModelState.AddModelError(string.Empty, "Invalid authenticator code.");
|
2019-05-02 14:01:08 +02:00
|
|
|
return View("SecondaryLogin", new SecondaryLoginViewModel()
|
|
|
|
{
|
|
|
|
LoginWith2FaViewModel = model,
|
2021-04-20 07:06:32 +02:00
|
|
|
LoginWithFido2ViewModel = (await _fido2Service.HasCredentials(user.Id)) ? await BuildFido2ViewModel(rememberMe, user) : null,
|
2021-11-11 13:03:08 +01:00
|
|
|
LoginWithLNURLAuthViewModel = (await _lnurlAuthService.HasCredentials(user.Id)) ? await BuildLNURLAuthViewModel(rememberMe, user) : null,
|
2019-05-02 14:01:08 +02:00
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/login/recovery-code")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
public async Task<IActionResult> LoginWithRecoveryCode(string returnUrl = null)
|
|
|
|
{
|
2019-11-14 19:01:26 +01:00
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
// Ensure the user has gone through the username & password screen first
|
|
|
|
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException($"Unable to load two-factor authentication user.");
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
|
|
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpPost("/login/recovery-code")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
public async Task<IActionResult> LoginWithRecoveryCode(LoginWithRecoveryCodeViewModel model, string returnUrl = null)
|
|
|
|
{
|
2019-11-14 19:01:26 +01:00
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException($"Unable to load two-factor authentication user.");
|
|
|
|
}
|
|
|
|
|
2018-02-17 05:18:16 +01:00
|
|
|
var recoveryCode = model.RecoveryCode.Replace(" ", string.Empty, StringComparison.InvariantCulture);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode);
|
|
|
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
{
|
|
|
|
_logger.LogInformation("User with ID {UserId} logged in with a recovery code.", user.Id);
|
2022-05-24 06:18:16 +02:00
|
|
|
return RedirectToLocal(returnUrl);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
if (result.IsLockedOut)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("User with ID {UserId} account locked out.", user.Id);
|
2023-01-06 14:18:07 +01:00
|
|
|
|
|
|
|
return RedirectToAction(nameof(Lockout), new { user.LockoutEnd });
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Invalid recovery code entered for user with ID {UserId}", user.Id);
|
|
|
|
ModelState.AddModelError(string.Empty, "Invalid recovery code entered.");
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/login/lockout")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
2022-04-26 14:27:35 +02:00
|
|
|
public IActionResult Lockout(DateTimeOffset? lockoutEnd)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2022-04-26 14:27:35 +02:00
|
|
|
return View(lockoutEnd);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/register")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
2020-03-18 15:10:15 +01:00
|
|
|
[RateLimitsFilter(ZoneLimits.Register, Scope = RateLimitsScope.RemoteAddress)]
|
2022-05-24 06:18:16 +02:00
|
|
|
public IActionResult Register(string returnUrl = null, bool logon = true)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-11-16 08:57:16 +01:00
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
SetInsecureFlags();
|
|
|
|
}
|
2022-05-24 06:18:16 +02:00
|
|
|
if (PoliciesSettings.LockSubscription && !User.IsInRole(Roles.ServerAdmin))
|
2022-01-07 04:32:00 +01:00
|
|
|
return RedirectToAction(nameof(UIHomeController.Index), "UIHome");
|
2017-10-27 10:53:04 +02:00
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpPost("/register")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
2018-08-01 17:16:16 +02:00
|
|
|
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null, bool logon = true)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-11-14 19:01:26 +01:00
|
|
|
if (!CanLoginOrRegister())
|
|
|
|
{
|
|
|
|
return RedirectToAction("Register");
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
2018-08-01 17:16:16 +02:00
|
|
|
ViewData["Logon"] = logon.ToString(CultureInfo.InvariantCulture).ToLowerInvariant();
|
2017-12-03 16:55:39 +01:00
|
|
|
var policies = await _SettingsRepository.GetSettingAsync<PoliciesSettings>() ?? new PoliciesSettings();
|
2018-12-20 20:39:48 +01:00
|
|
|
if (policies.LockSubscription && !User.IsInRole(Roles.ServerAdmin))
|
2022-01-07 04:32:00 +01:00
|
|
|
return RedirectToAction(nameof(UIHomeController.Index), "UIHome");
|
2017-10-27 10:53:04 +02:00
|
|
|
if (ModelState.IsValid)
|
|
|
|
{
|
2021-12-31 08:59:02 +01:00
|
|
|
var user = new ApplicationUser
|
|
|
|
{
|
|
|
|
UserName = model.Email,
|
|
|
|
Email = model.Email,
|
|
|
|
RequiresEmailConfirmation = policies.RequiresConfirmedEmail,
|
|
|
|
Created = DateTimeOffset.UtcNow
|
|
|
|
};
|
2017-10-27 10:53:04 +02:00
|
|
|
var result = await _userManager.CreateAsync(user, model.Password);
|
|
|
|
if (result.Succeeded)
|
|
|
|
{
|
|
|
|
var admin = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
2021-10-11 05:32:09 +02:00
|
|
|
if (admin.Count == 0 || (model.IsAdmin && _Options.CheatMode))
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
await _RoleManager.CreateAsync(new IdentityRole(Roles.ServerAdmin));
|
|
|
|
await _userManager.AddToRoleAsync(user, Roles.ServerAdmin);
|
2019-11-06 06:31:45 +01:00
|
|
|
var settings = await _SettingsRepository.GetSettingAsync<ThemeSettings>();
|
|
|
|
settings.FirstRun = false;
|
|
|
|
await _SettingsRepository.UpdateSetting<ThemeSettings>(settings);
|
2020-08-01 16:17:17 +02:00
|
|
|
|
2021-11-22 09:16:08 +01:00
|
|
|
await _SettingsRepository.FirstAdminRegistered(policies, _Options.UpdateUrl != null, _Options.DisableRegistration, Logs);
|
2020-02-29 06:15:06 +01:00
|
|
|
RegisteredAdmin = true;
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
2020-03-13 11:47:22 +01:00
|
|
|
_eventAggregator.Publish(new UserRegisteredEvent()
|
|
|
|
{
|
2020-04-10 08:59:39 +02:00
|
|
|
RequestUri = Request.GetAbsoluteRootUri(),
|
2020-03-13 11:47:22 +01:00
|
|
|
User = user,
|
|
|
|
Admin = RegisteredAdmin
|
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
RegisteredUserId = user.Id;
|
2019-01-06 15:53:37 +01:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
if (!policies.RequiresConfirmedEmail)
|
|
|
|
{
|
2020-02-29 06:15:14 +01:00
|
|
|
if (logon)
|
2018-08-01 17:16:16 +02:00
|
|
|
await _signInManager.SignInAsync(user, isPersistent: false);
|
2022-05-24 06:18:16 +02:00
|
|
|
return RedirectToLocal(returnUrl);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-31 07:19:38 +01:00
|
|
|
TempData[WellKnownTempData.SuccessMessage] = "Account created, please confirm your email";
|
2017-10-27 10:53:04 +02:00
|
|
|
return View();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AddErrors(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, something failed, redisplay form
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:15:06 +01:00
|
|
|
// Properties used by tests
|
|
|
|
public string RegisteredUserId { get; set; }
|
|
|
|
public bool RegisteredAdmin { get; set; }
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/logout")]
|
2017-10-27 10:53:04 +02:00
|
|
|
public async Task<IActionResult> Logout()
|
|
|
|
{
|
|
|
|
await _signInManager.SignOutAsync();
|
2022-02-01 02:42:31 +01:00
|
|
|
HttpContext.DeleteUserPrefsCookie();
|
2017-10-27 10:53:04 +02:00
|
|
|
_logger.LogInformation("User logged out.");
|
2022-02-07 13:18:22 +01:00
|
|
|
return RedirectToAction(nameof(UIAccountController.Login));
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/register/confirm-email")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
public async Task<IActionResult> ConfirmEmail(string userId, string code)
|
|
|
|
{
|
|
|
|
if (userId == null || code == null)
|
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
return RedirectToAction(nameof(UIHomeController.Index), "UIHome");
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
var user = await _userManager.FindByIdAsync(userId);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException($"Unable to load user with ID '{userId}'.");
|
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
var result = await _userManager.ConfirmEmailAsync(user, code);
|
2020-09-05 12:16:48 +02:00
|
|
|
if (!await _userManager.HasPasswordAsync(user))
|
|
|
|
{
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2020-09-05 12:16:48 +02:00
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
|
|
|
Severity = StatusMessageModel.StatusSeverity.Info,
|
|
|
|
Message = "Your email has been confirmed but you still need to set your password."
|
|
|
|
});
|
2021-12-31 08:59:02 +01:00
|
|
|
return RedirectToAction("SetPassword", new { email = user.Email, code = await _userManager.GeneratePasswordResetTokenAsync(user) });
|
2020-09-05 12:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
{
|
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
|
|
|
Severity = StatusMessageModel.StatusSeverity.Success,
|
|
|
|
Message = "Your email has been confirmed."
|
|
|
|
});
|
2021-12-31 08:59:02 +01:00
|
|
|
return RedirectToAction("Login", new { email = user.Email });
|
2020-09-05 12:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return View("Error");
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/login/forgot-password")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
public IActionResult ForgotPassword()
|
|
|
|
{
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpPost("/login/forgot-password")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
2021-03-28 13:56:46 +02:00
|
|
|
[RateLimitsFilter(ZoneLimits.ForgotPassword, Scope = RateLimitsScope.RemoteAddress)]
|
2017-10-27 10:53:04 +02:00
|
|
|
public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
|
|
|
|
{
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
{
|
|
|
|
var user = await _userManager.FindByEmailAsync(model.Email);
|
2020-10-17 09:25:48 +02:00
|
|
|
if (user == null || (user.RequiresEmailConfirmation && !(await _userManager.IsEmailConfirmedAsync(user))))
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
// Don't reveal that the user does not exist or is not confirmed
|
|
|
|
return RedirectToAction(nameof(ForgotPasswordConfirmation));
|
|
|
|
}
|
2020-09-05 12:16:48 +02:00
|
|
|
_eventAggregator.Publish(new UserPasswordResetRequestedEvent()
|
|
|
|
{
|
2021-12-31 08:59:02 +01:00
|
|
|
User = user,
|
|
|
|
RequestUri = Request.GetAbsoluteRootUri()
|
2020-09-05 12:16:48 +02:00
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
return RedirectToAction(nameof(ForgotPasswordConfirmation));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, something failed, redisplay form
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/login/forgot-password/confirm")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
public IActionResult ForgotPasswordConfirmation()
|
|
|
|
{
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpGet("/login/set-password")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
2020-09-05 12:16:48 +02:00
|
|
|
public async Task<IActionResult> SetPassword(string code = null, string userId = null, string email = null)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
if (code == null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException("A code must be supplied for password reset.");
|
|
|
|
}
|
2020-09-05 12:16:48 +02:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(userId))
|
|
|
|
{
|
|
|
|
var user = await _userManager.FindByIdAsync(userId);
|
|
|
|
email = user?.Email;
|
|
|
|
}
|
|
|
|
|
2021-12-31 08:59:02 +01:00
|
|
|
var model = new SetPasswordViewModel { Code = code, Email = email, EmailSetInternally = !string.IsNullOrEmpty(email) };
|
2017-10-27 10:53:04 +02:00
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:20:50 +01:00
|
|
|
[HttpPost("/login/set-password")]
|
2017-10-27 10:53:04 +02:00
|
|
|
[AllowAnonymous]
|
|
|
|
[ValidateAntiForgeryToken]
|
2020-09-05 12:16:48 +02:00
|
|
|
public async Task<IActionResult> SetPassword(SetPasswordViewModel model)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
var user = await _userManager.FindByEmailAsync(model.Email);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
// Don't reveal that the user does not exist
|
2020-09-05 12:16:48 +02:00
|
|
|
return RedirectToAction(nameof(Login));
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2020-09-05 12:16:48 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);
|
|
|
|
if (result.Succeeded)
|
|
|
|
{
|
2020-09-05 12:16:48 +02:00
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
2021-12-31 08:59:02 +01:00
|
|
|
Severity = StatusMessageModel.StatusSeverity.Success,
|
|
|
|
Message = "Password successfully set."
|
2020-09-05 12:16:48 +02:00
|
|
|
});
|
|
|
|
return RedirectToAction(nameof(Login));
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 12:16:48 +02:00
|
|
|
AddErrors(result);
|
2020-10-15 08:36:42 +02:00
|
|
|
return View(model);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#region Helpers
|
|
|
|
|
|
|
|
private void AddErrors(IdentityResult result)
|
|
|
|
{
|
|
|
|
foreach (var error in result.Errors)
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(string.Empty, error.Description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 06:18:16 +02:00
|
|
|
private IActionResult RedirectToLocal(string returnUrl = null)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-07-14 15:16:23 +02:00
|
|
|
if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
return Redirect(returnUrl);
|
|
|
|
}
|
2023-02-02 12:53:42 +01:00
|
|
|
|
|
|
|
// After login, if there is an app on "/", we should redirect to BTCPay explicit home route, and not to the app.
|
|
|
|
if (PoliciesSettings.RootAppId is not null && PoliciesSettings.RootAppType is not null)
|
|
|
|
return RedirectToAction(nameof(UIHomeController.Home), "UIHome");
|
|
|
|
|
|
|
|
if (PoliciesSettings.DomainToAppMapping is { } mapping)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2023-02-02 12:53:42 +01:00
|
|
|
var matchedDomainMapping = mapping.FirstOrDefault(item =>
|
|
|
|
item.Domain.Equals(HttpContext.Request.Host.Host, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
if (matchedDomainMapping is not null)
|
2022-02-07 13:18:22 +01:00
|
|
|
return RedirectToAction(nameof(UIHomeController.Home), "UIHome");
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2023-04-10 04:07:03 +02:00
|
|
|
|
2023-02-02 12:53:42 +01:00
|
|
|
return RedirectToAction(nameof(UIHomeController.Index), "UIHome");
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2020-02-29 06:15:14 +01:00
|
|
|
|
2019-11-14 19:01:26 +01:00
|
|
|
private bool CanLoginOrRegister()
|
|
|
|
{
|
2022-11-21 19:32:19 +01:00
|
|
|
return _btcPayServerEnvironment.IsDeveloping || _btcPayServerEnvironment.IsSecure(HttpContext);
|
2019-11-16 08:57:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void SetInsecureFlags()
|
|
|
|
{
|
2019-11-14 19:01:26 +01:00
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
|
|
|
Severity = StatusMessageModel.StatusSeverity.Error,
|
|
|
|
Message = "You cannot login over an insecure connection. Please use HTTPS or Tor."
|
|
|
|
});
|
2020-02-29 06:15:14 +01:00
|
|
|
|
2019-11-14 19:01:26 +01:00
|
|
|
ViewData["disabled"] = true;
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|