Skip to content

Commit

Permalink
Stored JWT payload key in String.payloadKey static property
Browse files Browse the repository at this point in the history
  • Loading branch information
calebkleveter committed Dec 7, 2018
1 parent 774fd06 commit 9de3e38
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 8 additions & 2 deletions Sources/JWTAuthenticatable/BasicJWTAuthenticatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import Fluent
import Crypto
import Vapor

extension String {

/// The key for the JWT payload when it is stored in a Vapor `Request` object.
public static let payloadKey: String = "skelpo-payload"
}

/// Used to decode a request body in
/// `BasicJWTAuthenticatable.authBody(from:)`.
///
Expand Down Expand Up @@ -95,7 +101,7 @@ extension BasicJWTAuthenticatable {
// Store the model and payload in the request
// using the request's `privateContainer`.
try request.authenticate(model)
try request.set("skelpo-payload", to: payload)
try request.set(.payloadKey, to: payload)

return model
})
Expand Down Expand Up @@ -123,7 +129,7 @@ extension BasicJWTAuthenticatable {

// Store the payload and the model in the request
// for later access.
try request.set("skelpo-payload", to: authenticated.0)
try request.set(.payloadKey, to: authenticated.0)
try request.authenticate(authenticated.1)

return authenticated.1
Expand Down
2 changes: 1 addition & 1 deletion Sources/JWTMiddleware/JWTVerificationMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class JWTStorageMiddleware<Payload: JWTPayload>: Middleware {

// Verify to token and store the payload in the request's private container.
let payload = try JWT<Payload>(from: data, verifiedUsing: jwt.signer).payload
try request.set("skelpo-payload", to: payload)
try request.set(.payloadKey, to: payload)

// Fire the next responder in the chain.
return try next.respond(to: request)
Expand Down
4 changes: 2 additions & 2 deletions Sources/JWTMiddleware/Request+JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension Request {
/// request storage. This is because this method should _only_ be called
/// if a JWT compatible model has been authenticated through a `JWTMiddleware`.
public func payload<Payload: Decodable>(as payloadType: Payload.Type = Payload.self)throws -> Payload {
guard let payload = try self.get("skelpo-payload", as: Payload .self) else {
guard let payload = try self.get(.payloadKey, as: Payload .self) else {
throw Abort(.internalServerError, reason: "No JWTMiddleware has been registered for the current route.")
}
return payload
Expand All @@ -46,7 +46,7 @@ extension Request {
/// or some other error from encoding and decoding the payload.
public func payloadData<Payload, Object>(storedAs stored: Payload.Type, convertedTo objectType: Object.Type = Object.self)throws -> Object
where Payload: Encodable, Object: Decodable {
guard let payload = try self.get("skelpo-payload", as: Payload.self) else {
guard let payload = try self.get(.payloadKey, as: Payload.self) else {
throw Abort(.internalServerError, reason: "No JWTMiddleware has been registered for the current route.")
}

Expand Down

0 comments on commit 9de3e38

Please sign in to comment.