-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathTestCredentialPublicKey.swift
97 lines (85 loc) · 2.58 KB
/
TestCredentialPublicKey.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//===----------------------------------------------------------------------===//
//
// This source file is part of the WebAuthn Swift open source project
//
// Copyright (c) 2023 the WebAuthn Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of WebAuthn Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
@testable import WebAuthn
import PotentCBOR
struct TestCredentialPublicKey {
var kty: CBOR?
var alg: CBOR?
var crv: CBOR?
var xCoordinate: CBOR?
var yCoordinate: CBOR?
var byteArrayRepresentation: [UInt8] {
var value = CBOR.Map()
if let kty {
value[COSEKey.kty.cbor] = kty
}
if let alg {
value[COSEKey.alg.cbor] = alg
}
if let crv {
value[COSEKey.crv.cbor] = crv
}
if let xCoordinate {
value[COSEKey.x.cbor] = xCoordinate
}
if let yCoordinate {
value[COSEKey.y.cbor] = yCoordinate
}
let data = try! CBORSerialization.data(from: .map(value))
return [UInt8](data)
}
}
struct TestCredentialPublicKeyBuilder {
var wrapped: TestCredentialPublicKey
init(wrapped: TestCredentialPublicKey = TestCredentialPublicKey()) {
self.wrapped = wrapped
}
func buildAsByteArray() -> [UInt8] {
return wrapped.byteArrayRepresentation
}
func validMock() -> Self {
return self
.kty(.ellipticKey)
.crv(.p256)
.alg(.algES256)
.xCoordinate(TestECCKeyPair.publicKeyXCoordinate)
.yCoordiante(TestECCKeyPair.publicKeyYCoordinate)
}
func kty(_ kty: COSEKeyType) -> Self {
var temp = self
temp.wrapped.kty = .unsignedInt(kty.rawValue)
return temp
}
func crv(_ crv: COSECurve) -> Self {
var temp = self
temp.wrapped.crv = .unsignedInt(crv.rawValue)
return temp
}
func alg(_ alg: COSEAlgorithmIdentifier) -> Self {
var temp = self
temp.wrapped.alg = .negativeInt(UInt64(abs(alg.rawValue) - 1))
return temp
}
func xCoordinate(_ xCoordinate: [UInt8]) -> Self {
var temp = self
temp.wrapped.xCoordinate = .byteString(Data(xCoordinate))
return temp
}
func yCoordiante(_ yCoordinate: [UInt8]) -> Self {
var temp = self
temp.wrapped.yCoordinate = .byteString(Data(yCoordinate))
return temp
}
}