Graphley / app · graphley.ai / Sources / OwnerMetricsUI / PasskeyAuthorizer.swift
| 1 | import AuthenticationServices |
| 2 | import OwnerMetricsKit |
| 3 | #if os(iOS) |
| 4 | import UIKit |
| 5 | #else |
| 6 | import AppKit |
| 7 | #endif |
| 8 | |
| 9 | /// Runs the system passkey sheet for projectatom.ai (webcredentials associated domain). |
| 10 | @MainActor |
| 11 | final class PasskeyAuthorizer: NSObject, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding { |
| 12 | private var continuation: CheckedContinuation<ASAuthorization, Error>? |
| 13 | private var controller: ASAuthorizationController? |
| 14 | |
| 15 | func assertion(for challenge: PasskeyChallenge) async throws -> PasskeyAssertion { |
| 16 | let provider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: PasskeyClient.relyingParty) |
| 17 | let request = provider.createCredentialAssertionRequest(challenge: challenge.challenge) |
| 18 | request.userVerificationPreference = .required |
| 19 | guard let credential = try await perform(request).credential as? ASAuthorizationPlatformPublicKeyCredentialAssertion |
| 20 | else { throw MetricsError.passkeyRejected } |
| 21 | return PasskeyAssertion(credentialID: credential.credentialID, clientDataJSON: credential.rawClientDataJSON, |
| 22 | authenticatorData: credential.rawAuthenticatorData, signature: credential.signature, |
| 23 | userID: credential.userID) |
| 24 | } |
| 25 | |
| 26 | func registration(for challenge: PasskeyChallenge) async throws -> PasskeyRegistration { |
| 27 | let provider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: PasskeyClient.relyingParty) |
| 28 | let request = provider.createCredentialRegistrationRequest(challenge: challenge.challenge, name: challenge.userName, |
| 29 | userID: challenge.userID) |
| 30 | request.userVerificationPreference = .required |
| 31 | if #available(iOS 17.4, macOS 14.4, *) { |
| 32 | request.excludedCredentials = challenge.excludedCredentialIDs.map { |
| 33 | ASAuthorizationPlatformPublicKeyCredentialDescriptor(credentialID: $0) |
| 34 | } |
| 35 | } |
| 36 | guard let credential = try await perform(request).credential as? ASAuthorizationPlatformPublicKeyCredentialRegistration, |
| 37 | let attestation = credential.rawAttestationObject else { throw MetricsError.passkeyRejected } |
| 38 | return PasskeyRegistration(credentialID: credential.credentialID, clientDataJSON: credential.rawClientDataJSON, |
| 39 | attestationObject: attestation) |
| 40 | } |
| 41 | |
| 42 | private func perform(_ request: ASAuthorizationRequest) async throws -> ASAuthorization { |
| 43 | guard continuation == nil else { throw CancellationError() } |
| 44 | return try await withCheckedThrowingContinuation { continuation in |
| 45 | self.continuation = continuation |
| 46 | let controller = ASAuthorizationController(authorizationRequests: [request]) |
| 47 | controller.delegate = self |
| 48 | controller.presentationContextProvider = self |
| 49 | self.controller = controller |
| 50 | controller.performRequests() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private func finish(_ result: Result<ASAuthorization, Error>) { |
| 55 | continuation?.resume(with: result) |
| 56 | continuation = nil |
| 57 | controller = nil |
| 58 | } |
| 59 | |
| 60 | func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { |
| 61 | finish(.success(authorization)) |
| 62 | } |
| 63 | |
| 64 | func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { |
| 65 | if (error as? ASAuthorizationError)?.code == .canceled { finish(.failure(CancellationError())) } |
| 66 | else { finish(.failure(MetricsError.passkeyRejected)) } |
| 67 | } |
| 68 | |
| 69 | func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor { |
| 70 | #if os(iOS) |
| 71 | UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene } |
| 72 | .flatMap(\.windows).first(where: \.isKeyWindow) ?? ASPresentationAnchor() |
| 73 | #else |
| 74 | NSApp.keyWindow ?? NSApp.windows.first ?? ASPresentationAnchor() |
| 75 | #endif |
| 76 | } |
| 77 | } |