Cotton code
142 lines · 5.4 KB · SwiftRaw
// A refusal the gate stands on, as the shell draws it.//// The core decides every refusal it can see (`shell.home`'s gate: its code,// its title, its body, its reason, its ways out and where it is drawn) and// this type carries its words to the banner and the slip// (`init(gate:)`). The three it cannot see are the Keychain's, at launch,// before any core is open (`forKeyRead`): those are the only words written// here.import CottonKitimport Foundationimport Security/// A refusal's one way out beyond Try again.public enum RefusalWayOut: String, Equatable, Sendable {    case signOut = "sign_out"    case resetKey = "reset_key"    case none}public struct ServerRefusal: Equatable, Sendable {    public let code: String    public let title: String    public let body: String    public let reason: String?    public let retryable: Bool    public let wayOut: RefusalWayOut    /// The core's ways out, in order, when the core answered this refusal.    public let waysOut: [HomeAnswer.WayOut]    public let erase: Bool    public init(        code: String,        title: String,        body: String,        reason: String? = nil,        retryable: Bool,        wayOut: RefusalWayOut,        waysOut: [HomeAnswer.WayOut] = [],        erase: Bool = false    ) {        self.code = code        self.title = title        self.body = body        self.reason = reason        self.retryable = retryable        self.wayOut = wayOut        self.waysOut = waysOut        self.erase = erase    }    /// The refusal the core's gate stands on, in its words; `nil` when none.    public init?(gate: HomeAnswer.Gate) {        guard let code = gate.code else { return nil }        let ids = gate.ways_out.map(\.id)        self.init(            code: code,            title: gate.words.title ?? "",            body: gate.words.body ?? "",            reason: gate.words.reason,            retryable: ids.contains("retry"),            wayOut: ids.contains("reset_key") ? .resetKey : ids.contains("sign_out") ? .signOut : .none,            waysOut: gate.ways_out,            erase: gate.erase        )    }    /// The Keychain's three, which no core can answer: it is not open yet.    public static func of(code: String) -> ServerRefusal? {        switch code {        case "device_key_unreadable", "no_device_key":            return ServerRefusal(                code: code,                title: CottonStrings.refusalKeyUnreadableTitle,                body: CottonStrings.refusalKeyUnreadableBody,                reason: CottonStrings.refusalReasonKeyUnreadable,                retryable: false,                wayOut: .resetKey            )        case "keychain_unavailable":            return ServerRefusal(                code: code,                title: CottonStrings.refusalKeychainUnavailableTitle,                body: CottonStrings.refusalKeychainUnavailableBody,                reason: CottonStrings.refusalReasonKeychain,                retryable: true,                wayOut: .none            )        case "keychain_locked":            return ServerRefusal(                code: code,                title: CottonStrings.refusalKeychainLockedTitle,                body: CottonStrings.refusalKeychainLockedBody,                reason: CottonStrings.refusalReasonKeychainLocked,                retryable: true,                wayOut: .none            )        default:            return nil        }    }    /// The refusal a failed Keychain read at launch paints, before any core    /// is open.    ///    /// **Three states, and "Start over" only fixes one of them.** A slot that is    /// present and unreadable is `device_key_unreadable`, and forgetting it    /// lets the next open mint a key. But a build the Keychain refuses    /// outright — `errSecMissingEntitlement`, which every simulator build made    /// with `CODE_SIGNING_ALLOWED=NO` answers to every read, write and delete —    /// has no slot to forget: the delete fails, the re-open fails the same way,    /// and the screen came back exactly as it was. That was the reported "Start    /// over does nothing". It is `keychain_unavailable`, with Retry and no    /// button that promises what it cannot do.    ///    /// And a Keychain that answers `errSecInteractionNotAllowed` is not    /// refusing anything for good: the key is kept "after first unlock", and    /// the app was opened before the phone had been unlocked since it started    /// (a notification, a background wake). Starting over there    /// would throw away a perfectly good identity. It is `keychain_locked`,    /// with Retry — and the app retries by itself when the phone unlocks.    public static func forKeyRead(_ error: Error) -> ServerRefusal {        let status: OSStatus?        switch error as? DeviceKeyError {        case .storeUnreadable(let answered), .notWritten(let answered):            status = answered        default:            status = nil        }        // One sentence per code, so the table in `of(code:)` is the only copy.        // Each unwrap names a code that table declares; `GateSlipTests` runs        // every branch, so a code dropped from the table fails there first.        switch status {        case errSecMissingEntitlement:            return of(code: "keychain_unavailable")!        case errSecInteractionNotAllowed:            return of(code: "keychain_locked")!        default:            return of(code: "device_key_unreadable")!        }    }}