@@ -15,14 +15,27 @@ public final class JWTStorageMiddleware<Payload: JWTPayload>: Middleware {
15
15
/// See `Middleware.respond(to:chainingTo:)`.
16
16
public func respond( to request: Request , chainingTo next: Responder ) throws -> Future < Response > {
17
17
18
- // 1. Get the payload from the request
19
- // 2. Store the payload in the request
18
+ // 1. Get the JWT from the request
19
+ // 2. Verify the JWT
20
+ // 3. Store the payload in the request
20
21
// for easier access.
21
- // 3 . Fire the next responder.
22
+ // 4 . Fire the next responder.
22
23
23
- let payload : Payload = try request. payload ( )
24
+ // Extract the token from the request. It is expected to
25
+ // be in the `Authorization` header as a bearer: `Bearer ...`
26
+ guard let token = request. http. headers. bearerAuthorization? . token else {
27
+ throw Abort ( . badRequest, reason: " 'Authorization' header with bearer token is missing " )
28
+ }
24
29
30
+ // Get JWT service to verify the token with
31
+ let jwt = try request. make ( JWTService . self)
32
+ let data = Data ( token. utf8)
33
+
34
+ // Verify to token and store the payload in the request's private container.
35
+ let payload = try JWT < Payload > ( from: data, verifiedUsing: jwt. signer) . payload
25
36
try request. set ( " skelpo-payload " , to: payload)
37
+
38
+ // Fire the next responder in the chain.
26
39
return try next. respond ( to: request)
27
40
}
28
41
}
0 commit comments