Skip to content

Commit d4fa8c8

Browse files
committed
add coments and refactor WebAuthnConfig
1 parent ff58bbd commit d4fa8c8

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

Sources/WebAuthn/WebAuthnConfig.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@ public struct WebAuthnConfig {
2121
/// For example, if the origin is https://login.example.com:1337, then _login.example.com_ or _example.com_ are
2222
/// valid ids, but not _m.login.example.com_ and not _com_.
2323
public let relyingPartyID: String
24+
/// The domain, with HTTP protocol (e.g. "https://example.com")
2425
public let relyingPartyOrigin: String
25-
public let timeout: TimeInterval
2626

2727
public init(
2828
relyingPartyDisplayName: String,
2929
relyingPartyID: String,
30-
relyingPartyOrigin: String,
31-
timeout: TimeInterval
30+
relyingPartyOrigin: String
3231
) {
3332
self.relyingPartyDisplayName = relyingPartyDisplayName
3433
self.relyingPartyID = relyingPartyID
3534
self.relyingPartyOrigin = relyingPartyOrigin
36-
self.timeout = timeout
3735
}
3836
}

Sources/WebAuthn/WebAuthnManager.swift

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Crypto
1615
import Foundation
17-
import Logging
18-
import SwiftCBOR
1916

2017
public struct WebAuthnManager {
2118
private let config: WebAuthnConfig
@@ -28,8 +25,16 @@ public struct WebAuthnManager {
2825
}
2926

3027
/// Generate a new set of registration data to be sent to the client and authenticator.
28+
///
29+
/// - Parameters:
30+
/// - user: The user to register.
31+
/// - attestation: The level of attestation to be provided by the authenticator.
32+
/// - publicKeyCredentialParameters: A list of public key algorithms the Relying Party chooses to restrict
33+
/// support to. Defaults to all supported algorithms.
34+
/// - Returns: Registration options ready for the browser.
3135
public func beginRegistration(
3236
user: WebAuthnUser,
37+
timeout: TimeInterval = 60000,
3338
attestation: AttestationConveyancePreference = .none,
3439
publicKeyCredentialParameters: [PublicKeyCredentialParameters] = PublicKeyCredentialParameters.supported
3540
) throws -> PublicKeyCredentialCreationOptions {
@@ -47,17 +52,25 @@ public struct WebAuthnManager {
4752
user: userEntity,
4853
rp: relyingParty,
4954
pubKeyCredParams: publicKeyCredentialParameters,
50-
timeout: config.timeout,
55+
timeout: timeout,
5156
attestation: attestation
5257
)
5358
}
5459

5560
/// Take response from authenticator and client and verify credential against the user's credentials and
5661
/// session data.
62+
///
5763
/// - Parameters:
58-
/// - challenge: The user to verify against the authenticator response. Base64 encoded.
64+
/// - challenge: The challenge passed to the authenticator within the preceding registration options.
5965
/// - credentialCreationData: The value returned from `navigator.credentials.create()`
6066
/// - requireUserVerification: Whether or not to require that the authenticator verified the user.
67+
/// - supportedPublicKeyAlgorithms: A list of public key algorithms the Relying Party chooses to restrict
68+
/// support to. Defaults to all supported algorithms.
69+
/// - pemRootCertificatesByFormat: A list of root certificates used for attestation verification.
70+
/// If attestation verification is not required (default behavior) this parameter does nothing.
71+
/// - confirmCredentialIDNotRegisteredYet: For a successful registration ceremony we need to verify that the
72+
/// `credentialId`, generated by the authenticator, is not yet registered for any user. This is a good place to
73+
/// handle that.
6174
/// - Returns: A new `Credential` with information about the authenticator and registration
6275
public func finishRegistration(
6376
challenge: EncodedBase64,
@@ -97,9 +110,20 @@ public struct WebAuthnManager {
97110
)
98111
}
99112

113+
/// Generate options for retrieving a credential via navigator.credentials.get()
114+
///
115+
/// - Parameters:
116+
/// - challenge: Optionally generate a challenge on your own. Defaults to `nil` which will let the library
117+
/// generate it.
118+
/// - timeout: How long in milliseconds the browser should give the user to choose an authenticator. This value
119+
/// is a *hint* and may be ignored by the browser.
120+
/// - allowCredentials: A list of credentials registered to the user.
121+
/// - userVerification: The Relying Party's preference for the authenticator's enforcement of the
122+
/// "user verified" flag.
123+
/// - Returns: Authentication options ready for the browser.
100124
public func beginAuthentication(
101125
challenge: EncodedBase64? = nil,
102-
timeout: TimeInterval?,
126+
timeout: TimeInterval? = 60000,
103127
allowCredentials: [PublicKeyCredentialDescriptor]? = nil,
104128
userVerification: UserVerificationRequirement = .preferred
105129
) throws -> PublicKeyCredentialRequestOptions {
@@ -113,6 +137,16 @@ public struct WebAuthnManager {
113137
)
114138
}
115139

140+
/// Verify a response from navigator.credentials.get()
141+
///
142+
/// - Parameters:
143+
/// - credential: The value returned from `navigator.credentials.get()`.
144+
/// - expectedChallenge: The challenge passed to the authenticator within the preceding authentication options.
145+
/// - credentialPublicKey: The public key for the credential's ID as provided in a preceding authenticator
146+
/// registration ceremony.
147+
/// - credentialCurrentSignCount: The current known number of times the authenticator was used.
148+
/// - requireUserVerification: Whether or not to require that the authenticator verified the user.
149+
/// - Returns: Information about the authenticator
116150
public func finishAuthentication(
117151
credential: AuthenticationCredential,
118152
// clientExtensionResults: ,

Tests/WebAuthnTests/WebAuthnManagerTests+Authentication.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ extension WebAuthnManagerTests {
2323
func testBeginAuthentication() async throws {
2424
let allowCredentials: [PublicKeyCredentialDescriptor] = [.init(type: "public-key", id: [1, 0, 2, 30])]
2525
let options = try webAuthnManager.beginAuthentication(
26-
timeout: timeout,
26+
timeout: 1234,
2727
allowCredentials: allowCredentials,
2828
userVerification: .preferred
2929
)
3030

3131
XCTAssertEqual(options.challenge, challenge.base64EncodedString())
32-
XCTAssertEqual(options.timeout, timeout)
32+
XCTAssertEqual(options.timeout, 1234)
3333
XCTAssertEqual(options.rpId, relyingPartyID)
3434
XCTAssertEqual(options.allowCredentials, allowCredentials)
3535
XCTAssertEqual(options.userVerification, .preferred)

Tests/WebAuthnTests/WebAuthnManagerTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ final class WebAuthnManagerTests: XCTestCase {
2626
let relyingPartyDisplayName = "Testy test"
2727
let relyingPartyID = "webauthn.io"
2828
let relyingPartyOrigin = "https://example.com"
29-
let timeout: TimeInterval = 6000
3029

3130
override func setUp() {
3231
let config = WebAuthnConfig(
3332
relyingPartyDisplayName: relyingPartyDisplayName,
3433
relyingPartyID: relyingPartyID,
35-
relyingPartyOrigin: relyingPartyOrigin,
36-
timeout: timeout
34+
relyingPartyOrigin: relyingPartyOrigin
3735
)
3836
webAuthnManager = .init(config: config, challengeGenerator: .mock(generate: challenge))
3937
}
@@ -51,7 +49,6 @@ final class WebAuthnManagerTests: XCTestCase {
5149
XCTAssertEqual(options.challenge, challenge.base64EncodedString())
5250
XCTAssertEqual(options.rp.id, relyingPartyID)
5351
XCTAssertEqual(options.rp.name, relyingPartyDisplayName)
54-
XCTAssertEqual(options.timeout, timeout)
5552
XCTAssertEqual(options.user.id, user.userID.toBase64().asString())
5653
XCTAssertEqual(options.user.displayName, user.displayName)
5754
XCTAssertEqual(options.user.name, user.name)

0 commit comments

Comments
 (0)