1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-28 08:12:22 -05:00
Todd Martin 7ede956c32
Individual Vault Item Encryption Feature (#3256)
* [SG-966] [SG-967] Add new cipher properties, update DB objects and create migrations (#2681)

* Updated cipher entity with two new columns

* Added sqlserver mifgration and updated applicable stored procedures and table

* Added EF Migrations

* Made changes to response model to include new column properties

* Fixed formatting

* Modified scripts to reflect suggestions made on PR

* Added column to cipher table using default

* Include constraint in create cipher table script

* Added key and forcerotatekey property to request model (#2716)

* Added key update on the Cipher_UpdateWithCollection stored procedure, ef (#2855)

* Added key and forceKeyRotation to BuildCiphersTable method (#2893)

* [PM-2211] Remove forceKeyRotation column (#2921)

* Removed forceKeyRotation column

* Adjusted date for migrtaion file

* Passed key column to update cipher script to update cipher key when it is rotated (#2967)

* [PM-2448] Update CipherDetails_Update SP to update attachment column (#2992)

* Updated the cipherdetails_update stored procedure to update the attachement column when encrypted with the cipher key

* Moved migration and renamed old migration file

* Fixed lint issues

* Fixed lint issues

* renamed sqlserver migration to have a more recent date

* [PM-2548] Added validation to edit and add attachments methods (#3130)

* PM-2548 Added validation to edit and add attachments methods

* PM-2548 Moved the validation to a private method

* PM-2548 Minor refactor

* Bumped up minimum version

* Bumped up minimum version

* Changed version for tests purposes

* Bumped up minimum version

* Updated encryption minimum version to match clients for QA.

* PM-3976 Passed Key column to update cipher on bulk edit (#3299)

* Updated minimum client version in preparation for release.

* Renamed migration with current date. (#3303)

---------

Co-authored-by: SmithThe4th <gsmith@bitwarden.com>
Co-authored-by: gbubemismith <gsmithwalter@gmail.com>
Co-authored-by: Carlos Gonçalves <cgoncalves@bitwarden.com>
Co-authored-by: Carlos Gonçalves <carlosmaccam@gmail.com>
2023-09-28 08:45:13 -04:00

115 lines
3.0 KiB
C#

using System.Text.Json;
using Bit.Core.Entities;
using Bit.Core.Utilities;
using Bit.Core.Vault.Models.Data;
namespace Bit.Core.Vault.Entities;
public class Cipher : ITableObject<Guid>, ICloneable
{
private Dictionary<string, CipherAttachment.MetaData> _attachmentData;
public Guid Id { get; set; }
public Guid? UserId { get; set; }
public Guid? OrganizationId { get; set; }
public Enums.CipherType Type { get; set; }
public string Data { get; set; }
public string Favorites { get; set; }
public string Folders { get; set; }
public string Attachments { get; set; }
public DateTime CreationDate { get; set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; set; } = DateTime.UtcNow;
public DateTime? DeletedDate { get; set; }
public Enums.CipherRepromptType? Reprompt { get; set; }
public string Key { get; set; }
public void SetNewId()
{
Id = CoreHelpers.GenerateComb();
}
public Dictionary<string, CipherAttachment.MetaData> GetAttachments()
{
if (string.IsNullOrWhiteSpace(Attachments))
{
return null;
}
if (_attachmentData != null)
{
return _attachmentData;
}
try
{
_attachmentData = JsonSerializer.Deserialize<Dictionary<string, CipherAttachment.MetaData>>(Attachments);
foreach (var kvp in _attachmentData)
{
kvp.Value.AttachmentId = kvp.Key;
if (kvp.Value.TempMetadata != null)
{
kvp.Value.TempMetadata.AttachmentId = kvp.Key;
}
}
return _attachmentData;
}
catch
{
return null;
}
}
public void SetAttachments(Dictionary<string, CipherAttachment.MetaData> data)
{
if (data == null || data.Count == 0)
{
_attachmentData = null;
Attachments = null;
return;
}
_attachmentData = data;
Attachments = JsonSerializer.Serialize(_attachmentData);
}
public void AddAttachment(string id, CipherAttachment.MetaData data)
{
var attachments = GetAttachments();
if (attachments == null)
{
attachments = new Dictionary<string, CipherAttachment.MetaData>();
}
attachments.Add(id, data);
SetAttachments(attachments);
}
public void DeleteAttachment(string id)
{
var attachments = GetAttachments();
if (!attachments?.ContainsKey(id) ?? true)
{
return;
}
attachments.Remove(id);
SetAttachments(attachments);
}
public bool ContainsAttachment(string id)
{
var attachments = GetAttachments();
return attachments?.ContainsKey(id) ?? false;
}
object ICloneable.Clone() => Clone();
public Cipher Clone()
{
var clone = CoreHelpers.CloneObject(this);
clone.CreationDate = CreationDate;
clone.RevisionDate = RevisionDate;
return clone;
}
}