Skip to content

Commit

Permalink
Update package for new JWTKit API
Browse files Browse the repository at this point in the history
  • Loading branch information
ptoffy committed Feb 19, 2024
1 parent 2e6de9b commit 3837d63
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Sources/JWT/Request+JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public extension Request {
try await self._request.application.jwt.keys.verify(message, as: Payload.self)
}

public func sign<Payload>(_ jwt: Payload, kid: JWKIdentifier? = nil) async throws -> String
public func sign<Payload>(_ jwt: Payload, header: JWTHeader = .init()) async throws -> String
where Payload: JWTPayload
{
try await self._request.application.jwt.keys.sign(jwt, kid: kid)
return try await self._request.application.jwt.keys.sign(jwt, header: header)
}
}
}
12 changes: 6 additions & 6 deletions Tests/JWTTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class JWTTests: XCTestCase {
)
// Return the signed JWT
return try await [
"token": req.jwt.sign(payload, kid: "a"),
"token": req.jwt.sign(payload, header: ["kid": "a"]),
]
}

Expand Down Expand Up @@ -124,7 +124,7 @@ class JWTTests: XCTestCase {
defer { app.shutdown() }

// configures an es512 signer using random key
try await app.jwt.keys.addES512(key: .generate())
await app.jwt.keys.addES512(key: ES512PrivateKey())

// jwt creation using req.jwt.sign
app.post("login") { req async throws -> LoginResponse in
Expand Down Expand Up @@ -166,7 +166,7 @@ class JWTTests: XCTestCase {

// create a token from a different signer
let fakeToken = try await JWTKeyCollection()
.addES512(key: .generate()).sign(TestUser(name: "bob"))
.addES512(key: ES512PrivateKey()).sign(TestUser(name: "bob"))
try app.testable().test(
.GET, "me", headers: ["authorization": "Bearer \(fakeToken)"]
) { res in
Expand All @@ -181,7 +181,7 @@ class JWTTests: XCTestCase {
defer { app.shutdown() }

// configures an es512 signer using random key
try await app.jwt.keys.addES512(key: .generate())
await app.jwt.keys.addES512(key: ES512PrivateKey())

// jwt creation using req.jwt.sign
app.post("login") { req async throws -> LoginResponse in
Expand Down Expand Up @@ -241,7 +241,7 @@ class JWTTests: XCTestCase {
}

// create a token from a different signer
let fakeToken = try await JWTKeyCollection().addES512(key: .generate()).sign(TestUser(name: "bob"))
let fakeToken = try await JWTKeyCollection().addES512(key: ES512PrivateKey()).sign(TestUser(name: "bob"))
try app.testable().test(
.GET, "me", headers: ["authorization": "Bearer \(fakeToken)"]
) { res in
Expand Down Expand Up @@ -318,7 +318,7 @@ class JWTTests: XCTestCase {
-----END RSA PRIVATE KEY-----
"""

try await app.jwt.keys.addRS512(key: .private(pem: [UInt8](privateKeyString.utf8)))
try await app.jwt.keys.addRS256(key: Insecure.RSA.PrivateKey(pem: [UInt8](privateKeyString.utf8)))

app.get { req async throws -> String in
let authorizationPayload = UserPayload(id: UUID(), userName: "John Smith")
Expand Down

0 comments on commit 3837d63

Please sign in to comment.