using System; using System.Collections.Generic; using Bit.App.Resources; using System.Linq; using Bit.App.Enums; namespace Bit.App.Models.Page { public class VaultListPageModel { public class Cipher { public Cipher(Models.Cipher cipher) { Id = cipher.Id; Shared = !string.IsNullOrWhiteSpace(cipher.OrganizationId); HasAttachments = cipher.Attachments?.Any() ?? false; FolderId = cipher.FolderId; Name = cipher.Name?.Decrypt(cipher.OrganizationId); Type = cipher.Type; switch(cipher.Type) { case CipherType.Login: LoginUsername = cipher.Login?.Username?.Decrypt(cipher.OrganizationId) ?? " "; LoginUri = cipher.Login?.Uri?.Decrypt(cipher.OrganizationId) ?? " "; LoginPassword = new Lazy(() => cipher.Login?.Password?.Decrypt(cipher.OrganizationId)); LoginTotp = new Lazy(() => cipher.Login?.Totp?.Decrypt(cipher.OrganizationId)); Icon = "login.png"; var hostnameUri = LoginUri; var isWebsite = false; if(hostnameUri.StartsWith("androidapp://")) { Icon = "android.png"; } else if(hostnameUri.StartsWith("iosapp://")) { Icon = "apple.png"; } else if(!hostnameUri.Contains("://") && hostnameUri.Contains(".")) { hostnameUri = $"http://{hostnameUri}"; isWebsite = true; } else if(true) { isWebsite = hostnameUri.StartsWith("http") && hostnameUri.Contains("."); } if(isWebsite && Uri.TryCreate(LoginUri, UriKind.Absolute, out Uri u)) { Icon = "https://icons.bitwarden.com/" + u.Host + "/icon.png"; } Subtitle = LoginUsername; break; case CipherType.SecureNote: Icon = "note.png"; Subtitle = " "; break; case CipherType.Card: CardNumber = cipher.Card?.Number?.Decrypt(cipher.OrganizationId) ?? " "; var cardBrand = cipher.Card?.Brand?.Decrypt(cipher.OrganizationId) ?? " "; CardCode = new Lazy(() => cipher.Card?.Code?.Decrypt(cipher.OrganizationId)); Icon = "card.png"; Subtitle = cardBrand; if(!string.IsNullOrWhiteSpace(CardNumber) && CardNumber.Length >= 4) { if(!string.IsNullOrWhiteSpace(CardNumber)) { Subtitle += ", "; } Subtitle += ("*" + CardNumber.Substring(CardNumber.Length - 4)); } break; case CipherType.Identity: var firstName = cipher.Identity?.FirstName?.Decrypt(cipher.OrganizationId) ?? " "; var lastName = cipher.Identity?.LastName?.Decrypt(cipher.OrganizationId) ?? " "; Icon = "id.png"; Subtitle = " "; if(!string.IsNullOrWhiteSpace(firstName)) { Subtitle = firstName; } if(!string.IsNullOrWhiteSpace(lastName)) { if(!string.IsNullOrWhiteSpace(Subtitle)) { Subtitle += " "; } Subtitle += lastName; } break; default: break; } } public string Id { get; set; } public bool Shared { get; set; } public bool HasAttachments { get; set; } public string FolderId { get; set; } public string Name { get; set; } public string Subtitle { get; set; } public CipherType Type { get; set; } public string Icon { get; set; } public string Image { get; set; } // Login metadata public string LoginUsername { get; set; } public Lazy LoginPassword { get; set; } public string LoginUri { get; set; } public Lazy LoginTotp { get; set; } // Login metadata public string CardNumber { get; set; } public Lazy CardCode { get; set; } } public class AutofillCipher : Cipher { public AutofillCipher(Models.Cipher cipher, bool fuzzy = false) : base(cipher) { Fuzzy = fuzzy; } public bool Fuzzy { get; set; } } public class Folder : List { public Folder(Models.Folder folder) { Id = folder.Id; Name = folder.Name?.Decrypt(); } public Folder(List ciphers) { AddRange(ciphers); } public string Id { get; set; } public string Name { get; set; } = AppResources.FolderNone; } public class AutofillGrouping : List { public AutofillGrouping(List logins, string name) { AddRange(logins); Name = name; } public string Name { get; set; } } } }