mirror of
https://github.com/bitwarden/server.git
synced 2025-06-07 11:40:31 -05:00
75 lines
2.9 KiB
C#
75 lines
2.9 KiB
C#
#nullable enable
|
|
using AutoMapper;
|
|
using Bit.Core.KeyManagement.Models.Data;
|
|
using Bit.Core.KeyManagement.Repositories;
|
|
using Bit.Core.KeyManagement.UserKey;
|
|
using Bit.Core.Utilities;
|
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Bit.Infrastructure.EntityFramework.KeyManagement.Repositories;
|
|
|
|
public class UserSignatureKeyPairRepository : Repository<Core.KeyManagement.Entities.UserSignatureKeyPair, Models.UserSignatureKeyPair, Guid>, IUserSignatureKeyPairRepository
|
|
{
|
|
public UserSignatureKeyPairRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) : base(serviceScopeFactory, mapper, context => context.UserSignatureKeyPair)
|
|
{
|
|
}
|
|
|
|
public async Task<SignatureKeyPairData?> GetByUserIdAsync(Guid userId)
|
|
{
|
|
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
|
var dbContext = GetDatabaseContext(scope);
|
|
var signingKeys = await dbContext.UserSignatureKeyPair.FindAsync(userId);
|
|
if (signingKeys == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new SignatureKeyPairData(
|
|
signingKeys.SignatureAlgorithm,
|
|
signingKeys.SigningKey,
|
|
signingKeys.VerifyingKey
|
|
);
|
|
}
|
|
|
|
public UpdateEncryptedDataForKeyRotation SetUserSignatureKeyPair(Guid userId, SignatureKeyPairData signingKeys)
|
|
{
|
|
return async (_, _) =>
|
|
{
|
|
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
|
var dbContext = GetDatabaseContext(scope);
|
|
var entity = new Models.UserSignatureKeyPair
|
|
{
|
|
Id = CoreHelpers.GenerateComb(),
|
|
UserId = userId,
|
|
SignatureAlgorithm = signingKeys.SignatureAlgorithm,
|
|
SigningKey = signingKeys.WrappedSigningKey,
|
|
VerifyingKey = signingKeys.VerifyingKey,
|
|
CreationDate = DateTime.UtcNow,
|
|
RevisionDate = DateTime.UtcNow,
|
|
};
|
|
await dbContext.UserSignatureKeyPair.AddAsync(entity);
|
|
await dbContext.SaveChangesAsync();
|
|
};
|
|
}
|
|
|
|
public UpdateEncryptedDataForKeyRotation UpdateForKeyRotation(Guid grantorId, SignatureKeyPairData signingKeys)
|
|
{
|
|
return async (_, _) =>
|
|
{
|
|
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
|
var dbContext = GetDatabaseContext(scope);
|
|
var entity = await dbContext.UserSignatureKeyPair.FirstOrDefaultAsync(x => x.UserId == grantorId);
|
|
if (entity != null)
|
|
{
|
|
entity.SignatureAlgorithm = signingKeys.SignatureAlgorithm;
|
|
entity.SigningKey = signingKeys.WrappedSigningKey;
|
|
entity.VerifyingKey = signingKeys.VerifyingKey;
|
|
entity.RevisionDate = DateTime.UtcNow;
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
};
|
|
}
|
|
}
|