SPB Git

Repository Public

PasskeyPresenter.swift 6.2 KB 122 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Drive pour macOS9// File             : App/PasskeyPresenter.swift10// Description      : Presents the system passkey sheet (Touch ID, iCloud11//                    Keychain, security key) for a challenge of the server and12//                    returns the signed assertion. Nothing biometric leaves the13//                    Mac.14// Module           : App15// Repository       : https://git.spboucher.ai/sp-drive-macos16// Created          : 2026-11-1817// Last Modified    : 2026-11-1818//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author           : Simon-Pierre Boucher24// Position         : Professor of Finance25// Institution      : Université du Québec en Outaouais (UQO)26// Department       : Département des sciences administratives27// Organization     : SP Boucher28//29// Website          : https://spboucher.ai30// Git              : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General          : info@spboucher.ai37// Professional     : contact@spboucher.ai38// Direct           : simon@spboucher.ai39// Research         : research@spboucher.ai40// Development      : dev@spboucher.ai41// Support          : support@spboucher.ai42// Security/Admin   : admin@spboucher.ai43// Notifications    : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification   : Public70// Distribution     : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License          : None specified (no license file in this repository)84// Maintainer       : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import AppKit92import AuthenticationServices93import SPDriveCore9495@MainActor final class PasskeyPresenter: NSObject, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {96    private var continuation: CheckedContinuation<PasskeyAssertion, Error>?97    private var controller: ASAuthorizationController?9899    /// Asks the person to confirm with a passkey of `drive.spboucher.ai`. Throws `.cancelled` when the sheet is dismissed.100    func assert(_ challenge: PasskeyChallenge) async throws -> PasskeyAssertion {101        let provider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: challenge.relyingParty)102        let request = provider.createCredentialAssertionRequest(challenge: challenge.challenge)103        request.userVerificationPreference = .required104        request.allowedCredentials = challenge.allowedCredentials.map { ASAuthorizationPlatformPublicKeyCredentialDescriptor(credentialID: $0) }105        let c = ASAuthorizationController(authorizationRequests: [request])106        c.delegate = self; c.presentationContextProvider = self; controller = c107        return try await withCheckedThrowingContinuation { cont in continuation = cont; c.performRequests() }108    }109    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor { NSApp.keyWindow ?? NSApp.windows.first ?? ASPresentationAnchor() }110    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {111        defer { continuation = nil; self.controller = nil }112        guard let a = authorization.credential as? ASAuthorizationPlatformPublicKeyCredentialAssertion else { continuation?.resume(throwing: DriveError.local("réponse de passkey inattendue")); return }113        continuation?.resume(returning: PasskeyAssertion(credentialID: a.credentialID, clientDataJSON: a.rawClientDataJSON, authenticatorData: a.rawAuthenticatorData, signature: a.signature, userHandle: a.userID))114    }115    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {116        defer { continuation = nil; self.controller = nil }117        let ns = error as NSError118        if ns.domain == ASAuthorizationError.errorDomain, ns.code == ASAuthorizationError.canceled.rawValue { continuation?.resume(throwing: DriveError.cancelled) }119        else { continuation?.resume(throwing: DriveError.local("Passkey : \(ns.localizedDescription) (\(ns.code))")) }120    }121}122
Files main
Menu

Theme