using System.Text.Json; using Bit.Core.Models.Api; using Bit.Core.Settings; using Bit.Core.Tools.Entities; using Bit.Core.Tools.Enums; using Bit.Core.Tools.Models.Data; using Bit.Core.Utilities; namespace Bit.Api.Tools.Models.Response; /// /// A response issued to a Bitwarden client in response to ownership operations. /// /// public class SendResponseModel : ResponseModel { /// /// Instantiates a send response model /// /// Content to transmit to the client. /// /// Settings that control response generation. /// /// /// Thrown when is /// /// /// Thrown when has an invalid . /// public SendResponseModel(Send send, GlobalSettings globalSettings) : base("send") { if (send == null) { throw new ArgumentNullException(nameof(send)); } Id = send.Id; AccessId = CoreHelpers.Base64UrlEncode(send.Id.ToByteArray()); Type = send.Type; Key = send.Key; MaxAccessCount = send.MaxAccessCount; AccessCount = send.AccessCount; RevisionDate = send.RevisionDate; ExpirationDate = send.ExpirationDate; DeletionDate = send.DeletionDate; Password = send.Password; Emails = send.Emails; Disabled = send.Disabled; HideEmail = send.HideEmail.GetValueOrDefault(); SendData sendData; switch (send.Type) { case SendType.File: var fileData = JsonSerializer.Deserialize(send.Data); sendData = fileData; File = new SendFileModel(fileData); break; case SendType.Text: var textData = JsonSerializer.Deserialize(send.Data); sendData = textData; Text = new SendTextModel(textData); break; default: throw new ArgumentException("Unsupported " + nameof(Type) + "."); } Name = sendData.Name; Notes = sendData.Notes; } /// /// Identifies the send to its owner /// public Guid Id { get; set; } /// /// Identifies the send in a send URL /// public string AccessId { get; set; } /// /// Indicates whether the send contains text or file data. /// public SendType Type { get; set; } /// /// Label for the send. /// /// /// This field contains a base64-encoded byte array. The array contains /// the E2E-encrypted encrypted content. /// public string Name { get; set; } /// /// Notes for the send. This is only visible to the owner of the send. /// This field is encrypted. /// /// /// This field contains a base64-encoded byte array. The array contains /// the E2E-encrypted encrypted content. /// public string Notes { get; set; } /// /// Contains file metadata uploaded with the send. /// The file content is uploaded separately. /// public SendFileModel File { get; set; } /// /// Contains text data uploaded with the send. /// public SendTextModel Text { get; set; } /// /// A base64-encoded byte array containing the Send's encryption key. /// It's also provided to send recipients in the Send's URL. /// /// /// This field contains a base64-encoded byte array. The array contains /// the E2E-encrypted content. /// public string Key { get; set; } /// /// The maximum number of times a send can be accessed before it expires. /// When this value is , there is no limit. /// public int? MaxAccessCount { get; set; } /// /// The number of times a send has been accessed since it was created. /// public int AccessCount { get; set; } /// /// Base64-encoded byte array of a password hash that grants access to the send. /// Mutually exclusive with . /// public string Password { get; set; } /// /// Comma-separated list of emails that may access the send using OTP /// authentication. Mutually exclusive with . /// public string Emails { get; set; } /// /// When , send access is disabled. /// public bool Disabled { get; set; } /// /// The last time this send's data changed. /// public DateTime RevisionDate { get; set; } /// /// The date after which a send cannot be accessed. When this value is /// , there is no expiration date. /// public DateTime? ExpirationDate { get; set; } /// /// The date after which a send may be automatically deleted from the server. /// public DateTime DeletionDate { get; set; } /// /// When send access hides the user's email address /// and displays a confirmation message instead. /// public bool HideEmail { get; set; } }