Repository Public
1// SP Wallet Core2// File: Sources/SPWalletCore/Auth/WebAuthn.swift3// Description: WebAuthn JSON as the backend exchanges it (SimpleWebAuthn format, unpadded base64url), and the protocol the apps4// implement with AuthenticationServices. The Core never sees a private key: an authenticator returns signed assertions.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910public struct CredentialDescriptor: Codable, Sendable, Equatable {11 public let id: String12 public let type: String?13 public let transports: [String]?14 public init(id: String, type: String? = "public-key", transports: [String]? = nil) { self.id = id; self.type = type; self.transports = transports }15 /** Raw credential id. */16 public var idData: Data? { Data(base64URL: id) }17}1819/** `PublicKeyCredentialRequestOptionsJSON`: what the server asks an existing passkey to sign. */20public struct PasskeyRequestOptions: Codable, Sendable, Equatable {21 public let challenge: String22 public let rpId: String?23 public let timeout: Double?24 public let userVerification: String?25 public let allowCredentials: [CredentialDescriptor]?2627 public init(challenge: String, rpId: String?, timeout: Double? = nil, userVerification: String? = "required", allowCredentials: [CredentialDescriptor]? = nil) {28 self.challenge = challenge; self.rpId = rpId; self.timeout = timeout; self.userVerification = userVerification; self.allowCredentials = allowCredentials29 }30 public var challengeData: Data? { Data(base64URL: challenge) }31}3233/** `PublicKeyCredentialCreationOptionsJSON`: what the server asks a new passkey to be created with. */34public struct PasskeyCreationOptions: Codable, Sendable, Equatable {35 public struct RelyingParty: Codable, Sendable, Equatable { public let id: String?; public let name: String }36 public struct User: Codable, Sendable, Equatable { public let id: String; public let name: String; public let displayName: String }37 public struct Parameter: Codable, Sendable, Equatable { public let type: String; public let alg: Int }38 public struct Selection: Codable, Sendable, Equatable { public let residentKey: String?; public let userVerification: String?; public let requireResidentKey: Bool?; public let authenticatorAttachment: String? }39 public let challenge: String40 public let rp: RelyingParty41 public let user: User42 public let pubKeyCredParams: [Parameter]?43 public let timeout: Double?44 public let excludeCredentials: [CredentialDescriptor]?45 public let authenticatorSelection: Selection?46 public let attestation: String?47 public var challengeData: Data? { Data(base64URL: challenge) }48 public var userIdData: Data? { Data(base64URL: user.id) }49}5051/** `AuthenticationResponseJSON`: a passkey's signature over (authenticatorData ‖ SHA-256(clientDataJSON)). */52public struct PasskeyAssertion: Codable, Sendable, Equatable {53 public struct Response: Codable, Sendable, Equatable {54 public let clientDataJSON: String55 public let authenticatorData: String56 public let signature: String57 public let userHandle: String?58 }59 public let id: String60 public let rawId: String61 public let type: String62 public let response: Response63 public let authenticatorAttachment: String?64 public let clientExtensionResults: [String: JSONValue]6566 public init(credentialId: Data, clientDataJSON: Data, authenticatorData: Data, signature: Data, userHandle: Data?, attachment: String? = "platform") {67 id = credentialId.base64URL; rawId = credentialId.base64URL; type = "public-key"68 response = Response(clientDataJSON: clientDataJSON.base64URL, authenticatorData: authenticatorData.base64URL, signature: signature.base64URL,69 userHandle: userHandle.flatMap { $0.isEmpty ? nil : $0.base64URL })70 authenticatorAttachment = attachment; clientExtensionResults = [:]71 }72}7374/** `RegistrationResponseJSON`: a new passkey's attestation. */75public struct PasskeyRegistration: Codable, Sendable, Equatable {76 public struct Response: Codable, Sendable, Equatable {77 public let clientDataJSON: String78 public let attestationObject: String79 public let transports: [String]?80 }81 public let id: String82 public let rawId: String83 public let type: String84 public let response: Response85 public let authenticatorAttachment: String?86 public let clientExtensionResults: [String: JSONValue]8788 public init(credentialId: Data, clientDataJSON: Data, attestationObject: Data, transports: [String]? = ["internal", "hybrid"], attachment: String? = "platform") {89 id = credentialId.base64URL; rawId = credentialId.base64URL; type = "public-key"90 response = Response(clientDataJSON: clientDataJSON.base64URL, attestationObject: attestationObject.base64URL, transports: transports)91 authenticatorAttachment = attachment; clientExtensionResults = [:]92 }93}9495public enum PasskeyError: Error, Sendable, Equatable {96 /** The person dismissed the system sheet. Nothing was signed. */97 case cancelled98 /** No passkey of this wallet is available on this device or nearby. */99 case noCredential100 /** The device cannot use passkeys for this domain (associated domain not validated, passcode off, …). */101 case notAvailable(String)102 case failed(String)103}104105/**106 The device's passkey provider. The apps implement it with `ASAuthorizationPlatformPublicKeyCredentialProvider`; tests use a software107 authenticator against a throwaway stack. The assertion is the approval: a local Face ID success alone approves nothing.108 */109public protocol PasskeyAuthenticator: Sendable {110 func assert(_ options: PasskeyRequestOptions, rpId: String) async throws -> PasskeyAssertion111 func register(_ options: PasskeyCreationOptions, rpId: String) async throws -> PasskeyRegistration112}113