1
0
mirror of https://github.com/bitwarden/server synced 2025-12-22 03:03:33 +00:00
This commit is contained in:
Bernd Schoolmann
2025-12-02 16:50:59 +01:00
parent b151506ed2
commit 9015bc8169
4 changed files with 22 additions and 21 deletions

View File

@@ -1,13 +1,9 @@
using Bit.Core.KeyManagement.Models.Api.Request;
using Bit.Core.KeyManagement.Repositories;
using Bit.Core.Repositories;
namespace Bit.Core.KeyManagement.Commands.Interfaces;
public interface ISetAccountKeysForUserCommand
{
Task SetAccountKeysForUserAsync(Guid userId,
AccountKeysRequestModel accountKeys,
IUserRepository userRepository,
IUserSignatureKeyPairRepository userSignatureKeyPairRepository);
AccountKeysRequestModel accountKeys);
}

View File

@@ -9,9 +9,19 @@ namespace Bit.Core.KeyManagement.Commands;
public class SetAccountKeysForUserCommand : ISetAccountKeysForUserCommand
{
public async Task SetAccountKeysForUserAsync(Guid userId, AccountKeysRequestModel accountKeys, IUserRepository userRepository, IUserSignatureKeyPairRepository userSignatureKeyPairRepository)
private readonly IUserRepository _userRepository;
private readonly IUserSignatureKeyPairRepository _userSignatureKeyPairRepository;
public SetAccountKeysForUserCommand(
IUserRepository userRepository,
IUserSignatureKeyPairRepository userSignatureKeyPairRepository)
{
var user = await userRepository.GetByIdAsync(userId);
_userRepository = userRepository;
_userSignatureKeyPairRepository = userSignatureKeyPairRepository;
}
public async Task SetAccountKeysForUserAsync(Guid userId, AccountKeysRequestModel accountKeys)
{
var user = await _userRepository.GetByIdAsync(userId);
if (user == null)
{
throw new ArgumentException("User not found", nameof(userId));
@@ -29,7 +39,7 @@ public class SetAccountKeysForUserCommand : ISetAccountKeysForUserCommand
user.SignedPublicKey = accountKeysData.PublicKeyEncryptionKeyPairData.SignedPublicKey;
user.SecurityState = accountKeysData.SecurityStateData.SecurityState;
user.SecurityVersion = accountKeysData.SecurityStateData.SecurityVersion;
await userSignatureKeyPairRepository.UpsertAsync(new UserSignatureKeyPair
await _userSignatureKeyPairRepository.UpsertAsync(new UserSignatureKeyPair
{
Id = CoreHelpers.GenerateComb(),
UserId = userId,
@@ -40,6 +50,6 @@ public class SetAccountKeysForUserCommand : ISetAccountKeysForUserCommand
RevisionDate = DateTime.UtcNow,
});
}
await userRepository.ReplaceAsync(user);
await _userRepository.ReplaceAsync(user);
}
}

View File

@@ -21,6 +21,7 @@ public static class KeyManagementServiceCollectionExtensions
{
services.AddScoped<IRegenerateUserAsymmetricKeysCommand, RegenerateUserAsymmetricKeysCommand>();
services.AddScoped<IChangeKdfCommand, ChangeKdfCommand>();
services.AddScoped<ISetAccountKeysForUserCommand, SetAccountKeysForUserCommand>();
}
private static void AddKeyManagementQueries(this IServiceCollection services)

View File

@@ -20,16 +20,14 @@ public class SetAccountKeysForUserCommandTests
Guid userId,
AccountKeysRequestModel accountKeys)
{
var command = new SetAccountKeysForUserCommand();
var userRepository = Substitute.For<IUserRepository>();
var userSignatureKeyPairRepository = Substitute.For<IUserSignatureKeyPairRepository>();
var command = new SetAccountKeysForUserCommand(userRepository, userSignatureKeyPairRepository);
userRepository.GetByIdAsync(userId).ReturnsNullForAnyArgs();
var exception = await Assert.ThrowsAsync<ArgumentException>(() =>
command.SetAccountKeysForUserAsync(userId, accountKeys,
userRepository,
userSignatureKeyPairRepository));
command.SetAccountKeysForUserAsync(userId, accountKeys));
Assert.Equal("userId", exception.ParamName);
Assert.Contains("User not found", exception.Message);
@@ -50,15 +48,13 @@ public class SetAccountKeysForUserCommandTests
user.SecurityState = null;
user.SecurityVersion = null;
var command = new SetAccountKeysForUserCommand();
var userRepository = Substitute.For<IUserRepository>();
var userSignatureKeyPairRepository = Substitute.For<IUserSignatureKeyPairRepository>();
var command = new SetAccountKeysForUserCommand(userRepository, userSignatureKeyPairRepository);
userRepository.GetByIdAsync(user.Id).Returns(user);
await command.SetAccountKeysForUserAsync(user.Id, accountKeys,
userRepository,
userSignatureKeyPairRepository);
await command.SetAccountKeysForUserAsync(user.Id, accountKeys);
Assert.Equal(accountKeys.UserKeyEncryptedAccountPrivateKey, user.PrivateKey);
Assert.Equal(accountKeys.AccountPublicKey, user.PublicKey);
@@ -109,15 +105,13 @@ public class SetAccountKeysForUserCommandTests
SecurityState = securityState
};
var command = new SetAccountKeysForUserCommand();
var userRepository = Substitute.For<IUserRepository>();
var userSignatureKeyPairRepository = Substitute.For<IUserSignatureKeyPairRepository>();
var command = new SetAccountKeysForUserCommand(userRepository, userSignatureKeyPairRepository);
userRepository.GetByIdAsync(user.Id).Returns(user);
await command.SetAccountKeysForUserAsync(user.Id, accountKeys,
userRepository,
userSignatureKeyPairRepository);
await command.SetAccountKeysForUserAsync(user.Id, accountKeys);
Assert.Equal(publicKeyEncryptionKeyPair.WrappedPrivateKey, user.PrivateKey);
Assert.Equal(publicKeyEncryptionKeyPair.PublicKey, user.PublicKey);