Skip to content

Commit 15a1a73

Browse files
Cleaned up and simplified testing configurations
1 parent e9205fd commit 15a1a73

File tree

7 files changed

+257
-265
lines changed

7 files changed

+257
-265
lines changed

Tests/WebAuthnTests/Utils/TestModels/TestAttestationObject.swift

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@
1313

1414
import WebAuthn
1515
@preconcurrency import SwiftCBOR
16+
import Testing
1617

1718
// protocol AttestationObjectParameter: CBOR {}
1819

1920
struct TestAttestationObject {
2021
var fmt: CBOR?
2122
var attStmt: CBOR?
22-
var authData: CBOR?
23+
var authData: AuthData = .none
24+
25+
enum AuthData {
26+
case structured(TestAuthData)
27+
case cbor(CBOR)
28+
case none
29+
}
2330

2431
var cborEncoded: [UInt8] {
2532
var attestationObject: [CBOR: CBOR] = [:]
@@ -29,8 +36,12 @@ struct TestAttestationObject {
2936
if let attStmt {
3037
attestationObject[.utf8String("attStmt")] = attStmt
3138
}
32-
if let authData {
39+
switch authData {
40+
case .structured(let authData):
41+
attestationObject[.utf8String("authData")] = .byteString(authData.byteArrayRepresentation)
42+
case .cbor(let authData):
3343
attestationObject[.utf8String("authData")] = authData
44+
case .none: break
3445
}
3546

3647
return [UInt8](CBOR.map(attestationObject).encode())
@@ -53,13 +64,13 @@ struct TestAttestationObjectBuilder {
5364

5465
func validMockECDSA() -> Self {
5566
var temp = self.keyAgnosticBase()
56-
temp.wrapped.authData = .byteString(TestAuthDataBuilder().validMockECDSA().build().byteArrayRepresentation)
67+
temp.wrapped.authData = .structured(TestAuthDataBuilder().validMockECDSA().build())
5768
return temp
5869
}
5970

6071
func validMockRSA() -> Self {
6172
var temp = self.keyAgnosticBase()
62-
temp.wrapped.authData = .byteString(TestAuthDataBuilder().validMockRSA().build().byteArrayRepresentation)
73+
temp.wrapped.authData = .structured(TestAuthDataBuilder().validMockRSA().build())
6374
return temp
6475
}
6576

@@ -115,25 +126,38 @@ struct TestAttestationObjectBuilder {
115126

116127
func invalidAuthData() -> Self {
117128
var temp = self
118-
temp.wrapped.authData = .double(1)
129+
temp.wrapped.authData = .cbor(.double(1))
119130
return temp
120131
}
121132

122133
func emptyAuthData() -> Self {
123134
var temp = self
124-
temp.wrapped.authData = .byteString([])
135+
temp.wrapped.authData = .cbor(.byteString([]))
125136
return temp
126137
}
127138

128139
func zeroAuthData(byteCount: Int) -> Self {
129140
var temp = self
130-
temp.wrapped.authData = .byteString([UInt8](repeating: 0, count: byteCount))
141+
temp.wrapped.authData = .cbor(.byteString([UInt8](repeating: 0, count: byteCount)))
131142
return temp
132143
}
133144

134145
func authData(_ builder: TestAuthDataBuilder) -> Self {
135146
var temp = self
136-
temp.wrapped.authData = .byteString(builder.build().byteArrayRepresentation)
147+
temp.wrapped.authData = .structured(builder.build())
148+
return temp
149+
}
150+
151+
func authData(builder: (TestAuthDataBuilder) -> TestAuthDataBuilder) -> Self {
152+
var temp = self
153+
switch temp.wrapped.authData {
154+
case .structured(let testAuthData):
155+
temp.wrapped.authData = .structured(builder(.init(wrapped: testAuthData)).build())
156+
case .cbor:
157+
Issue.record("authData must be structured")
158+
case .none:
159+
temp.wrapped.authData = .structured(builder(.init()).build())
160+
}
137161
return temp
138162
}
139163

Tests/WebAuthnTests/Utils/TestModels/TestECCKeyPair.swift

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,33 @@ struct TestECCKeyPair: TestSigner {
3333
static let publicKeyXCoordinate = "9621b40922a4b52f513c1b6679b8d48f81019972c7f3c64d6c856de34e45a645".hexadecimal!
3434
static let publicKeyYCoordinate = "6637312afe0ae9a2bec08bcf4611e0e9081e6f120311a8986605d5d3b4b248f8".hexadecimal!
3535

36-
static func signature(data: Data) throws -> P256.Signing.ECDSASignature {
37-
let privateKey = try P256.Signing.PrivateKey(pemRepresentation: privateKeyPEM)
38-
return try privateKey.signature(for: data)
39-
}
40-
4136
static func sign(data: Data) throws -> [UInt8] {
42-
Array(try signature(data: data).derRepresentation)
37+
let privateKey = try P256.Signing.PrivateKey(pemRepresentation: privateKeyPEM)
38+
return Array(try privateKey.signature(for: data).derRepresentation)
4339
}
4440

4541
static var signature: [UInt8] {
4642
get throws {
4743
let authenticatorData = TestAuthDataBuilder()
4844
.validAuthenticationMock()
4945
.buildAsBase64URLEncoded()
50-
46+
5147
// Create a signature. This part is usually performed by the authenticator
5248
let clientData: Data = TestClientDataJSON(type: "webauthn.get").jsonData
5349
let clientDataHash = SHA256.hash(data: clientData)
5450
let rawAuthenticatorData = authenticatorData.urlDecoded.decoded!
5551
let signatureBase = rawAuthenticatorData + clientDataHash
56-
// swiftlint:disable:next force_try
57-
let signature = try TestECCKeyPair.signature(data: signatureBase).derRepresentation
58-
59-
return [UInt8](signature)
52+
53+
return try sign(data: signatureBase)
6054
}
6155
}
6256
}
57+
58+
extension TestKeyConfiguration {
59+
static let ecdsa = TestKeyConfiguration(
60+
signer: TestECCKeyPair.self,
61+
credentialPublicKeyBuilder: TestCredentialPublicKeyBuilder().validMockECDSA(),
62+
authDataBuilder: TestAuthDataBuilder().validMockECDSA(),
63+
attestationObjectBuilder: TestAttestationObjectBuilder().validMockECDSA()
64+
)
65+
}

Tests/WebAuthnTests/Utils/TestModels/TestSigner.swift renamed to Tests/WebAuthnTests/Utils/TestModels/TestKeyConfiguration.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,20 @@ import Foundation
1515

1616
protocol TestSigner {
1717
static func sign(data: Data) throws -> [UInt8]
18+
19+
static var signature: [UInt8] { get throws }
20+
}
21+
22+
struct TestKeyConfiguration {
23+
var signer: any TestSigner.Type
24+
var credentialPublicKeyBuilder: TestCredentialPublicKeyBuilder
25+
var authDataBuilder: TestAuthDataBuilder
26+
var attestationObjectBuilder: TestAttestationObjectBuilder
27+
28+
var credentialPublicKey: [UInt8] {
29+
credentialPublicKeyBuilder.buildAsByteArray()
30+
}
31+
var attestationObject: [UInt8] {
32+
attestationObjectBuilder.build().cborEncoded
33+
}
1834
}

Tests/WebAuthnTests/Utils/TestModels/TestRSAKeyPair.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct TestRSAKeyPair: TestSigner {
4646
m0Eq9qinAmFyVbkuIzqCJMGeC1FxUYIf/DkpAMOb/ACTyig+YFgFjdU=
4747
-----END RSA PRIVATE KEY-----
4848
"""
49-
49+
5050
static let publicKeyPEM = """
5151
-----BEGIN PUBLIC KEY-----
5252
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAngCfNRz1D1HvyvWxURSK
@@ -60,32 +60,34 @@ struct TestRSAKeyPair: TestSigner {
6060
"""
6161
static let publicKeyNCoordinate = [UInt8](try! _RSA.Signing.PublicKey(pemRepresentation: publicKeyPEM).getKeyPrimitives().modulus)
6262
static let publicKeyECoordinate = [UInt8](try! _RSA.Signing.PublicKey(pemRepresentation: publicKeyPEM).getKeyPrimitives().publicExponent)
63-
64-
static func signature(data: Data) throws -> _RSA.Signing.RSASignature {
65-
let privateKey = try _RSA.Signing.PrivateKey(pemRepresentation: privateKeyPEM)
66-
let rsaSignature = try privateKey.signature(for: data,padding:_RSA.Signing.Padding.insecurePKCS1v1_5)
67-
return rsaSignature
68-
}
6963

7064
static func sign(data: Data) throws -> [UInt8] {
71-
Array(try signature(data: data).rawRepresentation)
65+
let privateKey = try _RSA.Signing.PrivateKey(pemRepresentation: privateKeyPEM)
66+
return Array(try privateKey.signature(for: data,padding:_RSA.Signing.Padding.insecurePKCS1v1_5).rawRepresentation)
7267
}
73-
68+
7469
static var signature: [UInt8] {
7570
get throws {
7671
let authenticatorData = TestAuthDataBuilder()
7772
.validAuthenticationMock()
7873
.buildAsBase64URLEncoded()
79-
74+
8075
// Create a signature. This part is usually performed by the authenticator
8176
let clientData: Data = TestClientDataJSON(type: "webauthn.get").jsonData
8277
let clientDataHash = SHA256.hash(data: clientData)
8378
let rawAuthenticatorData = authenticatorData.urlDecoded.decoded!
8479
let signatureBase = rawAuthenticatorData + clientDataHash
85-
// swiftlint:disable:next force_try
86-
let signature = try TestRSAKeyPair.signature(data: signatureBase).rawRepresentation
87-
88-
return [UInt8](signature)
80+
81+
return try sign(data: signatureBase)
8982
}
9083
}
9184
}
85+
86+
extension TestKeyConfiguration {
87+
static let rsa = TestKeyConfiguration(
88+
signer: TestRSAKeyPair.self,
89+
credentialPublicKeyBuilder: TestCredentialPublicKeyBuilder().validMockRSA(),
90+
authDataBuilder: TestAuthDataBuilder().validMockRSA(),
91+
attestationObjectBuilder: TestAttestationObjectBuilder().validMockRSA()
92+
)
93+
}

0 commit comments

Comments
 (0)