forked from vapor/jwt-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EdDSATests.swift
153 lines (129 loc) · 4.84 KB
/
EdDSATests.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import JWTKit
import XCTest
final class EdDSATests: XCTestCase {
func testEdDSAGenerate() async throws {
let payload = TestPayload(
sub: "vapor",
name: "Foo",
admin: false,
exp: .init(value: .init(timeIntervalSince1970: 2_000_000_000))
)
let keyCollection = try await JWTKeyCollection()
.addEdDSA(key: EdDSA.PrivateKey(curve: .ed25519))
let token = try await keyCollection.sign(payload)
try await XCTAssertEqualAsync(await keyCollection.verify(token, as: TestPayload.self), payload)
}
func testEdDSAPublicPrivate() async throws {
let keys = try await JWTKeyCollection()
.addEdDSA(key: EdDSA.PublicKey(x: eddsaPublicKeyBase64, curve: .ed25519), kid: "public")
.addEdDSA(key: EdDSA.PrivateKey(x: eddsaPublicKeyBase64, d: eddsaPrivateKeyBase64, curve: .ed25519), kid: "private")
let payload = TestPayload(
sub: "vapor",
name: "Foo",
admin: false,
exp: .init(value: .init(timeIntervalSince1970: 2_000_000_000))
)
for _ in 0 ..< 1000 {
let token = try await keys.sign(payload, kid: "private")
// test public signer decoding
try await XCTAssertEqualAsync(await keys.verify(token, as: TestPayload.self), payload)
}
}
func testVerifyingEdDSAKeyUsingJWK() async throws {
struct Foo: JWTPayload {
var bar: Int
func verify(using _: JWTAlgorithm) throws {}
}
// ecdsa key in base64 format
let x = eddsaPublicKeyBase64
let d = eddsaPrivateKeyBase64
// sign jwt
let keyCollection = try await JWTKeyCollection()
.addEdDSA(key: EdDSA.PrivateKey(x: x, d: d, curve: .ed25519), kid: "vapor")
let jwt = try await keyCollection.sign(Foo(bar: 42))
// verify using jwks
let jwksString = """
{
"keys": [
{
"kty": "OKP",
"crv": "Ed25519",
"use": "sig",
"kid": "vapor",
"x": "\(x)",
"d": "\(d)"
}
]
}
"""
try await keyCollection.use(jwksJSON: jwksString)
let foo = try await keyCollection.verify(jwt, as: Foo.self)
XCTAssertEqual(foo.bar, 42)
}
func testVerifyingEdDSAKeyUsingJWKBase64URL() async throws {
struct Foo: JWTPayload {
var bar: Int
func verify(using _: JWTAlgorithm) throws {}
}
// eddsa key in base64url format
let x = eddsaPublicKeyBase64Url
let d = eddsaPrivateKeyBase64Url
// sign jwt
let keyCollection = try await JWTKeyCollection()
.addEdDSA(key: EdDSA.PrivateKey(x: x, d: d, curve: .ed25519), kid: "vapor")
let jwt = try await keyCollection.sign(Foo(bar: 42))
// verify using jwks without alg
let jwksString = """
{
"keys": [
{
"kty": "OKP",
"crv": "Ed25519",
"use": "sig",
"kid": "vapor",
"x": "\(x)",
"d": "\(d)"
}
]
}
"""
try await keyCollection.use(jwksJSON: jwksString)
let foo = try await keyCollection.verify(jwt, as: Foo.self)
XCTAssertEqual(foo.bar, 42)
}
func testVerifyingEdDSAKeyUsingJWKWithMixedBase64Formats() async throws {
struct Foo: JWTPayload {
var bar: Int
func verify(using _: JWTAlgorithm) throws {}
}
// eddsa key in base64url format
let x = eddsaPublicKeyBase64Url
let d = eddsaPrivateKeyBase64
// sign jwt
let keyCollection = try await JWTKeyCollection()
.addEdDSA(key: EdDSA.PrivateKey(x: x, d: d, curve: .ed25519), kid: "vapor")
let jwt = try await keyCollection.sign(Foo(bar: 42), kid: "vapor")
// verify using jwks without alg
let jwksString = """
{
"keys": [
{
"kty": "OKP",
"crv": "Ed25519",
"use": "sig",
"kid": "vapor",
"x": "\(x)",
"d": "\(d)"
}
]
}
"""
try await keyCollection.use(jwksJSON: jwksString)
let foo = try await keyCollection.verify(jwt, as: Foo.self)
XCTAssertEqual(foo.bar, 42)
}
}
let eddsaPublicKeyBase64 = "0ZcEvMCSYqSwR8XIkxOoaYjRQSAO8frTMSCpNbUl4lE="
let eddsaPrivateKeyBase64 = "d1H3/dcg0V3XyAuZW2TE5Z3rhY20M+4YAfYu/HUQd8w="
let eddsaPublicKeyBase64Url = "0ZcEvMCSYqSwR8XIkxOoaYjRQSAO8frTMSCpNbUl4lE"
let eddsaPrivateKeyBase64Url = "d1H3_dcg0V3XyAuZW2TE5Z3rhY20M-4YAfYu_HUQd8w"