From d0c793c95181dfb1b447eb450f85ba0bfd7ef643 Mon Sep 17 00:00:00 2001 From: Addison Beck Date: Mon, 19 Sep 2022 09:35:57 -0400 Subject: [PATCH] Update API endpoint to use RegisterResponseModel (#2282) --- src/Api/Controllers/AccountsController.cs | 19 +++++++++++++------ .../ICaptchaProtectedResponseModel.cs | 6 ++++++ .../Accounts}/RegisterResponseModel.cs | 4 +--- .../Controllers/AccountsController.cs | 5 ++--- .../Models/ICaptchaProtectedResponseModel.cs | 4 ---- .../Controllers/AccountsControllerTests.cs | 5 ++++- 6 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 src/Core/Models/Api/Response/Accounts/ICaptchaProtectedResponseModel.cs rename src/{Identity/Models => Core/Models/Api/Response/Accounts}/RegisterResponseModel.cs (82%) delete mode 100644 src/Identity/Models/ICaptchaProtectedResponseModel.cs diff --git a/src/Api/Controllers/AccountsController.cs b/src/Api/Controllers/AccountsController.cs index 5d7e6511a9..f80afc8f33 100644 --- a/src/Api/Controllers/AccountsController.cs +++ b/src/Api/Controllers/AccountsController.cs @@ -35,6 +35,7 @@ public class AccountsController : Controller private readonly IUserService _userService; private readonly ISendRepository _sendRepository; private readonly ISendService _sendService; + private readonly ICaptchaValidationService _captchaValidationService; public AccountsController( GlobalSettings globalSettings, @@ -47,7 +48,8 @@ public class AccountsController : Controller IUserRepository userRepository, IUserService userService, ISendRepository sendRepository, - ISendService sendService) + ISendService sendService, + ICaptchaValidationService captchaValidationService) { _cipherRepository = cipherRepository; _folderRepository = folderRepository; @@ -60,11 +62,13 @@ public class AccountsController : Controller _userService = userService; _sendRepository = sendRepository; _sendService = sendService; + _captchaValidationService = captchaValidationService; } #region DEPRECATED (Moved to Identity Service) - [Obsolete("2022-01-12 Moved to Identity, left for backwards compatability with older clients")] + // This method is still used by self hosted intalls + [Obsolete("2022-01-12 Moved to Identity, left for backwards compatability with older clients.")] [HttpPost("prelogin")] [AllowAnonymous] public async Task PostPrelogin([FromBody] PreloginRequestModel model) @@ -81,17 +85,20 @@ public class AccountsController : Controller return new PreloginResponseModel(kdfInformation); } - [Obsolete("2022-01-12 Moved to Identity, left for backwards compatability with older clients")] + // This method is still used by self hosted intalls + [Obsolete("2022-01-12 Moved to Identity, left for backwards compatability with older clients.")] [HttpPost("register")] [AllowAnonymous] [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/Core/Models/Api/Response/Accounts/ICaptchaProtectedResponseModel.cs b/src/Core/Models/Api/Response/Accounts/ICaptchaProtectedResponseModel.cs new file mode 100644 index 0000000000..863480b27c --- /dev/null +++ b/src/Core/Models/Api/Response/Accounts/ICaptchaProtectedResponseModel.cs @@ -0,0 +1,6 @@ +namespace Bit.Core.Models.Api.Response.Accounts; + +public interface ICaptchaProtectedResponseModel +{ + public string CaptchaBypassToken { get; set; } +} diff --git a/src/Identity/Models/RegisterResponseModel.cs b/src/Core/Models/Api/Response/Accounts/RegisterResponseModel.cs similarity index 82% rename from src/Identity/Models/RegisterResponseModel.cs rename to src/Core/Models/Api/Response/Accounts/RegisterResponseModel.cs index ca967e086e..857fc8a875 100644 --- a/src/Identity/Models/RegisterResponseModel.cs +++ b/src/Core/Models/Api/Response/Accounts/RegisterResponseModel.cs @@ -1,6 +1,4 @@ -using Bit.Core.Models.Api; - -namespace Bit.Identity.Models; +namespace Bit.Core.Models.Api.Response.Accounts; public class RegisterResponseModel : ResponseModel, ICaptchaProtectedResponseModel { diff --git a/src/Identity/Controllers/AccountsController.cs b/src/Identity/Controllers/AccountsController.cs index e816a18f4e..e5c3c5eadc 100644 --- a/src/Identity/Controllers/AccountsController.cs +++ b/src/Identity/Controllers/AccountsController.cs @@ -6,7 +6,6 @@ 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; @@ -33,7 +32,7 @@ public class AccountsController : Controller _captchaValidationService = captchaValidationService; } - // Moved from API, If you modify this endpoint, please update API as well. + // Moved from API, If you modify this endpoint, please update API as well. Self hosted installs still use the API endpoints. [HttpPost("register")] [CaptchaProtected] public async Task PostRegister([FromBody] RegisterRequestModel model) @@ -56,7 +55,7 @@ public class AccountsController : Controller throw new BadRequestException(ModelState); } - // Moved from API, If you modify this endpoint, please update API as well. + // Moved from API, If you modify this endpoint, please update API as well. Self hosted installs still use the API endpoints. [HttpPost("prelogin")] public async Task PostPrelogin([FromBody] PreloginRequestModel model) { diff --git a/src/Identity/Models/ICaptchaProtectedResponseModel.cs b/src/Identity/Models/ICaptchaProtectedResponseModel.cs deleted file mode 100644 index 9783c7cb41..0000000000 --- a/src/Identity/Models/ICaptchaProtectedResponseModel.cs +++ /dev/null @@ -1,4 +0,0 @@ -public interface ICaptchaProtectedResponseModel -{ - public string CaptchaBypassToken { get; set; } -} diff --git a/test/Api.Test/Controllers/AccountsControllerTests.cs b/test/Api.Test/Controllers/AccountsControllerTests.cs index 0b2747c386..142cc0b4d9 100644 --- a/test/Api.Test/Controllers/AccountsControllerTests.cs +++ b/test/Api.Test/Controllers/AccountsControllerTests.cs @@ -30,6 +30,7 @@ public class AccountsControllerTests : IDisposable private readonly ISendRepository _sendRepository; private readonly ISendService _sendService; private readonly IProviderUserRepository _providerUserRepository; + private readonly ICaptchaValidationService _captchaValidationService; public AccountsControllerTests() { @@ -44,6 +45,7 @@ public class AccountsControllerTests : IDisposable _globalSettings = new GlobalSettings(); _sendRepository = Substitute.For(); _sendService = Substitute.For(); + _captchaValidationService = Substitute.For(); _sut = new AccountsController( _globalSettings, _cipherRepository, @@ -55,7 +57,8 @@ public class AccountsControllerTests : IDisposable _userRepository, _userService, _sendRepository, - _sendService + _sendService, + _captchaValidationService ); }