Skip to content

Commit fddc39f

Browse files
authored
Update JWTKit and Vapor versions (#155)
1 parent 0b56253 commit fddc39f

File tree

2 files changed

+49
-58
lines changed

2 files changed

+49
-58
lines changed

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ let package = Package(
1313
.library(name: "JWT", targets: ["JWT"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/vapor/jwt-kit.git", from: "5.0.0-beta.3"),
17-
.package(url: "https://github.com/vapor/vapor.git", from: "4.92.0"),
16+
.package(url: "https://github.com/vapor/jwt-kit.git", from: "5.0.0-beta.4"),
17+
.package(url: "https://github.com/vapor/vapor.git", from: "4.101.0"),
1818
],
1919
targets: [
2020
.target(

Tests/JWTTests/JWTTests.swift

+47-56
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ import JWTKit
33
import XCTVapor
44

55
class JWTTests: XCTestCase {
6+
var app: Application!
7+
8+
override func setUp() async throws {
9+
app = try await Application.make(.testing)
10+
XCTAssert(isLoggingConfigured)
11+
}
12+
13+
override func tearDown() async throws {
14+
try await app.asyncShutdown()
15+
}
16+
617
func testDocs() async throws {
7-
// creates a new application for testing
8-
let app = Application(.testing)
9-
defer { app.shutdown() }
10-
1118
// Add HMAC with SHA-256 signer.
12-
await app.jwt.keys.addHMAC(key: "secret", digestAlgorithm: .sha256)
19+
await app.jwt.keys.add(hmac: "secret", digestAlgorithm: .sha256)
1320

14-
await app.jwt.keys.addHMAC(key: "foo", digestAlgorithm: .sha256, kid: "a")
15-
await app.jwt.keys.addHMAC(key: "bar", digestAlgorithm: .sha256, kid: "b")
21+
await app.jwt.keys.add(hmac: "foo", digestAlgorithm: .sha256, kid: "a")
22+
await app.jwt.keys.add(hmac: "bar", digestAlgorithm: .sha256, kid: "b")
1623

1724
app.jwt.apple.applicationIdentifier = "..."
1825
app.get("apple") { req async throws -> HTTPStatus in
@@ -101,16 +108,18 @@ class JWTTests: XCTestCase {
101108
}
102109
}
103110

104-
try app.test(.GET, "me", beforeRequest: { req in
111+
try await app.test(.GET, "me", beforeRequest: { req in
105112
req.headers.bearerAuthorization = .init(token: """
106113
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ2YXBvciIsImV4cCI6NjQwOTIyMTEyMDAsImFkbWluIjp0cnVlfQ.lS5lpwfRNSZDvpGQk6x5JI1g40gkYCOWqbc3J_ghowo
107114
""")
108115
print(req)
109-
}, afterResponse: { res in
116+
}, afterResponse: { res async in
110117
XCTAssertEqual(res.status, .ok)
111-
}).test(.POST, "login", beforeRequest: { req in
118+
})
119+
120+
try await app.test(.POST, "login", beforeRequest: { req in
112121
print(req)
113-
}, afterResponse: { res in
122+
}, afterResponse: { res async throws in
114123
XCTAssertEqual(res.status, .ok)
115124
print(res.body.string)
116125
try XCTAssertNotNil(res.content.decode([String: String].self)["token"])
@@ -119,12 +128,8 @@ class JWTTests: XCTestCase {
119128

120129
// manual authentication using req.jwt.verify
121130
func testManual() async throws {
122-
// creates a new application for testing
123-
let app = Application(.testing)
124-
defer { app.shutdown() }
125-
126131
// configures an es512 signer using random key
127-
await app.jwt.keys.addECDSA(key: ES512PrivateKey())
132+
await app.jwt.keys.add(ecdsa: ES512PrivateKey())
128133

129134
// jwt creation using req.jwt.sign
130135
app.post("login") { req async throws -> LoginResponse in
@@ -142,9 +147,9 @@ class JWTTests: XCTestCase {
142147
var token: String?
143148

144149
// test login
145-
try app.testable().test(.POST, "login", beforeRequest: { req in
150+
try await app.testable().test(.POST, "login", beforeRequest: { req in
146151
try req.content.encode(LoginCredentials(name: "foo"))
147-
}) { res in
152+
}) { res async throws in
148153
XCTAssertEqual(res.status, .ok)
149154
XCTAssertContent(LoginResponse.self, res) { login in
150155
token = login.token
@@ -157,31 +162,27 @@ class JWTTests: XCTestCase {
157162
}
158163

159164
// test manual authentication using req.jwt.verify
160-
try app.testable().test(
165+
try await app.testable().test(
161166
.GET, "me", headers: ["authorization": "Bearer \(t)"]
162-
) { res in
167+
) { res async in
163168
XCTAssertEqual(res.status, .ok)
164169
XCTAssertEqual(res.body.string, "foo")
165170
}
166171

167172
// create a token from a different signer
168173
let fakeToken = try await JWTKeyCollection()
169-
.addECDSA(key: ES512PrivateKey()).sign(TestUser(name: "bob"))
170-
try app.testable().test(
174+
.add(ecdsa: ES512PrivateKey()).sign(TestUser(name: "bob"))
175+
try await app.testable().test(
171176
.GET, "me", headers: ["authorization": "Bearer \(fakeToken)"]
172-
) { res in
177+
) { res async in
173178
XCTAssertEqual(res.status, .unauthorized)
174179
}
175180
}
176181

177182
// test middleware-based authentication using req.auth.require
178183
func testMiddleware() async throws {
179-
// creates a new application for testing
180-
let app = Application(.testing)
181-
defer { app.shutdown() }
182-
183184
// configures an es512 signer using random key
184-
await app.jwt.keys.addECDSA(key: ES512PrivateKey())
185+
await app.jwt.keys.add(ecdsa: ES512PrivateKey())
185186

186187
// jwt creation using req.jwt.sign
187188
app.post("login") { req async throws -> LoginResponse in
@@ -208,23 +209,23 @@ class JWTTests: XCTestCase {
208209
var token: String?
209210

210211
// test login
211-
try app.testable().test(.POST, "login", beforeRequest: { req in
212+
try await app.testable().test(.POST, "login", beforeRequest: { req in
212213
try req.content.encode(LoginCredentials(name: "foo"))
213-
}) { res in
214+
}) { res async in
214215
XCTAssertEqual(res.status, .ok)
215216
XCTAssertContent(LoginResponse.self, res) { login in
216217
token = login.token
217218
}
218219
}
219220

220-
guard let t = token else {
221+
guard let token else {
221222
XCTFail("login failed")
222223
return
223224
}
224225

225-
try app.testable().test(
226-
.GET, "me", headers: ["authorization": "Bearer \(t)"]
227-
) { res in
226+
try await app.testable().test(
227+
.GET, "me", headers: ["authorization": "Bearer \(token)"]
228+
) { res async in
228229
XCTAssertEqual(res.status, .ok)
229230
XCTAssertContent(TestUser.self, res) { user in
230231
XCTAssertEqual(user.name, "foo")
@@ -234,17 +235,17 @@ class JWTTests: XCTestCase {
234235
// token from same signer but for a different user
235236
// this tests that the guard middleware catches the failure to auth before it reaches the route handler
236237
let wrongNameToken = try await app.jwt.keys.sign(TestUser(name: "bob"))
237-
try app.testable().test(
238+
try await app.testable().test(
238239
.GET, "me", headers: ["authorization": "Bearer \(wrongNameToken)"]
239-
) { res in
240+
) { res async in
240241
XCTAssertEqual(res.status, .unauthorized)
241242
}
242243

243244
// create a token from a different signer
244-
let fakeToken = try await JWTKeyCollection().addECDSA(key: ES512PrivateKey()).sign(TestUser(name: "bob"))
245-
try app.testable().test(
245+
let fakeToken = try await JWTKeyCollection().add(ecdsa: ES512PrivateKey()).sign(TestUser(name: "bob"))
246+
try await app.testable().test(
246247
.GET, "me", headers: ["authorization": "Bearer \(fakeToken)"]
247-
) { res in
248+
) { res async in
248249
XCTAssertEqual(res.status, .unauthorized)
249250
}
250251
}
@@ -255,10 +256,6 @@ class JWTTests: XCTestCase {
255256
Note that it takes a day for the JWT to expire before the test passes
256257
*/
257258
func testApple() async throws {
258-
// creates a new application for testing
259-
let app = Application(.testing)
260-
defer { app.shutdown() }
261-
262259
app.jwt.apple.applicationIdentifier = "dev.timc.siwa-demo.TILiOS"
263260

264261
app.get("test") { req async throws in
@@ -274,9 +271,11 @@ class JWTTests: XCTestCase {
274271
eyJraWQiOiJmaDZCczhDIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiZGV2LnRpbWMuc2l3YS1kZW1vLlRJTGlPUyIsImV4cCI6MTcwODUxNTY3NiwiaWF0IjoxNzA4NDI5Mjc2LCJzdWIiOiIwMDE1NDIuYjA0MTAwYzUxYWNiNDhkM2E1NzA2ODRmMTdkNjM5NGQuMTYwMyIsImNfaGFzaCI6ImFxQjM1RXR1bWFtVUg0VjZBYklmaXciLCJlbWFpbCI6Ijh5c2JjaHZjMm1AcHJpdmF0ZXJlbGF5LmFwcGxlaWQuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImlzX3ByaXZhdGVfZW1haWwiOnRydWUsImF1dGhfdGltZSI6MTcwODQyOTI3Niwibm9uY2Vfc3VwcG9ydGVkIjp0cnVlLCJyZWFsX3VzZXJfc3RhdHVzIjoyfQ.E4SmBvvsr-L1f4rbwoXIg23XJEdA6WQxLfT6Z0TaFRTNbufuUtvG41MwJvf62T3HdCsY1VXlhdVYmTNbzqCuax6CUObue2ndx6osInDzfTkzysx17eUeCaG1XCfq9mScuVgW8xh3ZPfIeQdsII-MnP8ZG7q-CAxf6soSza_BKrrw4TArvEXrjbZO7FI1U2K72JtVZ118wcuEWfv8JO-FWFOHgWzJujqxI_7ayVG-mQfZitmYXv5ws-stZMxA0RvIbuYLWAksI6-ehYEgeEQa6NzzcJNWm3oArB0ithQE59fqFDoKCwpLchBMANz3tmNpN194Rc4ppL-niIDWFE-0Ug
275272
""")
276273

277-
try app.test(.GET, "test", headers: headers) { res in
274+
try await app.test(.GET, "test", headers: headers) { res async in
278275
XCTAssertEqual(res.status, .unauthorized)
279-
}.test(.GET, "test2", headers: headers) { res in
276+
}
277+
278+
try await app.test(.GET, "test2", headers: headers) { res async in
280279
XCTAssertEqual(res.status, .unauthorized)
281280
}
282281
}
@@ -290,10 +289,6 @@ class JWTTests: XCTestCase {
290289
func verify(using _: some JWTAlgorithm) throws {}
291290
}
292291

293-
// creates a new application for testing
294-
let app = Application(.testing)
295-
defer { app.shutdown() }
296-
297292
let privateKeyString = """
298293
-----BEGIN RSA PRIVATE KEY-----
299294
MIIEowIBAAKCAQEAhAHFb1M+P7qjwVlR7Es/3GBq3yICZP1eZ/JShBuLO4stTGHR
@@ -324,8 +319,8 @@ class JWTTests: XCTestCase {
324319
-----END RSA PRIVATE KEY-----
325320
"""
326321

327-
try await app.jwt.keys.addRSA(
328-
key: Insecure.RSA.PrivateKey(pem: [UInt8](privateKeyString.utf8)),
322+
try await app.jwt.keys.add(
323+
rsa: Insecure.RSA.PrivateKey(pem: [UInt8](privateKeyString.utf8)),
329324
digestAlgorithm: .sha256
330325
)
331326

@@ -336,15 +331,11 @@ class JWTTests: XCTestCase {
336331
}
337332

338333
for _ in 0 ..< 1000 {
339-
try app.test(.GET, "/") { res in
334+
try await app.test(.GET, "/") { res async in
340335
XCTAssertEqual(res.status, .ok)
341336
}
342337
}
343338
}
344-
345-
override func setUp() {
346-
XCTAssert(isLoggingConfigured)
347-
}
348339
}
349340

350341
extension ByteBuffer {

0 commit comments

Comments
 (0)