using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using Bit.Core.Entities; using Bit.Core.Enums; using Bit.Core.Models.Api; using Bit.Core.Models.Data; using Bit.Core.Settings; using Core.Models.Data; namespace Bit.Api.Models.Response { public class CipherMiniResponseModel : ResponseModel { public CipherMiniResponseModel(Cipher cipher, IGlobalSettings globalSettings, bool orgUseTotp, string obj = "cipherMini") : base(obj) { if (cipher == null) { throw new ArgumentNullException(nameof(cipher)); } Id = cipher.Id.ToString(); Type = cipher.Type; CipherData cipherData; switch (cipher.Type) { case CipherType.Login: var loginData = JsonSerializer.Deserialize(cipher.Data); cipherData = loginData; Data = loginData; Login = new CipherLoginModel(loginData); break; case CipherType.SecureNote: var secureNoteData = JsonSerializer.Deserialize(cipher.Data); Data = secureNoteData; cipherData = secureNoteData; SecureNote = new CipherSecureNoteModel(secureNoteData); break; case CipherType.Card: var cardData = JsonSerializer.Deserialize(cipher.Data); Data = cardData; cipherData = cardData; Card = new CipherCardModel(cardData); break; case CipherType.Identity: var identityData = JsonSerializer.Deserialize(cipher.Data); Data = identityData; cipherData = identityData; Identity = new CipherIdentityModel(identityData); break; default: throw new ArgumentException("Unsupported " + nameof(Type) + "."); } Name = cipherData.Name; Notes = cipherData.Notes; Fields = cipherData.Fields?.Select(f => new CipherFieldModel(f)); PasswordHistory = cipherData.PasswordHistory?.Select(ph => new CipherPasswordHistoryModel(ph)); RevisionDate = cipher.RevisionDate; OrganizationId = cipher.OrganizationId?.ToString(); Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings); OrganizationUseTotp = orgUseTotp; DeletedDate = cipher.DeletedDate; Reprompt = cipher.Reprompt.GetValueOrDefault(CipherRepromptType.None); } public string Id { get; set; } public string OrganizationId { get; set; } public CipherType Type { get; set; } public dynamic Data { get; set; } public string Name { get; set; } public string Notes { get; set; } public CipherLoginModel Login { get; set; } public CipherCardModel Card { get; set; } public CipherIdentityModel Identity { get; set; } public CipherSecureNoteModel SecureNote { get; set; } public IEnumerable Fields { get; set; } public IEnumerable PasswordHistory { get; set; } public IEnumerable Attachments { get; set; } public bool OrganizationUseTotp { get; set; } public DateTime RevisionDate { get; set; } public DateTime? DeletedDate { get; set; } public CipherRepromptType Reprompt { get; set; } } public class CipherResponseModel : CipherMiniResponseModel { public CipherResponseModel(CipherDetails cipher, IGlobalSettings globalSettings, string obj = "cipher") : base(cipher, globalSettings, cipher.OrganizationUseTotp, obj) { FolderId = cipher.FolderId?.ToString(); Favorite = cipher.Favorite; Edit = cipher.Edit; ViewPassword = cipher.ViewPassword; } public string FolderId { get; set; } public bool Favorite { get; set; } public bool Edit { get; set; } public bool ViewPassword { get; set; } } public class CipherDetailsResponseModel : CipherResponseModel { public CipherDetailsResponseModel(CipherDetails cipher, GlobalSettings globalSettings, IDictionary> collectionCiphers, string obj = "cipherDetails") : base(cipher, globalSettings, obj) { if (collectionCiphers?.ContainsKey(cipher.Id) ?? false) { CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId); } else { CollectionIds = new Guid[] { }; } } public CipherDetailsResponseModel(CipherDetails cipher, GlobalSettings globalSettings, IEnumerable collectionCiphers, string obj = "cipherDetails") : base(cipher, globalSettings, obj) { CollectionIds = collectionCiphers?.Select(c => c.CollectionId) ?? new List(); } public IEnumerable CollectionIds { get; set; } } public class CipherMiniDetailsResponseModel : CipherMiniResponseModel { public CipherMiniDetailsResponseModel(Cipher cipher, GlobalSettings globalSettings, IDictionary> collectionCiphers, bool orgUseTotp, string obj = "cipherMiniDetails") : base(cipher, globalSettings, orgUseTotp, obj) { if (collectionCiphers?.ContainsKey(cipher.Id) ?? false) { CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId); } else { CollectionIds = new Guid[] { }; } } public IEnumerable CollectionIds { get; set; } } }