Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

3.5 KB
Raw Blame
import Foundation

/// AutofillCredential is a purpose-specific model for iOS Autofill functionality.
/// It provides a simplified view of Item data optimized for password/passkey autofill.
/// This replaces the legacy Credential model which was based on the old data structure.
public struct AutofillCredential: Codable, Hashable, Equatable {
    public let id: UUID
    public let manifestId: String
    public let serviceName: String?
    public let serviceUrls: [String]
    public let logo: Data?
    public let username: String?
    public let email: String?
    public let password: String?
    public let notes: String?
    public let passkey: Passkey?
    public let totp: TotpCode?
    public let createdAt: Date
    public let updatedAt: Date

    /// The Base32 secret of the item's TOTP code, if it has one.
    public var totpSecret: String? {
        return totp?.secretKey
    }

    public init(
        id: UUID,
        manifestId: String,
        serviceName: String?,
        serviceUrls: [String],
        logo: Data?,
        username: String?,
        email: String?,
        password: String?,
        notes: String?,
        passkey: Passkey?,
        totp: TotpCode? = nil,
        createdAt: Date,
        updatedAt: Date
    ) {
        self.id = id
        self.manifestId = manifestId
        self.serviceName = serviceName
        self.serviceUrls = serviceUrls
        self.logo = logo
        self.username = username
        self.email = email
        self.password = password
        self.notes = notes
        self.passkey = passkey
        self.totp = totp
        self.createdAt = createdAt
        self.updatedAt = updatedAt
    }

    /// Initialize from an Item model.
    /// - Parameters:
    ///   - item: The Item to convert from
    ///   - passkey: Optional passkey associated with this item
    ///   - totp: Optional TOTP code for this item
    public init(from item: Item, passkey: Passkey? = nil, totp: TotpCode? = nil) {
        self.id = item.id
        self.manifestId = item.manifestId
        self.serviceName = item.name
        self.serviceUrls = item.urls
        self.logo = item.logo
        self.username = item.username
        self.email = item.email
        self.password = item.password
        self.notes = item.getFieldValue(FieldKey.notesContent)
        self.passkey = passkey
        self.totp = totp
        self.createdAt = item.createdAt
        self.updatedAt = item.updatedAt
    }

    public static func == (lhs: AutofillCredential, rhs: AutofillCredential) -> Bool {
        return lhs.id == rhs.id
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }

    // MARK: - Autofill Helpers

    /// Returns username or email, prioritizing username.
    /// Used for autofill identifier display and form filling.
    public var identifier: String {
        if let username = username, !username.isEmpty {
            return username
        }
        if let email = email, !email.isEmpty {
            return email
        }
        return ""
    }

    /// Returns true if this credential has a password value.
    public var hasPassword: Bool {
        guard let password = password else { return false }
        return !password.isEmpty
    }

    /// Returns true if this credential has a passkey.
    public var hasPasskey: Bool {
        return passkey != nil
    }

    /// Returns true if this credential has a TOTP secret.
    public var hasTotp: Bool {
        guard let secret = totpSecret else { return false }
        return !secret.isEmpty
    }
}