Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SG-656] Send a captcha bypass token back from the register endpoint #2278

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Identity/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Bit.Identity.Models;
using Bit.SharedWeb.Utilities;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -18,27 +19,32 @@ public class AccountsController : Controller
private readonly ILogger<AccountsController> _logger;
private readonly IUserRepository _userRepository;
private readonly IUserService _userService;
private readonly ICaptchaValidationService _captchaValidationService;

public AccountsController(
ILogger<AccountsController> 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<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"))
Expand Down
4 changes: 4 additions & 0 deletions src/Identity/Models/ICaptchaProtectedResponseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface ICaptchaProtectedResponseModel
{
public string CaptchaBypassToken { get; set; }
}
14 changes: 14 additions & 0 deletions src/Identity/Models/RegisterResponseModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
5 changes: 4 additions & 1 deletion test/Identity.Test/Controllers/AccountsControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ public class AccountsControllerTests : IDisposable
private readonly ILogger<AccountsController> _logger;
private readonly IUserRepository _userRepository;
private readonly IUserService _userService;
private readonly ICaptchaValidationService _captchaValidationService;

public AccountsControllerTests()
{
_logger = Substitute.For<ILogger<AccountsController>>();
_userRepository = Substitute.For<IUserRepository>();
_userService = Substitute.For<IUserService>();
_captchaValidationService = Substitute.For<ICaptchaValidationService>();
_sut = new AccountsController(
_logger,
_userRepository,
_userService
_userService,
_captchaValidationService
);
}

Expand Down