Cotton code
76 lines · 3.3 KB · SwiftRaw
// THE PHONE'S OWN LOCK IN FRONT OF THE VAULT KEY (e2e rulings R7, R8; 16 Nov// 2026). The vault key's Keychain item carries no access control any more// (`VaultKey.swift`), so the prompt a person meets before a reveal, an Approve// or an Allow is this one: `LAContext`'s `.deviceOwnerAuthentication`, Face ID// or Touch ID with the passcode behind it, asked by `VaultKeyring.privateKey()`.//// Three rules://   * **Never a wall.** A phone with no passcode (and a simulator out of the//     box) cannot be asked; the act runs, as Android's does on a phone with no//     lock (R7: nothing ever blocks).//   * **A cancel is a decision.** It is `VaultKeyError.cancelled`, drawn as//     nothing, and never retried.//   * **Never in a test process**, whose simulator may have a face enrolled//     and nobody to look at it. The Mac asks at the screen (`VaultAccess`).//// It blocks the calling thread while the sheet stands, as the guarded// Keychain read did: every caller already reads off the main thread.import CottonValuesimport Foundation#if os(iOS)    import LocalAuthentication#endifpublic enum VaultPresence {    /// The sentence above the sheet, the kit's line. Said in no app's words:    /// every app asks it before Approve or a recovery code, and "Unlock your    /// passwords" in Words, Days or Story read as another app's (launch-ios,    /// 22 Nov 2026). The passwords app's own reveal says its own line.    public static var reason: String { ShellLines.line("passwords", "vault.confirm_its_you") }    /// Ask the phone's own lock; throws ``VaultKeyError/cancelled`` when the    /// person said no.    public static func ask() throws {        #if os(iOS)            if KeychainGate.isTestProcess { return }            let context = LAContext()            var problem: NSError?            // No passcode set: nothing to ask, and nothing blocks.            guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &problem) else { return }            let answered = DispatchSemaphore(value: 0)            let outcome = Outcome()            context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { allowed, error in                outcome.set(allowed: allowed, error as? LAError)                answered.signal()            }            answered.wait()            if let refused = outcome.refused {                switch refused.code {                case .passcodeNotSet, .biometryNotEnrolled, .biometryNotAvailable: return                default: throw VaultKeyError.cancelled                }            }            if outcome.failed { throw VaultKeyError.cancelled }        #endif    }    #if os(iOS)        private final class Outcome: @unchecked Sendable {            private let lock = NSLock()            private var error: LAError?            private var done = false            private var allowed = false            func set(allowed: Bool, _ refused: LAError?) {                lock.withLock {                    error = allowed ? nil : refused                    self.allowed = allowed                    done = true                }            }            var refused: LAError? { lock.withLock { error } }            /// Refused with no error to read.            var failed: Bool { lock.withLock { done && !allowed && error == nil } }        }    #endif}