Skip to content

Commit 466838e

Browse files
Merge pull request #17 from skelpo/develop
Version 0.9.0
2 parents 736409a + 9de3e38 commit 466838e

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

Sources/JWTAuthenticatable/BasicJWTAuthenticatable.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import Fluent
55
import Crypto
66
import Vapor
77

8+
extension String {
9+
10+
/// The key for the JWT payload when it is stored in a Vapor `Request` object.
11+
public static let payloadKey: String = "skelpo-payload"
12+
}
13+
814
/// Used to decode a request body in
915
/// `BasicJWTAuthenticatable.authBody(from:)`.
1016
///
@@ -95,7 +101,7 @@ extension BasicJWTAuthenticatable {
95101
// Store the model and payload in the request
96102
// using the request's `privateContainer`.
97103
try request.authenticate(model)
98-
try request.set("skelpo-payload", to: payload)
104+
try request.set(.payloadKey, to: payload)
99105

100106
return model
101107
})
@@ -123,7 +129,7 @@ extension BasicJWTAuthenticatable {
123129

124130
// Store the payload and the model in the request
125131
// for later access.
126-
try request.set("skelpo-payload", to: authenticated.0)
132+
try request.set(.payloadKey, to: authenticated.0)
127133
try request.authenticate(authenticated.1)
128134

129135
return authenticated.1

Sources/JWTMiddleware/JWTVerificationMiddleware.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class JWTStorageMiddleware<Payload: JWTPayload>: Middleware {
2424
// Extract the token from the request. It is expected to
2525
// be in the `Authorization` header as a bearer: `Bearer ...`
2626
guard let token = request.http.headers.bearerAuthorization?.token else {
27-
throw Abort(.badRequest, reason: "'Authorization' header with bearer token is missing")
27+
throw Abort(.unauthorized, reason: "'Authorization' header with bearer token is missing")
2828
}
2929

3030
// Get JWT service to verify the token with
@@ -33,7 +33,7 @@ public final class JWTStorageMiddleware<Payload: JWTPayload>: Middleware {
3333

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

3838
// Fire the next responder in the chain.
3939
return try next.respond(to: request)
@@ -56,7 +56,7 @@ public final class JWTVerificationMiddleware: Middleware {
5656
// Extract the token from the request. It is expected to
5757
// be in the `Authorization` header as a bearer: `Bearer ...`
5858
guard let token = request.http.headers.bearerAuthorization?.token else {
59-
throw Abort(.badRequest, reason: "'Authorization' header with bearer token is missing")
59+
throw Abort(.unauthorized, reason: "'Authorization' header with bearer token is missing")
6060
}
6161

6262
// Get JWT service to verify the token with

Sources/JWTMiddleware/RouteRestrictionMiddleware.swift renamed to Sources/JWTMiddleware/PermissionsMiddleware.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public protocol PermissionedUserPayload: IdentifiableJWTPayload {
1818

1919
/// Verifies incoming request's authentication payload status
2020
/// against pre-defined allowed statuses.
21-
public final class PermissionsMiddleware<Status, Payload>: Middleware where Payload: PermissionedUserPayload, Status == Payload.Status {
21+
public final class PermissionsMiddleware<Payload>: Middleware where Payload: PermissionedUserPayload {
2222

2323
/// All the restrictions to check against the
2424
/// incoming request. Only one restriction must
2525
/// pass for the request to validated.
26-
public let statuses: [Status]
26+
public let statuses: [Payload.Status]
2727

2828
/// The status code to throw if no restriction passes.
2929
public let failureError: HTTPStatus
@@ -34,7 +34,7 @@ public final class PermissionsMiddleware<Status, Payload>: Middleware where Payl
3434
/// - statuses: An array of valid permission statuses.
3535
/// - failureError: The HTTP status to throw if all restrictions fail. The default
3636
/// value is `.notFound` (404). `.unauthorized` (401) would be another common option.
37-
public init(allowed statuses: [Status], failureError: HTTPStatus = .notFound) {
37+
public init(allowed statuses: [Payload.Status], failureError: HTTPStatus = .notFound) {
3838
self.statuses = statuses
3939
self.failureError = failureError
4040
}

Sources/JWTMiddleware/Request+JWT.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension Request {
2727
/// request storage. This is because this method should _only_ be called
2828
/// if a JWT compatible model has been authenticated through a `JWTMiddleware`.
2929
public func payload<Payload: Decodable>(as payloadType: Payload.Type = Payload.self)throws -> Payload {
30-
guard let payload = try self.get("skelpo-payload", as: Payload .self) else {
30+
guard let payload = try self.get(.payloadKey, as: Payload .self) else {
3131
throw Abort(.internalServerError, reason: "No JWTMiddleware has been registered for the current route.")
3232
}
3333
return payload
@@ -46,7 +46,7 @@ extension Request {
4646
/// or some other error from encoding and decoding the payload.
4747
public func payloadData<Payload, Object>(storedAs stored: Payload.Type, convertedTo objectType: Object.Type = Object.self)throws -> Object
4848
where Payload: Encodable, Object: Decodable {
49-
guard let payload = try self.get("skelpo-payload", as: Payload.self) else {
49+
guard let payload = try self.get(.payloadKey, as: Payload.self) else {
5050
throw Abort(.internalServerError, reason: "No JWTMiddleware has been registered for the current route.")
5151
}
5252

0 commit comments

Comments
 (0)