|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the WebAuthn Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 the WebAuthn Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of WebAuthn Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Crypto |
| 16 | + |
| 17 | +public struct AssertionAuthenticationRequest { |
| 18 | + public var options: PublicKeyCredentialRequestOptions |
| 19 | + public var clientDataHash: SHA256Digest |
| 20 | + public var attemptAuthentication: Callback |
| 21 | + |
| 22 | + init( |
| 23 | + options: PublicKeyCredentialRequestOptions, |
| 24 | + clientDataHash: SHA256Digest, |
| 25 | + attemptAuthentication: @escaping (_ assertionResults: Results) async throws -> () |
| 26 | + ) { |
| 27 | + self.options = options |
| 28 | + self.clientDataHash = clientDataHash |
| 29 | + self.attemptAuthentication = Callback(callback: attemptAuthentication) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +extension AssertionAuthenticationRequest { |
| 34 | + public struct Callback { |
| 35 | + /// The internal callback the attestation should call. |
| 36 | + var callback: (_ assertionResults: Results) async throws -> () |
| 37 | + |
| 38 | + /// Submit the results of asserting a user's authentication request. |
| 39 | + /// |
| 40 | + /// Authenticators should call this to submit a successful authentication and cancel any other pending authenticators. |
| 41 | + /// |
| 42 | + /// - SeeAlso: https://w3c.github.io/webauthn/#sctn-generating-an-attestation-object |
| 43 | + public func submitAssertionResults( |
| 44 | + credentialID: [UInt8], |
| 45 | + authenticatorData: [UInt8], |
| 46 | + signature: [UInt8], |
| 47 | + userHandle: [UInt8]?, |
| 48 | + authenticatorAttachment: AuthenticatorAttachment |
| 49 | + ) async throws { |
| 50 | + try await callback(Results( |
| 51 | + credentialID: credentialID, |
| 52 | + authenticatorData: authenticatorData, |
| 53 | + signature: signature, |
| 54 | + userHandle: userHandle, |
| 55 | + authenticatorAttachment: authenticatorAttachment |
| 56 | + )) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +extension AssertionAuthenticationRequest { |
| 62 | + struct Results { |
| 63 | + var credentialID: [UInt8] |
| 64 | + var authenticatorData: [UInt8] |
| 65 | + var signature: [UInt8] |
| 66 | + var userHandle: [UInt8]? |
| 67 | + var authenticatorAttachment: AuthenticatorAttachment |
| 68 | + } |
| 69 | +} |
0 commit comments