2023-10-30 08:40:06 -05:00
|
|
|
|
using Bit.Core;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
using Bit.Core.Auth.Enums;
|
2023-10-30 08:40:06 -05:00
|
|
|
|
using Bit.Core.Auth.Models.Api.Request.Accounts;
|
2023-04-14 13:25:56 -04:00
|
|
|
|
using Bit.Core.Auth.Models.Api.Response.Accounts;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
using Bit.Core.Auth.Models.Business.Tokenables;
|
2023-04-14 13:25:56 -04:00
|
|
|
|
using Bit.Core.Auth.Services;
|
|
|
|
|
|
using Bit.Core.Auth.Utilities;
|
|
|
|
|
|
using Bit.Core.Enums;
|
2022-01-17 13:21:51 +01:00
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Models.Data;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
using Bit.Core.Tokens;
|
2023-10-30 08:40:06 -05:00
|
|
|
|
using Bit.Core.Utilities;
|
2022-03-14 20:08:01 -05:00
|
|
|
|
using Bit.SharedWeb.Utilities;
|
2022-01-17 13:21:51 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Identity.Controllers;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
[Route("accounts")]
|
|
|
|
|
|
[ExceptionHandlerFilter]
|
|
|
|
|
|
public class AccountsController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<AccountsController> _logger;
|
|
|
|
|
|
private readonly IUserRepository _userRepository;
|
|
|
|
|
|
private readonly IUserService _userService;
|
2022-09-15 10:02:37 -04:00
|
|
|
|
private readonly ICaptchaValidationService _captchaValidationService;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
private readonly IDataProtectorTokenFactory<WebAuthnLoginAssertionOptionsTokenable> _assertionOptionsDataProtector;
|
|
|
|
|
|
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
public AccountsController(
|
|
|
|
|
|
ILogger<AccountsController> logger,
|
|
|
|
|
|
IUserRepository userRepository,
|
2022-09-15 10:02:37 -04:00
|
|
|
|
IUserService userService,
|
2023-11-20 15:55:31 +01:00
|
|
|
|
ICaptchaValidationService captchaValidationService,
|
|
|
|
|
|
IDataProtectorTokenFactory<WebAuthnLoginAssertionOptionsTokenable> assertionOptionsDataProtector)
|
2022-01-17 13:21:51 +01:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_userRepository = userRepository;
|
|
|
|
|
|
_userService = userService;
|
2022-09-15 10:02:37 -04:00
|
|
|
|
_captchaValidationService = captchaValidationService;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
_assertionOptionsDataProtector = assertionOptionsDataProtector;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2022-01-17 13:21:51 +01:00
|
|
|
|
|
2022-09-19 09:35:57 -04:00
|
|
|
|
// Moved from API, If you modify this endpoint, please update API as well. Self hosted installs still use the API endpoints.
|
2022-01-17 13:21:51 +01:00
|
|
|
|
[HttpPost("register")]
|
|
|
|
|
|
[CaptchaProtected]
|
2022-09-15 10:02:37 -04:00
|
|
|
|
public async Task<RegisterResponseModel> PostRegister([FromBody] RegisterRequestModel model)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-09-15 10:02:37 -04:00
|
|
|
|
var user = model.ToUser();
|
|
|
|
|
|
var result = await _userService.RegisterUserAsync(user, model.MasterPasswordHash,
|
2022-01-17 13:21:51 +01:00
|
|
|
|
model.Token, model.OrganizationUserId);
|
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
|
{
|
2022-09-15 10:02:37 -04:00
|
|
|
|
var captchaBypassToken = _captchaValidationService.GenerateCaptchaBypassToken(user);
|
|
|
|
|
|
return new RegisterResponseModel(captchaBypassToken);
|
2022-01-17 13:21:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var error in result.Errors.Where(e => e.Code != "DuplicateUserName"))
|
|
|
|
|
|
{
|
|
|
|
|
|
ModelState.AddModelError(string.Empty, error.Description);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
await Task.Delay(2000);
|
|
|
|
|
|
throw new BadRequestException(ModelState);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-19 09:35:57 -04:00
|
|
|
|
// Moved from API, If you modify this endpoint, please update API as well. Self hosted installs still use the API endpoints.
|
2022-01-17 13:21:51 +01:00
|
|
|
|
[HttpPost("prelogin")]
|
|
|
|
|
|
public async Task<PreloginResponseModel> PostPrelogin([FromBody] PreloginRequestModel model)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-01-17 13:21:51 +01:00
|
|
|
|
var kdfInformation = await _userRepository.GetKdfInformationByEmailAsync(model.Email);
|
|
|
|
|
|
if (kdfInformation == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
kdfInformation = new UserKdfInformation
|
|
|
|
|
|
{
|
|
|
|
|
|
Kdf = KdfType.PBKDF2_SHA256,
|
2023-12-05 17:21:46 +01:00
|
|
|
|
KdfIterations = AuthConstants.PBKDF2_ITERATIONS.Default,
|
2022-01-17 13:21:51 +01:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
return new PreloginResponseModel(kdfInformation);
|
|
|
|
|
|
}
|
2023-10-30 08:40:06 -05:00
|
|
|
|
|
2023-11-22 19:24:19 +01:00
|
|
|
|
[HttpGet("webauthn/assertion-options")]
|
2023-10-30 08:40:06 -05:00
|
|
|
|
[RequireFeature(FeatureFlagKeys.PasswordlessLogin)]
|
2023-11-22 19:24:19 +01:00
|
|
|
|
public WebAuthnLoginAssertionOptionsResponseModel GetWebAuthnLoginAssertionOptions()
|
2023-10-30 08:40:06 -05:00
|
|
|
|
{
|
2023-11-20 15:55:31 +01:00
|
|
|
|
var options = _userService.StartWebAuthnLoginAssertion();
|
2023-10-30 08:40:06 -05:00
|
|
|
|
|
2023-11-20 15:55:31 +01:00
|
|
|
|
var tokenable = new WebAuthnLoginAssertionOptionsTokenable(WebAuthnLoginAssertionOptionsScope.Authentication, options);
|
|
|
|
|
|
var token = _assertionOptionsDataProtector.Protect(tokenable);
|
2023-10-30 08:40:06 -05:00
|
|
|
|
|
2023-11-20 15:55:31 +01:00
|
|
|
|
return new WebAuthnLoginAssertionOptionsResponseModel
|
2023-10-30 08:40:06 -05:00
|
|
|
|
{
|
2023-11-20 15:55:31 +01:00
|
|
|
|
Options = options,
|
|
|
|
|
|
Token = token
|
|
|
|
|
|
};
|
2023-10-30 08:40:06 -05:00
|
|
|
|
}
|
2022-01-17 13:21:51 +01:00
|
|
|
|
}
|