// A code-hosting token left in this phone's Keychain by an old build, and the // one thing left to do about it. // // Access tokens live in `cotton_secrets` now — sealed under the workspace key, // so the Mac that serves you can read a repository while this phone is asleep // — and nothing reads the slots the old build wrote. This is the // `ByokLegacyKeychain` shape, for the same reason and with the same shape of // argument: what is being cleaned up is a SECRET rather than a feature, so a // forget and nothing else. // // **THE SLOTS CANNOT BE ENUMERATED, so the INDEX is read first.** The old store // kept one slot per host (`cotton.codehost.`) and an index slot naming // which hosts it held, precisely because the Keychain answers no listing. The // index is what makes a complete forget possible; a corrupt one costs // completeness, not safety, so the index slot is deleted whatever happened to // its contents. // // There is no read and no migration into `cotton_secrets`: the value would // have to cross the boundary again, and a person can paste a token once. import CottonKit import Foundation public enum CodeHostingLegacyKeychain { /// The slot the old store listed its hosts in. public static let indexSlot = "cotton.codehost.index" /// The slot one host's token sat in. public static func slot(for host: String) -> String { "cotton.codehost.\(host.lowercased())" } /// The hosts the index names, or none when it cannot be read. static func indexedHosts(in store: any SecretStore) -> [String] { guard let raw = store.read(indexSlot), let data = raw.data(using: .utf8), let parsed = try? JSONSerialization.jsonObject(with: data) as? [Any] else { return [] } return parsed.compactMap { $0 as? String } } /// Delete every slot the index names, and the index. Idempotent, silent, /// and safe to call on every launch — `SecItemDelete` on a slot that is not /// there is not an error. /// /// Off the main thread: `SecretStore.swift`'s header refuses a Keychain /// call on it, because a SecurityAgent prompt blocks the whole app. public static func forget(in store: any SecretStore = KeychainSecretStore()) async { await Task.detached(priority: .utility) { for host in indexedHosts(in: store) { store.forget(slot(for: host)) } store.forget(indexSlot) } .value } }