diff --git a/src/Core/KeyManagement/Commands/Interfaces/ISetAccountKeysForUserCommand.cs b/src/Core/KeyManagement/Commands/Interfaces/ISetAccountKeysForUserCommand.cs index 0e18f48daf..7346152248 100644 --- a/src/Core/KeyManagement/Commands/Interfaces/ISetAccountKeysForUserCommand.cs +++ b/src/Core/KeyManagement/Commands/Interfaces/ISetAccountKeysForUserCommand.cs @@ -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); } diff --git a/src/Core/KeyManagement/Commands/SetAccountKeysForUserCommand.cs b/src/Core/KeyManagement/Commands/SetAccountKeysForUserCommand.cs index 6d2a3612f2..b47f60809b 100644 --- a/src/Core/KeyManagement/Commands/SetAccountKeysForUserCommand.cs +++ b/src/Core/KeyManagement/Commands/SetAccountKeysForUserCommand.cs @@ -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); } } diff --git a/src/Core/KeyManagement/KeyManagementServiceCollectionExtensions.cs b/src/Core/KeyManagement/KeyManagementServiceCollectionExtensions.cs index 0e551c5d0e..253cf2e0b0 100644 --- a/src/Core/KeyManagement/KeyManagementServiceCollectionExtensions.cs +++ b/src/Core/KeyManagement/KeyManagementServiceCollectionExtensions.cs @@ -21,6 +21,7 @@ public static class KeyManagementServiceCollectionExtensions { services.AddScoped(); services.AddScoped(); + services.AddScoped(); } private static void AddKeyManagementQueries(this IServiceCollection services) diff --git a/test/Core.Test/KeyManagement/Commands/SetAccountKeysForUserCommandTests.cs b/test/Core.Test/KeyManagement/Commands/SetAccountKeysForUserCommandTests.cs index df8976ffbf..16477d4663 100644 --- a/test/Core.Test/KeyManagement/Commands/SetAccountKeysForUserCommandTests.cs +++ b/test/Core.Test/KeyManagement/Commands/SetAccountKeysForUserCommandTests.cs @@ -20,16 +20,14 @@ public class SetAccountKeysForUserCommandTests Guid userId, AccountKeysRequestModel accountKeys) { - var command = new SetAccountKeysForUserCommand(); var userRepository = Substitute.For(); var userSignatureKeyPairRepository = Substitute.For(); + var command = new SetAccountKeysForUserCommand(userRepository, userSignatureKeyPairRepository); userRepository.GetByIdAsync(userId).ReturnsNullForAnyArgs(); var exception = await Assert.ThrowsAsync(() => - 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(); var userSignatureKeyPairRepository = Substitute.For(); + 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(); var userSignatureKeyPairRepository = Substitute.For(); + 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);