1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-01 23:31:41 +01:00

Update API endpoint to use RegisterResponseModel (#2282)

This commit is contained in:
Addison Beck 2022-09-19 09:35:57 -04:00 committed by GitHub
parent 26fc67eec6
commit d0c793c951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 17 deletions

View File

@ -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<PreloginResponseModel> 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<RegisterResponseModel> 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"))

View File

@ -0,0 +1,6 @@
namespace Bit.Core.Models.Api.Response.Accounts;
public interface ICaptchaProtectedResponseModel
{
public string CaptchaBypassToken { get; set; }
}

View File

@ -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
{

View File

@ -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<RegisterResponseModel> 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<PreloginResponseModel> PostPrelogin([FromBody] PreloginRequestModel model)
{

View File

@ -1,4 +0,0 @@
public interface ICaptchaProtectedResponseModel
{
public string CaptchaBypassToken { get; set; }
}

View File

@ -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<ISendRepository>();
_sendService = Substitute.For<ISendService>();
_captchaValidationService = Substitute.For<ICaptchaValidationService>();
_sut = new AccountsController(
_globalSettings,
_cipherRepository,
@ -55,7 +57,8 @@ public class AccountsControllerTests : IDisposable
_userRepository,
_userService,
_sendRepository,
_sendService
_sendService,
_captchaValidationService
);
}