Skip to content

Commit c416dcb

Browse files
Updated PublicKeyCredentialRequestOptions to be Codable
1 parent 52f5a3e commit c416dcb

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

Sources/WebAuthn/Ceremonies/Authentication/PublicKeyCredentialRequestOptions.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Foundation
1919
/// When encoding using `Encodable`, the byte arrays are encoded as base64url.
2020
///
2121
/// - SeeAlso: https://www.w3.org/TR/webauthn-2/#dictionary-assertion-options
22-
public struct PublicKeyCredentialRequestOptions: Encodable, Sendable {
22+
public struct PublicKeyCredentialRequestOptions: Sendable {
2323
/// A challenge that the authenticator signs, along with other data, when producing an authentication assertion
2424
///
2525
/// When encoding using `Encodable` this is encoded as base64url.
@@ -45,7 +45,19 @@ public struct PublicKeyCredentialRequestOptions: Encodable, Sendable {
4545
public let userVerification: UserVerificationRequirement?
4646

4747
// let extensions: [String: Any]
48+
}
4849

50+
extension PublicKeyCredentialRequestOptions: Codable {
51+
public init(from decoder: Decoder) throws {
52+
let container = try decoder.container(keyedBy: CodingKeys.self)
53+
54+
challenge = try container.decodeBytesFromURLEncodedBase64(forKey: .challenge)
55+
timeout = try container.decodeIfPresent(UInt32.self, forKey: .timeout).map { .milliseconds($0) }
56+
relyingPartyID = try container.decode(String.self, forKey: .rpID)
57+
allowCredentials = try container.decodeIfPresent([PublicKeyCredentialDescriptor].self, forKey: .allowCredentials)
58+
userVerification = try container.decodeIfPresent(UserVerificationRequirement.self, forKey: .userVerification)
59+
}
60+
4961
public func encode(to encoder: Encoder) throws {
5062
var container = encoder.container(keyedBy: CodingKeys.self)
5163

@@ -68,10 +80,10 @@ public struct PublicKeyCredentialRequestOptions: Encodable, Sendable {
6880
/// Information about a generated credential.
6981
///
7082
/// When encoding using `Encodable`, `id` is encoded as base64url.
71-
public struct PublicKeyCredentialDescriptor: Equatable, Encodable, Sendable {
83+
public struct PublicKeyCredentialDescriptor: Equatable, Codable, Sendable {
7284
/// Defines hints as to how clients might communicate with a particular authenticator in order to obtain an
7385
/// assertion for a specific credential
74-
public enum AuthenticatorTransport: String, Equatable, Encodable, Sendable {
86+
public enum AuthenticatorTransport: String, Equatable, Codable, Sendable {
7587
/// Indicates the respective authenticator can be contacted over removable USB.
7688
case usb
7789
/// Indicates the respective authenticator can be contacted over Near Field Communication (NFC).
@@ -107,6 +119,14 @@ public struct PublicKeyCredentialDescriptor: Equatable, Encodable, Sendable {
107119
self.id = id
108120
self.transports = transports
109121
}
122+
123+
public init(from decoder: Decoder) throws {
124+
let container = try decoder.container(keyedBy: CodingKeys.self)
125+
126+
type = try container.decode(CredentialType.self, forKey: .type)
127+
id = try container.decodeBytesFromURLEncodedBase64(forKey: .id)
128+
transports = try container.decode([AuthenticatorTransport].self, forKey: .transports)
129+
}
110130

111131
public func encode(to encoder: Encoder) throws {
112132
var container = encoder.container(keyedBy: CodingKeys.self)
@@ -125,7 +145,7 @@ public struct PublicKeyCredentialDescriptor: Equatable, Encodable, Sendable {
125145

126146
/// The Relying Party may require user verification for some of its operations but not for others, and may use this
127147
/// type to express its needs.
128-
public enum UserVerificationRequirement: String, Encodable, Sendable {
148+
public enum UserVerificationRequirement: String, Codable, Sendable {
129149
/// The Relying Party requires user verification for the operation and will fail the overall ceremony if the
130150
/// user wasn't verified.
131151
case required

0 commit comments

Comments
 (0)