-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathAuthenticationCredential.swift
66 lines (54 loc) · 2.53 KB
/
AuthenticationCredential.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift WebAuthn open source project
//
// Copyright (c) 2022 the Swift WebAuthn project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
/// The unprocessed response received from `navigator.credentials.get()`.
///
/// When decoding using `Decodable`, the `rawID` is decoded from base64url to bytes.
public struct AuthenticationCredential: Sendable {
/// The credential ID of the newly created credential.
public let id: URLEncodedBase64
/// The raw credential ID of the newly created credential.
public let rawID: [UInt8]
/// The attestation response from the authenticator.
public let response: AuthenticatorAssertionResponse
/// Reports the authenticator attachment modality in effect at the time the navigator.credentials.create() or
/// navigator.credentials.get() methods successfully complete
public let authenticatorAttachment: AuthenticatorAttachment?
/// Value will always be ``CredentialType/publicKey`` (for now)
public let type: CredentialType
}
extension AuthenticationCredential: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(URLEncodedBase64.self, forKey: .id)
rawID = try container.decodeBytesFromURLEncodedBase64(forKey: .rawID)
response = try container.decode(AuthenticatorAssertionResponse.self, forKey: .response)
authenticatorAttachment = try container.decodeIfPresent(AuthenticatorAttachment.self, forKey: .authenticatorAttachment)
type = try container.decode(CredentialType.self, forKey: .type)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(rawID.base64URLEncodedString(), forKey: .rawID)
try container.encode(response, forKey: .response)
try container.encodeIfPresent(authenticatorAttachment, forKey: .authenticatorAttachment)
try container.encode(type, forKey: .type)
}
private enum CodingKeys: String, CodingKey {
case id
case rawID = "rawId"
case response
case authenticatorAttachment
case type
}
}