1
0
mirror of https://github.com/bitwarden/server synced 2026-01-09 12:03:21 +00:00
Files
server/src/Api/KeyManagement/Validators/CipherRotationValidator.cs
Bernd Schoolmann fae8692d2a [PM-12607] Move key rotation & validators to km ownership (#4941)
* Move key rotation & validators to km ownership

* Fix build errors

* Fix build errors

* Fix import ordering

* Update validator namespace

* Move key rotation data to km ownership

* Fix linting

* Fix namespaces

* Fix namespace

* Fix namespaces

* Move rotateuserkeycommandtests to km ownership
2024-11-21 19:17:04 +01:00

46 lines
1.4 KiB
C#

using Bit.Api.Vault.Models.Request;
using Bit.Core.Entities;
using Bit.Core.Exceptions;
using Bit.Core.Vault.Entities;
using Bit.Core.Vault.Repositories;
namespace Bit.Api.KeyManagement.Validators;
public class CipherRotationValidator : IRotationValidator<IEnumerable<CipherWithIdRequestModel>, IEnumerable<Cipher>>
{
private readonly ICipherRepository _cipherRepository;
public CipherRotationValidator(ICipherRepository cipherRepository)
{
_cipherRepository = cipherRepository;
}
public async Task<IEnumerable<Cipher>> ValidateAsync(User user, IEnumerable<CipherWithIdRequestModel> ciphers)
{
var result = new List<Cipher>();
var existingCiphers = await _cipherRepository.GetManyByUserIdAsync(user.Id);
if (existingCiphers == null)
{
return result;
}
var existingUserCiphers = existingCiphers.Where(c => c.OrganizationId == null);
if (existingUserCiphers.Count() == 0)
{
return result;
}
foreach (var existing in existingUserCiphers)
{
var cipher = ciphers.FirstOrDefault(c => c.Id == existing.Id);
if (cipher == null)
{
throw new BadRequestException("All existing ciphers must be included in the rotation.");
}
result.Add(cipher.ToCipher(existing));
}
return result;
}
}