Skip to content

Commit 233870a

Browse files
Added AssertionAuthenticationRequest to assist with authentication assertions
1 parent 37bd51c commit 233870a

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

Sources/WebAuthn/Authenticators/Protocol/AuthenticatorProtocol.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import Crypto
1616
import SwiftCBOR
1717

18+
public typealias CredentialStore<A: AuthenticatorProtocol> = [A.CredentialSource.ID : A.CredentialSource]
19+
1820
public protocol AuthenticatorProtocol<CredentialSource> {
1921
associatedtype CredentialSource: AuthenticatorCredentialSourceProtocol
2022

@@ -90,6 +92,21 @@ public protocol AuthenticatorProtocol<CredentialSource> {
9092
requiresUserPresence: Bool,
9193
credentialOptions: [CredentialSource]
9294
) async throws -> CredentialSource
95+
96+
/// Request that an authenticator assert one of the specified credentials.
97+
///
98+
/// - Note: If the authenticator fails, other authenticators should continue until either one succeeds, or the parent task is cancelled.
99+
///
100+
/// - SeeAlso: [WebAuthn Level 3 Editor's Draft §5.1.4.2. Issuing a Credential Request to an Authenticator](https://w3c.github.io/webauthn/#sctn-issuing-cred-request-to-authenticator)
101+
/// - SeeAlso: [WebAuthn Level 3 Editor's Draft §6.3.3. The authenticatorGetAssertion Operation](https://w3c.github.io/webauthn/#authenticatorgetassertion)
102+
/// - Parameters:
103+
/// - authenticationRequest: The authentication request from the relying party.
104+
/// - credentials: The set of credentials the authenticator should match against.
105+
/// - Returns: An updated credential source upon successful authentication.
106+
func assertCredentials(
107+
authenticationRequest: AssertionAuthenticationRequest,
108+
credentials: CredentialStore<Self>
109+
) async throws -> CredentialSource
93110
}
94111

95112
// MARK: - Default Implementations
@@ -320,3 +337,14 @@ extension AuthenticatorProtocol {
320337
return credentialSource
321338
}
322339
}
340+
341+
// MARK: Authentication
342+
343+
extension AuthenticatorProtocol {
344+
public func assertCredentials(
345+
authenticationRequest: AssertionAuthenticationRequest,
346+
credentials: CredentialStore<Self>
347+
) async throws -> CredentialSource {
348+
throw WebAuthnError.unsupported
349+
}
350+
}

0 commit comments

Comments
 (0)