Cotton code
130 lines · 5.5 KB · SwiftRaw
// The passkey channel: what Cotton Keys' credential provider tells the app.//// The SECOND KIND of mailbox, in the same spelling as the share channel// (`ShareMailbox`, one App Group `UserDefaults` key, raw-JSON append, drained// whole), under its own key so neither drain ever takes the other's entries:// Cotton Keys takes no shares, and a share drain must never swallow a// passkey. The provider has no core and no database (it is the share// extension's shape), so it writes one entry and knocks; the app drains:////   passkey.registered {rp_id, rp_name, user_name, user_handle, credential_id,//                       algorithm, public_key (COSE, base64url), created_at}//   passkey.used       {credential_id, used_at}//// No key material ever rides here: the private half is the shared keychain// group's (`PasskeyKeychain`), read by the app for the one call that seals it.//// THE KNOCK IS A DARWIN NOTIFICATION, not a URL: a credential provider cannot// open its app. A running Keys drains at once; otherwise the next foreground// drains. An entry the app cannot apply yet (the vault is locked, the row it// counts a use for is not written yet) is put back to wait.import Foundation/// The channel's names.public enum PasskeyChannel {    /// The App Group `UserDefaults` key. Wire protocol, like the share key.    public static let mailboxKey = "cotton.passkey.mailbox.v1"    /// The two kinds an entry is.    public static let registered = "passkey.registered"    public static let used = "passkey.used"    /// The Darwin notification the provider posts after it writes an entry,    /// named by the app group so Keys and Keys Dev never hear each other.    public static func knockName(bundleIdentifier: String? = Bundle.main.bundleIdentifier) -> String {        CottonDefaults.appGroup(bundleIdentifier: bundleIdentifier) + ".passkey.knock"    }    /// Knock: tell a running app an entry is waiting.    public static func knock(bundleIdentifier: String? = Bundle.main.bundleIdentifier) {        let name = CFNotificationName(knockName(bundleIdentifier: bundleIdentifier) as CFString)        CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), name, nil, nil, true)    }}/// One thing the provider did, as it wrote it down.public struct PasskeyEntry: Codable, Equatable, Sendable {    public var id: String    /// When the provider wrote it, ISO-8601.    public var at: String    /// ``PasskeyChannel/registered`` or ``PasskeyChannel/used``.    public var kind: String    public var credentialId: String    public var rpId: String?    public var rpName: String?    public var userName: String?    public var userHandle: String?    public var algorithm: String?    public var publicKey: String?    public var createdAt: String?    public var usedAt: String?    public init(        id: String = UUID().uuidString, at: String, kind: String, credentialId: String,        rpId: String? = nil, rpName: String? = nil, userName: String? = nil, userHandle: String? = nil,        algorithm: String? = nil, publicKey: String? = nil, createdAt: String? = nil, usedAt: String? = nil    ) {        self.id = id        self.at = at        self.kind = kind        self.credentialId = credentialId        self.rpId = rpId        self.rpName = rpName        self.userName = userName        self.userHandle = userHandle        self.algorithm = algorithm        self.publicKey = publicKey        self.createdAt = createdAt        self.usedAt = usedAt    }    private enum CodingKeys: String, CodingKey {        case id, at, kind        case credentialId = "credential_id"        case rpId = "rp_id"        case rpName = "rp_name"        case userName = "user_name"        case userHandle = "user_handle"        case algorithm        case publicKey = "public_key"        case createdAt = "created_at"        case usedAt = "used_at"    }    /// The columns this entry carries, as `passkey.register` / `passkey.used`    /// take them (the core's snake keys).    public var arguments: [String: String] {        var out: [String: String] = ["credential_id": credentialId]        for (key, value) in [            ("rp_id", rpId), ("rp_name", rpName), ("user_name", userName), ("user_handle", userHandle),            ("algorithm", algorithm), ("public_key", publicKey), ("created_at", createdAt), ("used_at", usedAt),        ] {            if let value { out[key] = value }        }        return out    }}/// The passkey mailbox: ``ShareMailbox`` under ``PasskeyChannel/mailboxKey``.public struct PasskeyMailbox: Sendable {    private let mailbox: ShareMailbox    public init(storage: any ShareMailboxStorage = DefaultsShareMailboxStorage()) {        mailbox = ShareMailbox(storage: storage, key: PasskeyChannel.mailboxKey)    }    public func append(_ entry: PasskeyEntry) { mailbox.append(entry) }    /// Take everything waiting, removing the list (``ShareMailbox/drain``).    public func drain() -> [PasskeyEntry] { mailbox.drain(PasskeyEntry.self) }    /// Put entries back to wait for a later drain (the vault was locked, the    /// row they count for is not written yet). Unbounded by design: a passkey    /// waits for its vault however long that takes, and the deck says so.    public func wait(_ entries: [PasskeyEntry]) {        for entry in entries { mailbox.append(entry) }    }    /// What is waiting, without taking it (the deck's line counts it).    public func waiting() -> [PasskeyEntry] { mailbox.peek(PasskeyEntry.self) }}