mirror of
https://github.com/bitwarden/server.git
synced 2025-06-19 18:38:03 -05:00

* Add signing key repositories, models, and sql migration scripts * Rename UserSigningKeys table to UserSigningKey * Rename signedpublickeyownershipclaim to signedpublickey * Move signedPublicKey to last parameter * Add newline at end of file * Rename to signature key pair * Further rename to signaturekeypair * Rename to UserSignatureKeyPairRepository * Add newline * Rename more instances to UserSignatureKeyPair * Update parameter order * Fix order * Add more renames * Cleanup * Fix sql * Add ef migrations * Fix difference in SQL SP compared to migration SP * Fix difference in SQL SP vs migration * Fix difference in SQL SP vs migration * Attempt to fix sql * Rename migration to start later * Address feedback * Move UserSignatureKeyPair to KM codeownership * Fix build * Fix build * Fix build * Move out entitytypeconfiguration * Use view for reading usersignaturekeypairs * Fix migration script * Fix migration script * Add initial get keys endpoint * Add sync response * Cleanup * Add query and fix types * Add tests and cleanup * Fix test * Drop view if exists * Add km queries * Cleanup * Enable nullable * Cleanup * Cleanup * Enable nullable * Fix incorrect namespace * Remove unused using * Fix test build * Fix build error * Fix build * Attempt to fix tests * Attempt to fix tests * Replace with create or alter view * Attempt to fix tests * Attempt to fix build * Rename to include async suffix * Fix test * Rename repo * Attempt to fix tests * Cleanup * Test * Undo test * Fix tests * Fix test * Switch go generatecomb * Switch to generatecomb * Move signature algorithm * Move useresignaturekeypairentitytypeconfiguration to km ownership * Move userSignatureKeyPair model * Unswap file names * Move sql files to km ownership * Add index on userid for signature keys * Fix wrong filename * Fix build * Remove string length limit * Regenerate EF migrations * Undo changes to program.cs * Update util/Migrator/DbScripts/2025-06-01_00_AddSignatureKeyPairTable.sql Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update util/Migrator/DbScripts/2025-06-01_00_AddSignatureKeyPairTable.sql Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update util/Migrator/DbScripts/2025-06-01_00_AddSignatureKeyPairTable.sql Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update util/Migrator/DbScripts/2025-06-01_00_AddSignatureKeyPairTable.sql Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Rename dbset to plural * Update src/Infrastructure.EntityFramework/KeyManagement/Repositories/UserSignatureKeyPairRepository.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update src/Api/KeyManagement/Models/Response/PrivateKeysResponseModel.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update src/Api/KeyManagement/Controllers/UsersController.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Cleanup and move query to core * Fix test * Fix build * Fix tests * Update src/Api/KeyManagement/Models/Response/PrivateKeysResponseModel.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Switch away from primary constructor * Use argumentNullException * Add test * Pass user account keys directly to profileresponsemodel * Move registration to core * Update src/Api/Startup.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update src/Api/Startup.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Remove empty line * Apply suggestions * Fix tests * Fix tests --------- Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
113 lines
3.9 KiB
C#
113 lines
3.9 KiB
C#
#nullable enable
|
|
using Bit.Api.KeyManagement.Controllers;
|
|
using Bit.Api.KeyManagement.Queries.Interfaces;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.KeyManagement.Enums;
|
|
using Bit.Core.KeyManagement.Models.Data;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Test.Common.AutoFixture;
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
using NSubstitute;
|
|
using NSubstitute.ReturnsExtensions;
|
|
using Xunit;
|
|
|
|
namespace Bit.Api.Test.KeyManagement.Controllers;
|
|
|
|
[ControllerCustomize(typeof(UsersController))]
|
|
[SutProviderCustomize]
|
|
[JsonDocumentCustomize]
|
|
public class UsersControllerTests
|
|
{
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task GetPublicKey_NotFound_ThrowsNotFoundException(
|
|
SutProvider<UsersController> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IUserRepository>().GetPublicKeyAsync(Arg.Any<Guid>()).ReturnsNull();
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetPublicKeyAsync(new Guid()));
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task GetPublicKey_ReturnsUserKeyResponseModel(
|
|
SutProvider<UsersController> sutProvider,
|
|
Guid userId)
|
|
{
|
|
var publicKey = "publicKey";
|
|
sutProvider.GetDependency<IUserRepository>().GetPublicKeyAsync(userId).Returns(publicKey);
|
|
|
|
var result = await sutProvider.Sut.GetPublicKeyAsync(userId);
|
|
Assert.NotNull(result);
|
|
Assert.Equal(userId, result.UserId);
|
|
Assert.Equal(publicKey, result.PublicKey);
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task GetAccountKeys_UserNotFound_ThrowsNotFoundException(
|
|
SutProvider<UsersController> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IUserRepository>().GetByIdAsync(Arg.Any<Guid>()).ReturnsNull();
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetAccountKeysAsync(new Guid()));
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task GetAccountKeys_ReturnsPublicUserKeysResponseModel(
|
|
SutProvider<UsersController> sutProvider,
|
|
Guid userId)
|
|
{
|
|
var user = new User
|
|
{
|
|
Id = userId,
|
|
PublicKey = "publicKey",
|
|
SignedPublicKey = "signedPublicKey",
|
|
};
|
|
|
|
sutProvider.GetDependency<IUserRepository>().GetByIdAsync(userId).Returns(user);
|
|
sutProvider.GetDependency<IUserAccountKeysQuery>()
|
|
.Run(user)
|
|
.Returns(new UserAccountKeysData
|
|
{
|
|
PublicKeyEncryptionKeyPairData = new PublicKeyEncryptionKeyPairData("wrappedPrivateKey", "publicKey", "signedPublicKey"),
|
|
SignatureKeyPairData = new SignatureKeyPairData(SignatureAlgorithm.Ed25519, "wrappedSigningKey", "verifyingKey"),
|
|
});
|
|
|
|
var result = await sutProvider.Sut.GetAccountKeysAsync(userId);
|
|
Assert.NotNull(result);
|
|
Assert.Equal("publicKey", result.PublicKey);
|
|
Assert.Equal("signedPublicKey", result.SignedPublicKey);
|
|
Assert.Equal("verifyingKey", result.VerifyingKey);
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task GetAccountKeys_ReturnsPublicUserKeysResponseModel_WithNullVerifyingKey(
|
|
SutProvider<UsersController> sutProvider,
|
|
Guid userId)
|
|
{
|
|
var user = new User
|
|
{
|
|
Id = userId,
|
|
PublicKey = "publicKey",
|
|
SignedPublicKey = null,
|
|
};
|
|
|
|
sutProvider.GetDependency<IUserRepository>().GetByIdAsync(userId).Returns(user);
|
|
sutProvider.GetDependency<IUserAccountKeysQuery>()
|
|
.Run(user)
|
|
.Returns(new UserAccountKeysData
|
|
{
|
|
PublicKeyEncryptionKeyPairData = new PublicKeyEncryptionKeyPairData("wrappedPrivateKey", "publicKey", null),
|
|
SignatureKeyPairData = null,
|
|
});
|
|
|
|
var result = await sutProvider.Sut.GetAccountKeysAsync(userId);
|
|
Assert.NotNull(result);
|
|
Assert.Equal("publicKey", result.PublicKey);
|
|
Assert.Null(result.SignedPublicKey);
|
|
Assert.Null(result.VerifyingKey);
|
|
}
|
|
}
|