2023-10-30 08:40:06 -05:00
|
|
|
|
using Bit.Core;
|
|
|
|
|
|
using Bit.Core.Auth.Models.Api.Request.Accounts;
|
2023-04-14 13:25:56 -04:00
|
|
|
|
using Bit.Core.Auth.Models.Api.Response.Accounts;
|
|
|
|
|
|
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-10-30 08:40:06 -05:00
|
|
|
|
using Bit.Core.Utilities;
|
2022-03-14 20:08:01 -05:00
|
|
|
|
using Bit.SharedWeb.Utilities;
|
2023-10-30 08:40:06 -05:00
|
|
|
|
using Fido2NetLib;
|
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;
|
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,
|
|
|
|
|
|
ICaptchaValidationService captchaValidationService)
|
2022-01-17 13:21:51 +01:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_userRepository = userRepository;
|
|
|
|
|
|
_userService = userService;
|
2022-09-15 10:02:37 -04:00
|
|
|
|
_captchaValidationService = captchaValidationService;
|
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,
|
|
|
|
|
|
KdfIterations = 100000,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
return new PreloginResponseModel(kdfInformation);
|
|
|
|
|
|
}
|
2023-10-30 08:40:06 -05:00
|
|
|
|
|
|
|
|
|
|
[HttpPost("webauthn-assertion-options")]
|
|
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)] // Disable Swagger due to CredentialCreateOptions not converting properly
|
|
|
|
|
|
[RequireFeature(FeatureFlagKeys.PasswordlessLogin)]
|
|
|
|
|
|
// TODO: Create proper models for this call
|
|
|
|
|
|
public async Task<AssertionOptions> PostWebAuthnAssertionOptions([FromBody] PreloginRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await _userRepository.GetByEmailAsync(model.Email);
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO: return something? possible enumeration attacks with this response
|
|
|
|
|
|
return new AssertionOptions();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var options = await _userService.StartWebAuthnLoginAssertionAsync(user);
|
|
|
|
|
|
return options;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("webauthn-assertion")]
|
|
|
|
|
|
[RequireFeature(FeatureFlagKeys.PasswordlessLogin)]
|
|
|
|
|
|
// TODO: Create proper models for this call
|
|
|
|
|
|
public async Task<string> PostWebAuthnAssertion([FromBody] PreloginRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await _userRepository.GetByEmailAsync(model.Email);
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO: proper response here?
|
|
|
|
|
|
throw new BadRequestException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var token = await _userService.CompleteWebAuthLoginAssertionAsync(null, user);
|
|
|
|
|
|
return token;
|
|
|
|
|
|
}
|
2022-01-17 13:21:51 +01:00
|
|
|
|
}
|