Skip to content

Commit 4862921

Browse files
Updated RegistrationCredential to be Codable
1 parent e7a549a commit 4862921

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

Sources/WebAuthn/Ceremonies/Registration/AuthenticatorAttestationResponse.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@ public struct AuthenticatorAttestationResponse {
3030
public let attestationObject: [UInt8]
3131
}
3232

33-
extension AuthenticatorAttestationResponse: Decodable {
33+
extension AuthenticatorAttestationResponse: Codable {
3434
public init(from decoder: Decoder) throws {
3535
let container = try decoder.container(keyedBy: CodingKeys.self)
3636

3737
clientDataJSON = try container.decodeBytesFromURLEncodedBase64(forKey: .clientDataJSON)
3838
attestationObject = try container.decodeBytesFromURLEncodedBase64(forKey: .attestationObject)
3939
}
40+
41+
public func encode(to encoder: Encoder) throws {
42+
var container = encoder.container(keyedBy: CodingKeys.self)
43+
44+
try container.encode(clientDataJSON.base64URLEncodedString(), forKey: .clientDataJSON)
45+
try container.encode(attestationObject.base64URLEncodedString(), forKey: .attestationObject)
46+
}
4047

4148
private enum CodingKeys: String, CodingKey {
4249
case clientDataJSON

Sources/WebAuthn/Ceremonies/Registration/RegistrationCredential.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,25 @@ public struct RegistrationCredential {
4343
}
4444
}
4545

46-
extension RegistrationCredential: Decodable {
46+
extension RegistrationCredential: Codable {
4747
public init(from decoder: Decoder) throws {
4848
let container = try decoder.container(keyedBy: CodingKeys.self)
4949

5050
id = try container.decode(URLEncodedBase64.self, forKey: .id)
5151
type = try container.decode(CredentialType.self, forKey: .type)
52-
guard let rawID = try container.decode(URLEncodedBase64.self, forKey: .rawID).decodedBytes else {
53-
throw DecodingError.dataCorruptedError(
54-
forKey: .rawID,
55-
in: container,
56-
debugDescription: "Failed to decode base64url encoded rawID into bytes"
57-
)
58-
}
59-
self.rawID = rawID
52+
rawID = try container.decodeBytesFromURLEncodedBase64(forKey: .rawID)
6053
attestationResponse = try container.decode(AuthenticatorAttestationResponse.self, forKey: .attestationResponse)
6154
}
6255

56+
public func encode(to encoder: Encoder) throws {
57+
var container = encoder.container(keyedBy: CodingKeys.self)
58+
59+
try container.encode(id, forKey: .id)
60+
try container.encode(rawID.base64URLEncodedString(), forKey: .rawID)
61+
try container.encode(type, forKey: .type)
62+
try container.encode(attestationResponse, forKey: .attestationResponse)
63+
}
64+
6365
private enum CodingKeys: String, CodingKey {
6466
case id
6567
case type

0 commit comments

Comments
 (0)