diff --git a/src/Identity/Controllers/AccountsController.cs b/src/Identity/Controllers/AccountsController.cs index 940e2ab97e..e816a18f4e 100644 --- a/src/Identity/Controllers/AccountsController.cs +++ b/src/Identity/Controllers/AccountsController.cs @@ -6,6 +6,7 @@ using Bit.Core.Models.Data; using Bit.Core.Repositories; using Bit.Core.Services; using Bit.Core.Utilities; +using Bit.Identity.Models; using Bit.SharedWeb.Utilities; using Microsoft.AspNetCore.Mvc; @@ -18,27 +19,32 @@ public class AccountsController : Controller private readonly ILogger _logger; private readonly IUserRepository _userRepository; private readonly IUserService _userService; + private readonly ICaptchaValidationService _captchaValidationService; public AccountsController( ILogger logger, IUserRepository userRepository, - IUserService userService) + IUserService userService, + ICaptchaValidationService captchaValidationService) { _logger = logger; _userRepository = userRepository; _userService = userService; + _captchaValidationService = captchaValidationService; } // Moved from API, If you modify this endpoint, please update API as well. [HttpPost("register")] [CaptchaProtected] - public async Task PostRegister([FromBody] RegisterRequestModel model) + public async Task PostRegister([FromBody] RegisterRequestModel model) { - var result = await _userService.RegisterUserAsync(model.ToUser(), model.MasterPasswordHash, + var user = model.ToUser(); + var result = await _userService.RegisterUserAsync(user, model.MasterPasswordHash, model.Token, model.OrganizationUserId); if (result.Succeeded) { - return; + var captchaBypassToken = _captchaValidationService.GenerateCaptchaBypassToken(user); + return new RegisterResponseModel(captchaBypassToken); } foreach (var error in result.Errors.Where(e => e.Code != "DuplicateUserName")) diff --git a/src/Identity/Models/ICaptchaProtectedResponseModel.cs b/src/Identity/Models/ICaptchaProtectedResponseModel.cs new file mode 100644 index 0000000000..9783c7cb41 --- /dev/null +++ b/src/Identity/Models/ICaptchaProtectedResponseModel.cs @@ -0,0 +1,4 @@ +public interface ICaptchaProtectedResponseModel +{ + public string CaptchaBypassToken { get; set; } +} diff --git a/src/Identity/Models/RegisterResponseModel.cs b/src/Identity/Models/RegisterResponseModel.cs new file mode 100644 index 0000000000..ca967e086e --- /dev/null +++ b/src/Identity/Models/RegisterResponseModel.cs @@ -0,0 +1,14 @@ +using Bit.Core.Models.Api; + +namespace Bit.Identity.Models; + +public class RegisterResponseModel : ResponseModel, ICaptchaProtectedResponseModel +{ + public RegisterResponseModel(string captchaBypassToken) + : base("register") + { + CaptchaBypassToken = captchaBypassToken; + } + + public string CaptchaBypassToken { get; set; } +} diff --git a/test/Identity.Test/Controllers/AccountsControllerTests.cs b/test/Identity.Test/Controllers/AccountsControllerTests.cs index 54b5856547..5318a55cd2 100644 --- a/test/Identity.Test/Controllers/AccountsControllerTests.cs +++ b/test/Identity.Test/Controllers/AccountsControllerTests.cs @@ -20,16 +20,19 @@ public class AccountsControllerTests : IDisposable private readonly ILogger _logger; private readonly IUserRepository _userRepository; private readonly IUserService _userService; + private readonly ICaptchaValidationService _captchaValidationService; public AccountsControllerTests() { _logger = Substitute.For>(); _userRepository = Substitute.For(); _userService = Substitute.For(); + _captchaValidationService = Substitute.For(); _sut = new AccountsController( _logger, _userRepository, - _userService + _userService, + _captchaValidationService ); }