Skip to content

Commit c05656a

Browse files
Updated AuthenticationCredential to be Codable
1 parent c416dcb commit c05656a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Sources/WebAuthn/Ceremonies/Authentication/AuthenticationCredential.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct AuthenticationCredential: Sendable {
3535
public let type: CredentialType
3636
}
3737

38-
extension AuthenticationCredential: Decodable {
38+
extension AuthenticationCredential: Codable {
3939
public init(from decoder: Decoder) throws {
4040
let container = try decoder.container(keyedBy: CodingKeys.self)
4141

@@ -45,6 +45,16 @@ extension AuthenticationCredential: Decodable {
4545
authenticatorAttachment = try container.decodeIfPresent(AuthenticatorAttachment.self, forKey: .authenticatorAttachment)
4646
type = try container.decode(CredentialType.self, forKey: .type)
4747
}
48+
49+
public func encode(to encoder: Encoder) throws {
50+
var container = encoder.container(keyedBy: CodingKeys.self)
51+
52+
try container.encode(id, forKey: .id)
53+
try container.encode(rawID.base64URLEncodedString(), forKey: .rawID)
54+
try container.encode(response, forKey: .response)
55+
try container.encodeIfPresent(authenticatorAttachment, forKey: .authenticatorAttachment)
56+
try container.encode(type, forKey: .type)
57+
}
4858

4959
private enum CodingKeys: String, CodingKey {
5060
case id

Sources/WebAuthn/Ceremonies/Authentication/AuthenticatorAssertionResponse.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public struct AuthenticatorAssertionResponse: Sendable {
4949
public let attestationObject: [UInt8]?
5050
}
5151

52-
extension AuthenticatorAssertionResponse: Decodable {
52+
extension AuthenticatorAssertionResponse: Codable {
5353
public init(from decoder: Decoder) throws {
5454
let container = try decoder.container(keyedBy: CodingKeys.self)
5555

@@ -59,6 +59,16 @@ extension AuthenticatorAssertionResponse: Decodable {
5959
userHandle = try container.decodeBytesFromURLEncodedBase64IfPresent(forKey: .userHandle)
6060
attestationObject = try container.decodeBytesFromURLEncodedBase64IfPresent(forKey: .attestationObject)
6161
}
62+
63+
public func encode(to encoder: Encoder) throws {
64+
var container = encoder.container(keyedBy: CodingKeys.self)
65+
66+
try container.encode(clientDataJSON.base64URLEncodedString(), forKey: .clientDataJSON)
67+
try container.encode(authenticatorData.base64URLEncodedString(), forKey: .authenticatorData)
68+
try container.encode(signature.base64URLEncodedString(), forKey: .signature)
69+
try container.encodeIfPresent(userHandle?.base64URLEncodedString(), forKey: .userHandle)
70+
try container.encodeIfPresent(attestationObject?.base64URLEncodedString(), forKey: .attestationObject)
71+
}
6272

6373
private enum CodingKeys: String, CodingKey {
6474
case clientDataJSON

0 commit comments

Comments
 (0)