// The password channel: what Cotton Keys' credential provider tells the app // about passwords. // // The THIRD KIND of mailbox, in the share channel's spelling (`ShareMailbox`, // one App Group `UserDefaults` key, raw-JSON append, drained whole), under // its own key so the passkey drain never takes a password entry (it drops a // kind it does not know) and this one never takes a passkey's: // // password.used {password_id, used_at} a fill, to stamp last_used_at // password.saved {pending_id, site, username, a site's form handed a login // title} to save (iOS 26.2) // // NO PASSWORD EVER RIDES HERE: a saved password waits in the shared keychain // group's pending store (`PasswordKeychain.pending()`) under `pending_id`, // read by the app for the one `password.save` call that seals it, then // removed. The knock is the passkey channel's (`PasskeyChannel.knock`): one // Darwin notification wakes Keys for both drains. import Foundation /// The channel's names. public enum PasswordChannel { /// The App Group `UserDefaults` key. Wire protocol, like the share key. public static let mailboxKey = "cotton.password.mailbox.v1" /// The two kinds an entry is. public static let used = "password.used" public static let saved = "password.saved" } /// One thing the provider did with a password, as it wrote it down. public struct PasswordEntry: Codable, Equatable, Sendable { public var id: String /// When the provider wrote it, ISO-8601. public var at: String /// ``PasswordChannel/used`` or ``PasswordChannel/saved``. public var kind: String /// The `secret` row a fill used. public var passwordId: String? public var usedAt: String? /// The pending keychain item a save's password waits in. public var pendingId: String? public var site: String? public var username: String? public var title: String? public init( id: String = UUID().uuidString, at: String, kind: String, passwordId: String? = nil, usedAt: String? = nil, pendingId: String? = nil, site: String? = nil, username: String? = nil, title: String? = nil ) { self.id = id self.at = at self.kind = kind self.passwordId = passwordId self.usedAt = usedAt self.pendingId = pendingId self.site = site self.username = username self.title = title } private enum CodingKeys: String, CodingKey { case id, at, kind, site, username, title case passwordId = "password_id" case usedAt = "used_at" case pendingId = "pending_id" } } /// The password mailbox: ``ShareMailbox`` under ``PasswordChannel/mailboxKey``. public struct PasswordMailbox: Sendable { private let mailbox: ShareMailbox public init(storage: any ShareMailboxStorage = DefaultsShareMailboxStorage()) { mailbox = ShareMailbox(storage: storage, key: PasswordChannel.mailboxKey) } public func append(_ entry: PasswordEntry) { mailbox.append(entry) } /// Take everything waiting, removing the list. public func drain() -> [PasswordEntry] { mailbox.drain(PasswordEntry.self) } /// Put entries back to wait for a later drain (the vault was locked). public func wait(_ entries: [PasswordEntry]) { for entry in entries { mailbox.append(entry) } } /// What is waiting, without taking it. public func waiting() -> [PasswordEntry] { mailbox.peek(PasswordEntry.self) } }