@@ -19,7 +19,7 @@ import Foundation
19
19
/// `Encodable` byte arrays are base64url encoded.
20
20
///
21
21
/// - SeeAlso: https://www.w3.org/TR/webauthn-2/#dictionary-makecredentialoptions
22
- public struct PublicKeyCredentialCreationOptions : Encodable , Sendable {
22
+ public struct PublicKeyCredentialCreationOptions : Codable , Sendable {
23
23
/// A byte array randomly generated by the Relying Party. Should be at least 16 bytes long to ensure sufficient
24
24
/// entropy.
25
25
///
@@ -28,24 +28,24 @@ public struct PublicKeyCredentialCreationOptions: Encodable, Sendable {
28
28
public let challenge : [ UInt8 ]
29
29
30
30
/// Contains names and an identifier for the user account performing the registration
31
- public let user : PublicKeyCredentialUserEntity
31
+ public var user : PublicKeyCredentialUserEntity
32
32
33
33
/// Contains a name and an identifier for the Relying Party responsible for the request
34
- public let relyingParty : PublicKeyCredentialRelyingPartyEntity
34
+ public var relyingParty : PublicKeyCredentialRelyingPartyEntity
35
35
36
36
/// A list of key types and signature algorithms the Relying Party supports. Ordered from most preferred to least
37
37
/// preferred.
38
- public let publicKeyCredentialParameters : [ PublicKeyCredentialParameters ]
38
+ public var publicKeyCredentialParameters : [ PublicKeyCredentialParameters ]
39
39
40
40
/// A time, in seconds, that the caller is willing to wait for the call to complete. This is treated as a
41
41
/// hint, and may be overridden by the client.
42
42
///
43
43
/// - Note: When encoded, this value is represented in milleseconds as a ``UInt32``.
44
- public let timeout : Duration ?
44
+ public var timeout : Duration ?
45
45
46
46
/// Sets the Relying Party's preference for attestation conveyance. At the time of writing only `none` is
47
47
/// supported.
48
- public let attestation : AttestationConveyancePreference
48
+ public var attestation : AttestationConveyancePreference
49
49
50
50
public func encode( to encoder: any Encoder ) throws {
51
51
var container = encoder. container ( keyedBy: CodingKeys . self)
@@ -57,6 +57,35 @@ public struct PublicKeyCredentialCreationOptions: Encodable, Sendable {
57
57
try container. encodeIfPresent ( timeout? . milliseconds, forKey: . timeout)
58
58
try container. encode ( attestation, forKey: . attestation)
59
59
}
60
+
61
+ public init ( from decoder: any Decoder ) throws {
62
+ let values = try decoder. container ( keyedBy: CodingKeys . self)
63
+
64
+ self . challenge = try values. decodeBytesFromURLEncodedBase64 ( forKey: . challenge)
65
+ self . user = try values. decode ( PublicKeyCredentialUserEntity . self, forKey: . user)
66
+ self . relyingParty = try values. decode ( PublicKeyCredentialRelyingPartyEntity . self, forKey: . relyingParty)
67
+ self . publicKeyCredentialParameters = try values. decode ( [ PublicKeyCredentialParameters ] . self, forKey: . publicKeyCredentialParameters)
68
+ if let timeout = try values. decodeIfPresent ( UInt32 . self, forKey: . timeout) {
69
+ self . timeout = . milliseconds( timeout)
70
+ }
71
+ self . attestation = try values. decode ( AttestationConveyancePreference . self, forKey: . attestation)
72
+ }
73
+
74
+ public init (
75
+ challenge: [ UInt8 ] ,
76
+ user: PublicKeyCredentialUserEntity ,
77
+ relyingParty: PublicKeyCredentialRelyingPartyEntity ,
78
+ publicKeyCredentialParameters: [ PublicKeyCredentialParameters ] ,
79
+ timeout: Duration ? ,
80
+ attestation: AttestationConveyancePreference
81
+ ) {
82
+ self . challenge = challenge
83
+ self . user = user
84
+ self . relyingParty = relyingParty
85
+ self . publicKeyCredentialParameters = publicKeyCredentialParameters
86
+ self . timeout = timeout
87
+ self . attestation = attestation
88
+ }
60
89
61
90
private enum CodingKeys : String , CodingKey {
62
91
case challenge
@@ -70,7 +99,7 @@ public struct PublicKeyCredentialCreationOptions: Encodable, Sendable {
70
99
71
100
// MARK: - Credential parameters
72
101
/// From §5.3 (https://w3c.github.io/TR/webauthn/#dictionary-credential-params)
73
- public struct PublicKeyCredentialParameters : Equatable , Encodable , Sendable {
102
+ public struct PublicKeyCredentialParameters : Equatable , Codable , Sendable {
74
103
/// The type of credential to be created. At the time of writing always ``CredentialType/publicKey``.
75
104
public let type : CredentialType
76
105
/// The cryptographic signature algorithm with which the newly generated credential will be used, and thus also
@@ -87,6 +116,13 @@ public struct PublicKeyCredentialParameters: Equatable, Encodable, Sendable {
87
116
self . type = type
88
117
self . alg = alg
89
118
}
119
+
120
+ public init ( from decoder: any Decoder ) throws {
121
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
122
+
123
+ self . type = try container. decode ( CredentialType . self, forKey: . type)
124
+ self . alg = try container. decode ( COSEAlgorithmIdentifier . self, forKey: . alg)
125
+ }
90
126
}
91
127
92
128
extension Array where Element == PublicKeyCredentialParameters {
@@ -103,22 +139,26 @@ extension Array where Element == PublicKeyCredentialParameters {
103
139
/// From §5.4.2 (https://www.w3.org/TR/webauthn/#sctn-rp-credential-params).
104
140
/// The PublicKeyCredentialRelyingPartyEntity dictionary is used to supply additional Relying Party attributes when
105
141
/// creating a new credential.
106
- public struct PublicKeyCredentialRelyingPartyEntity : Encodable , Sendable {
142
+ public struct PublicKeyCredentialRelyingPartyEntity : Codable , Sendable {
107
143
/// A unique identifier for the Relying Party entity.
108
- public let id : String
144
+ public var id : String
109
145
110
146
/// A human-readable identifier for the Relying Party, intended only for display. For example, "ACME Corporation",
111
147
/// "Wonderful Widgets, Inc." or "ОАО Примертех".
112
- public let name : String
148
+ public var name : String
113
149
150
+ public init ( id: String , name: String ) {
151
+ self . id = id
152
+ self . name = name
153
+ }
114
154
}
115
155
116
156
/// From §5.4.3 (https://www.w3.org/TR/webauthn/#dictionary-user-credential-params)
117
157
/// The PublicKeyCredentialUserEntity dictionary is used to supply additional user account attributes when
118
158
/// creating a new credential.
119
159
///
120
160
/// When encoding using `Encodable`, `id` is base64url encoded.
121
- public struct PublicKeyCredentialUserEntity : Encodable , Sendable {
161
+ public struct PublicKeyCredentialUserEntity : Codable , Sendable {
122
162
/// Generated by the Relying Party, unique to the user account, and must not contain personally identifying
123
163
/// information about the user.
124
164
///
@@ -149,6 +189,15 @@ public struct PublicKeyCredentialUserEntity: Encodable, Sendable {
149
189
try container. encode ( name, forKey: . name)
150
190
try container. encode ( displayName, forKey: . displayName)
151
191
}
192
+
193
+ public init ( from decoder: any Decoder ) throws {
194
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
195
+
196
+ self . id = try container. decodeBytesFromURLEncodedBase64 ( forKey: . id)
197
+ self . name = try container. decode ( String . self, forKey: . name)
198
+ self . displayName = try container. decode ( String . self, forKey: . displayName)
199
+ }
200
+
152
201
153
202
private enum CodingKeys : String , CodingKey {
154
203
case id
0 commit comments