mirror of
https://github.com/bitwarden/server.git
synced 2024-11-28 13:15:12 +01:00
80740aa4ba
* [PM-2032] feat: add assertion options tokenable * [PM-2032] feat: add request and response models * [PM-2032] feat: implement `assertion-options` identity endpoint * [PM-2032] feat: implement authentication with passkey * [PM-2032] chore: rename to `WebAuthnGrantValidator` * [PM-2032] fix: add missing subsitute * [PM-2032] feat: start adding builder * [PM-2032] feat: add support for KeyConnector * [PM-2032] feat: add first version of TDE * [PM-2032] chore: refactor WithSso * [PM-2023] feat: add support for TDE feature flag * [PM-2023] feat: add support for approving devices * [PM-2023] feat: add support for hasManageResetPasswordPermission * [PM-2032] feat: add support for hasAdminApproval * [PM-2032] chore: don't supply device if not necessary * [PM-2032] chore: clean up imports * [PM-2023] feat: extract interface * [PM-2023] chore: add clarifying comment * [PM-2023] feat: use new builder in production code * [PM-2032] feat: add support for PRF * [PM-2032] chore: clean-up todos * [PM-2023] chore: remove token which is no longer used * [PM-2032] chore: remove todo * [PM-2032] feat: improve assertion error handling * [PM-2032] fix: linting issues * [PM-2032] fix: revert changes to `launchSettings.json` * [PM-2023] chore: clean up assertion endpoint * [PM-2032] feat: bypass 2FA * [PM-2032] fix: rename prf option to singular * [PM-2032] fix: lint * [PM-2032] fix: typo * [PM-2032] chore: improve builder tests Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com> * [PM-2032] chore: clarify why we don't require 2FA * [PM-2023] feat: move `identityProvider` constant to common class * [PM-2032] fix: lint * [PM-2023] fix: move `IdentityProvider` to core.Constants * [PM-2032] fix: missing import * [PM-2032] chore: refactor token timespan to use `TimeSpan` * [PM-2032] chore: make `StartWebAuthnLoginAssertion` sync * [PM-2032] chore: use `FromMinutes` * [PM-2032] fix: change to 17 minutes to cover webauthn assertion * [PM-2032] chore: do not use `async void` * [PM-2032] fix: comment saying wrong amount of minutes * [PM-2032] feat: put validator behind feature flag * [PM-2032] fix: lint --------- Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com>
121 lines
4.2 KiB
C#
121 lines
4.2 KiB
C#
using Bit.Core.Auth.Models.Api.Request.Accounts;
|
|
using Bit.Core.Auth.Models.Business.Tokenables;
|
|
using Bit.Core.Auth.Services;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Models.Data;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Tokens;
|
|
using Bit.Identity.Controllers;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace Bit.Identity.Test.Controllers;
|
|
|
|
public class AccountsControllerTests : IDisposable
|
|
{
|
|
|
|
private readonly AccountsController _sut;
|
|
private readonly ILogger<AccountsController> _logger;
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly IUserService _userService;
|
|
private readonly ICaptchaValidationService _captchaValidationService;
|
|
private readonly IDataProtectorTokenFactory<WebAuthnLoginAssertionOptionsTokenable> _assertionOptionsDataProtector;
|
|
|
|
public AccountsControllerTests()
|
|
{
|
|
_logger = Substitute.For<ILogger<AccountsController>>();
|
|
_userRepository = Substitute.For<IUserRepository>();
|
|
_userService = Substitute.For<IUserService>();
|
|
_captchaValidationService = Substitute.For<ICaptchaValidationService>();
|
|
_assertionOptionsDataProtector = Substitute.For<IDataProtectorTokenFactory<WebAuthnLoginAssertionOptionsTokenable>>();
|
|
_sut = new AccountsController(
|
|
_logger,
|
|
_userRepository,
|
|
_userService,
|
|
_captchaValidationService,
|
|
_assertionOptionsDataProtector
|
|
);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_sut?.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostPrelogin_WhenUserExists_ShouldReturnUserKdfInfo()
|
|
{
|
|
var userKdfInfo = new UserKdfInformation
|
|
{
|
|
Kdf = KdfType.PBKDF2_SHA256,
|
|
KdfIterations = 5000
|
|
};
|
|
_userRepository.GetKdfInformationByEmailAsync(Arg.Any<string>()).Returns(Task.FromResult(userKdfInfo));
|
|
|
|
var response = await _sut.PostPrelogin(new PreloginRequestModel { Email = "user@example.com" });
|
|
|
|
Assert.Equal(userKdfInfo.Kdf, response.Kdf);
|
|
Assert.Equal(userKdfInfo.KdfIterations, response.KdfIterations);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostPrelogin_WhenUserDoesNotExist_ShouldDefaultToSha256And100000Iterations()
|
|
{
|
|
_userRepository.GetKdfInformationByEmailAsync(Arg.Any<string>()).Returns(Task.FromResult<UserKdfInformation>(null!));
|
|
|
|
var response = await _sut.PostPrelogin(new PreloginRequestModel { Email = "user@example.com" });
|
|
|
|
Assert.Equal(KdfType.PBKDF2_SHA256, response.Kdf);
|
|
Assert.Equal(100000, response.KdfIterations);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostRegister_ShouldRegisterUser()
|
|
{
|
|
var passwordHash = "abcdef";
|
|
var token = "123456";
|
|
var userGuid = new Guid();
|
|
_userService.RegisterUserAsync(Arg.Any<User>(), passwordHash, token, userGuid)
|
|
.Returns(Task.FromResult(IdentityResult.Success));
|
|
var request = new RegisterRequestModel
|
|
{
|
|
Name = "Example User",
|
|
Email = "user@example.com",
|
|
MasterPasswordHash = passwordHash,
|
|
MasterPasswordHint = "example",
|
|
Token = token,
|
|
OrganizationUserId = userGuid
|
|
};
|
|
|
|
await _sut.PostRegister(request);
|
|
|
|
await _userService.Received(1).RegisterUserAsync(Arg.Any<User>(), passwordHash, token, userGuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostRegister_WhenUserServiceFails_ShouldThrowBadRequestException()
|
|
{
|
|
var passwordHash = "abcdef";
|
|
var token = "123456";
|
|
var userGuid = new Guid();
|
|
_userService.RegisterUserAsync(Arg.Any<User>(), passwordHash, token, userGuid)
|
|
.Returns(Task.FromResult(IdentityResult.Failed()));
|
|
var request = new RegisterRequestModel
|
|
{
|
|
Name = "Example User",
|
|
Email = "user@example.com",
|
|
MasterPasswordHash = passwordHash,
|
|
MasterPasswordHint = "example",
|
|
Token = token,
|
|
OrganizationUserId = userGuid
|
|
};
|
|
|
|
await Assert.ThrowsAsync<BadRequestException>(() => _sut.PostRegister(request));
|
|
}
|
|
}
|