Skip to content

Commit 9bfb5d1

Browse files
Replaced usage of TimeInterval with Duration
1 parent d76772a commit 9bfb5d1

File tree

7 files changed

+56
-12
lines changed

7 files changed

+56
-12
lines changed

Sources/WebAuthn/Ceremonies/Authentication/PublicKeyCredentialRequestOptions.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public struct PublicKeyCredentialRequestOptions: Encodable {
3030
///
3131
/// - Note: When encoded, this value is represented in milleseconds as a ``UInt32``.
3232
/// See https://www.w3.org/TR/webauthn-2/#dictionary-assertion-options
33-
public let timeout: TimeInterval?
33+
public let timeout: Duration?
3434

3535
/// The Relying Party ID.
3636
public let rpId: String?
@@ -47,8 +47,7 @@ public struct PublicKeyCredentialRequestOptions: Encodable {
4747
var container = encoder.container(keyedBy: CodingKeys.self)
4848

4949
try container.encode(challenge.base64URLEncodedString(), forKey: .challenge)
50-
let timeoutInMilliseconds = timeout.map { UInt32($0 * 1000) }
51-
try container.encodeIfPresent(timeoutInMilliseconds, forKey: .timeout)
50+
try container.encodeIfPresent(timeout?.milliseconds, forKey: .timeout)
5251
try container.encodeIfPresent(rpId, forKey: .rpId)
5352
try container.encodeIfPresent(allowCredentials, forKey: .allowCredentials)
5453
try container.encodeIfPresent(userVerification, forKey: .userVerification)

Sources/WebAuthn/Ceremonies/Registration/PublicKeyCredentialCreationOptions.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public struct PublicKeyCredentialCreationOptions: Encodable {
4242
/// hint, and may be overridden by the client.
4343
///
4444
/// - Note: When encoded, this value is represented in milleseconds as a ``UInt32``.
45-
public let timeout: TimeInterval?
45+
public let timeout: Duration?
4646

4747
/// Sets the Relying Party's preference for attestation conveyance. At the time of writing only `none` is
4848
/// supported.
@@ -55,8 +55,7 @@ public struct PublicKeyCredentialCreationOptions: Encodable {
5555
try container.encode(user, forKey: .user)
5656
try container.encode(relyingParty, forKey: .relyingParty)
5757
try container.encode(publicKeyCredentialParameters, forKey: .publicKeyCredentialParameters)
58-
let timeoutInMilliseconds = timeout.map { UInt32($0 * 1000) }
59-
try container.encodeIfPresent(timeoutInMilliseconds, forKey: .timeout)
58+
try container.encodeIfPresent(timeout?.milliseconds, forKey: .timeout)
6059
try container.encode(attestation, forKey: .attestation)
6160
}
6261

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the WebAuthn Swift open source project
4+
//
5+
// Copyright (c) 2022 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+
extension Duration {
16+
/// The value of a positive duration in milliseconds, suitable to be encoded in WebAuthn types.
17+
var milliseconds: Int64 {
18+
let (seconds, attoseconds) = self.components
19+
return Int64(seconds * 1000) + Int64(attoseconds/1_000_000_000_000_000)
20+
}
21+
}

Sources/WebAuthn/WebAuthnManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public struct WebAuthnManager {
5555
/// - Returns: Registration options ready for the browser.
5656
public func beginRegistration(
5757
user: PublicKeyCredentialUserEntity,
58-
timeout: TimeInterval? = 3600,
58+
timeout: Duration? = .seconds(3600),
5959
attestation: AttestationConveyancePreference = .none,
6060
publicKeyCredentialParameters: [PublicKeyCredentialParameters] = .supported
6161
) -> PublicKeyCredentialCreationOptions {
@@ -134,7 +134,7 @@ public struct WebAuthnManager {
134134
/// "user verified" flag.
135135
/// - Returns: Authentication options ready for the browser.
136136
public func beginAuthentication(
137-
timeout: TimeInterval? = 60,
137+
timeout: Duration? = .seconds(60),
138138
allowCredentials: [PublicKeyCredentialDescriptor]? = nil,
139139
userVerification: UserVerificationRequirement = .preferred
140140
) throws -> PublicKeyCredentialRequestOptions {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the WebAuthn Swift open source project
4+
//
5+
// Copyright (c) 2022 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 XCTest
16+
@testable import WebAuthn
17+
18+
final class DurationTests: XCTestCase {
19+
func testMilliseconds() throws {
20+
XCTAssertEqual(Duration.milliseconds(1234).milliseconds, 1234)
21+
XCTAssertEqual(Duration.milliseconds(-1234).milliseconds, -1234)
22+
XCTAssertEqual(Duration.microseconds(12345).milliseconds, 12)
23+
XCTAssertEqual(Duration.microseconds(-12345).milliseconds, -12)
24+
}
25+
}

Tests/WebAuthnTests/WebAuthnManagerAuthenticationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ final class WebAuthnManagerAuthenticationTests: XCTestCase {
3737
func testBeginAuthentication() async throws {
3838
let allowCredentials: [PublicKeyCredentialDescriptor] = [.init(type: "public-key", id: [1, 0, 2, 30])]
3939
let options = try webAuthnManager.beginAuthentication(
40-
timeout: 1234,
40+
timeout: .seconds(1234),
4141
allowCredentials: allowCredentials,
4242
userVerification: .preferred
4343
)
4444

4545
XCTAssertEqual(options.challenge, challenge)
46-
XCTAssertEqual(options.timeout, 1234)
46+
XCTAssertEqual(options.timeout, .seconds(1234))
4747
XCTAssertEqual(options.rpId, relyingPartyID)
4848
XCTAssertEqual(options.allowCredentials, allowCredentials)
4949
XCTAssertEqual(options.userVerification, .preferred)

Tests/WebAuthnTests/WebAuthnManagerIntegrationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class WebAuthnManagerIntegrationTests: XCTestCase {
3131

3232
// Step 1.: Begin Registration
3333
let mockUser = PublicKeyCredentialUserEntity.mock
34-
let timeout: TimeInterval = 1234
34+
let timeout: Duration = .seconds(1234)
3535
let attestationPreference = AttestationConveyancePreference.none
3636
let publicKeyCredentialParameters: [PublicKeyCredentialParameters] = .supported
3737

@@ -93,7 +93,7 @@ final class WebAuthnManagerIntegrationTests: XCTestCase {
9393
XCTAssertEqual(credential.publicKey, mockCredentialPublicKey)
9494

9595
// Step 3.: Begin Authentication
96-
let authenticationTimeout: TimeInterval = 4567
96+
let authenticationTimeout: Duration = .seconds(4567)
9797
let userVerification: UserVerificationRequirement = .preferred
9898
let rememberedCredentials = [PublicKeyCredentialDescriptor(
9999
type: "public-key",

0 commit comments

Comments
 (0)