Skip to content

Commit

Permalink
Merge pull request #24 from mihailstumkins/fix/incorrect-bearer-token…
Browse files Browse the repository at this point in the history
…-format

fix: parseJWT is now throwing AuthenticationError.invalidCredentials …
  • Loading branch information
tanner0101 authored Oct 25, 2017
2 parents 67e86ae + 04c8046 commit f648dd6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Sources/JWTProvider/JWTError+Status.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Vapor
import HTTP
import JWT


extension JWTError: AbortError {
public var status: Status {
switch self {
case .incorrectNumberOfSegments,
.incorrectPayloadForClaimVerification,
.missingAlgorithm,
.missingClaim,
.wrongAlgorithm,
.verificationFailedForClaim,
.signatureVerificationFailed:
return .unauthorized
default:
return .internalServerError
}
}
}

extension JWTError: Debuggable {
public var reason: String {
return self.description
}

public var identifier: String {
return self.description
}

public var possibleCauses: [String] {
return []
}

public var suggestedFixes: [String] {
return []
}
}

20 changes: 20 additions & 0 deletions Tests/JWTProviderTests/PayloadAuthenticationMiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Vapor
import Transport
import HTTP
import JWT
import AuthProvider

@testable import JWTProvider

class PayloadAuthenticationMiddlewareTests: XCTestCase {
Expand All @@ -14,6 +16,7 @@ class PayloadAuthenticationMiddlewareTests: XCTestCase {
("testAuthenticateWithIdentifiedToken", testAuthenticateWithIdentifiedToken),
("testAuthenticateWithIdentifiedTokenWithNoMatchingSigner", testAuthenticateWithIdentifiedTokenWithNoMatchingSigner),
("testAuthenticateWithJWKSURL", testAuthenticateWithJWKSURL),
("testAuthenticateWithNonParsableToken", testAuthenticateWithNonParsableToken)
]

func testAuthenticateWithLegacySigner() throws {
Expand Down Expand Up @@ -106,6 +109,23 @@ class PayloadAuthenticationMiddlewareTests: XCTestCase {

_ = try middleware.respond(to: request, chainingTo: MockResponder())
}


func testAuthenticateWithNonParsableToken() throws {

let request = Request(
method: .get,
uri: "http://localhost/test",
headers: [HeaderKey.authorization: "Bearer nonparsablejwttoken"]
)

let signers = ["1234": Unsigned(), "5678": Unsigned()]
let middleware = PayloadAuthenticationMiddleware<MockUser>(signers)

XCTAssertThrowsError(try middleware.respond(to: request, chainingTo: MockResponder()), "invalidCredentials") { error in
XCTAssertTrue((error as? JWTError)?.status == .unauthorized)
}
}
}

extension PayloadAuthenticationMiddlewareTests {
Expand Down

0 comments on commit f648dd6

Please sign in to comment.