Files
server/util/Seeder/Factories/CipherEncryption.cs
Matt Gibson 4f37c93349 Arch/cipher scene (#7241)
* User Cipher scene

For now only supports one login cipher

* Fixup batch delete, which fails due to db collisions

* Create cipher scenes for each cipher type

* Remove unnecessary mutex locking

* Include notes in ssh key ciphers

* Add reprompt to ssh keys

* Add deleted and archived options to login cipher seeder

* Remove ArchivedDate for now

* Update util/Seeder/Factories/SshKeyCipherSeeder.cs

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>

* Allow setting favorite in seeder

* Propagate favorites to created cipher

* Propagate delete date to cipher creation

fix favorites, which have to be all caps for detection on the client side

* conditionally set cipher as favorite

* More review comments

---------

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
2026-03-24 12:00:26 -07:00

62 lines
2.0 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using Bit.Core.Utilities;
using Bit.Core.Vault.Entities;
using Bit.Core.Vault.Enums;
using Bit.RustSDK;
using Bit.Seeder.Attributes;
using Bit.Seeder.Models;
namespace Bit.Seeder.Factories;
internal static class CipherEncryption
{
private static readonly JsonSerializerOptions _sdkJsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
private static readonly JsonSerializerOptions _serverJsonOptions = new()
{
PropertyNamingPolicy = null,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
private static readonly string _fieldPathsJson =
JsonSerializer.Serialize(EncryptPropertyAttribute.GetFieldPaths<CipherViewDto>());
internal static EncryptedCipherDto Encrypt(CipherViewDto cipherView, string keyBase64)
{
var viewJson = JsonSerializer.Serialize(cipherView, _sdkJsonOptions);
var encryptedJson = RustSdkService.EncryptFields(viewJson, _fieldPathsJson, keyBase64);
return JsonSerializer.Deserialize<EncryptedCipherDto>(encryptedJson, _sdkJsonOptions)
?? throw new InvalidOperationException("Failed to parse encrypted cipher");
}
internal static Cipher CreateEntity(
EncryptedCipherDto encrypted,
object data,
CipherType cipherType,
Guid? organizationId,
Guid? userId,
DateTime? deletedDate = null)
{
var dataJson = JsonSerializer.Serialize(data, _serverJsonOptions);
return new Cipher
{
Id = CoreHelpers.GenerateComb(),
OrganizationId = organizationId,
UserId = userId,
Type = cipherType,
Data = dataJson,
Key = encrypted.Key,
Reprompt = (CipherRepromptType?)encrypted.Reprompt,
CreationDate = DateTime.UtcNow,
RevisionDate = DateTime.UtcNow,
DeletedDate = deletedDate
};
}
}