Cotton code
46 lines · 2.2 KB · SwiftRaw
// WHETHER THIS PHONE AND THIS APP CAN DO A COTTON ACCOUNT PASSKEY (lane// passkeys-ios, 19 Nov 2026). The sheet takes a passkey for the sign-in host// only when the app is signed with `webcredentials:<host>` and the phone has a// passcode; otherwise it fails after the tap, so the sign-in screen and// Account's passkeys card offer a passkey only when both hold.//// The app cannot read its own entitlements on iOS, so the spec gate stamps// the host it signed the app for into Info.plist (`CottonPasskeyHost`, the// same `$(COTTON_SIGNIN_HOST)` the entitlement says; `scripts/ios-spec-gate.ts`).// A parked or planned app carries neither, and offers no passkey.import Foundation#if canImport(LocalAuthentication)    import LocalAuthentication#endifpublic enum PasskeyPlatform {    /// The Info.plist key the spec gate stamps with the passkey host.    public static let hostKey = "CottonPasskeyHost"    /// A passkey is offered when the app is signed for exactly the host it    /// signs in at (`server`, an https URL) and the phone has a passcode.    public static func offers(stampedHost: String?, server: String, passcodeSet: Bool) -> Bool {        guard passcodeSet else { return false }        guard let stamped = stampedHost?.trimmingCharacters(in: .whitespaces).lowercased(), !stamped.isEmpty,            !stamped.contains("$(")        else { return false }        guard let url = URL(string: server), url.scheme == "https", let host = url.host?.lowercased() else { return false }        return host == stamped    }    /// `offers` for this app, this phone, and the server it signs in at.    public static func offersHere(server: String) -> Bool {        offers(stampedHost: Bundle.main.object(forInfoDictionaryKey: hostKey) as? String, server: server, passcodeSet: passcodeSet())    }    /// Whether the phone has a passcode (or Face ID, which needs one): a    /// platform passkey asks for one, and a phone without cannot keep one.    static func passcodeSet() -> Bool {        #if canImport(LocalAuthentication)            return LAContext().canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)        #else            return false        #endif    }}