-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from skelpo/ready-to-use
Make the middleware usable and add tests
- Loading branch information
Showing
7 changed files
with
128 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: test | ||
on: | ||
- pull_request | ||
jobs: | ||
#jwtmiddleware_macos: | ||
# runs-on: macos-latest | ||
# env: | ||
# DEVELOPER_DIR: /Applications/Xcode_11.4_beta.app/Contents/Developer | ||
# steps: | ||
# - uses: actions/checkout@v2 | ||
# - run: brew install vapor/tap/vapor-beta | ||
# - run: xcrun swift test --enable-test-discovery --sanitize=thread | ||
jwtmiddleware_xenial: | ||
container: | ||
image: vapor/swift:5.2-xenial | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- run: swift test --enable-test-discovery --sanitize=thread | ||
jwtmiddleware_bionic: | ||
container: | ||
image: vapor/swift:5.2-bionic | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- run: swift test --enable-test-discovery --sanitize=thread | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
/.build | ||
/Packages | ||
/*.xcodeproj | ||
/.swiftpm | ||
Package.resolved |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,86 @@ | ||
import XCTest | ||
import JWT | ||
import Vapor | ||
import XCTVapor | ||
import CNIOBoringSSL | ||
@testable import JWTMiddleware | ||
@testable import JWTKit | ||
|
||
struct TestPayload: JWTPayload { | ||
let id: UUID | ||
let exp: TimeInterval | ||
|
||
init(id: UUID, exp: TimeInterval) { | ||
self.id = id | ||
self.exp = exp | ||
} | ||
|
||
func verify(using signer: JWTSigner) throws { | ||
_ = SubjectClaim(value: self.id.uuidString) // Nothing to verify here. | ||
try ExpirationClaim(value: Date(timeIntervalSince1970: self.exp)).verifyNotExpired() | ||
} | ||
} | ||
|
||
final class JWTMiddlewareTests: XCTestCase { | ||
func testExample() {} | ||
|
||
static var allTests = [ | ||
("testExample", testExample), | ||
] | ||
var tester: Application! | ||
|
||
override func setUpWithError() throws { | ||
// CryptoKit only generates EC keys and I don't know how to turn the raw representation into JWKS. | ||
var exp: BIGNUM = .init(); CNIOBoringSSL_BN_set_u64(&exp, 0x10001) | ||
var rsa: RSA = .init(); CNIOBoringSSL_RSA_generate_key_ex(&rsa, 4096, &exp, nil) | ||
|
||
let dBytes: [UInt8] = .init(unsafeUninitializedCapacity: Int(CNIOBoringSSL_BN_num_bytes(rsa.d))) { $1 = CNIOBoringSSL_BN_bn2bin(rsa.d, $0.baseAddress!) } | ||
let nBytes: [UInt8] = .init(unsafeUninitializedCapacity: Int(CNIOBoringSSL_BN_num_bytes(rsa.n))) { $1 = CNIOBoringSSL_BN_bn2bin(rsa.n, $0.baseAddress!) } | ||
struct LocalJWKS: Codable { | ||
struct LocalJWK: Codable { let kty, d, e, n, use, kid, alg: String } | ||
let keys: [LocalJWK] | ||
} | ||
let keyset = LocalJWKS(keys: [.init(kty: "RSA", d: String(bytes: dBytes.base64URLEncodedBytes(), encoding: .utf8)!, e: "AQAB", n: String(bytes: nBytes.base64URLEncodedBytes(), encoding: .utf8)!, use: "sig", kid: "jwttest", alg: "RS256")]) | ||
let json = try JSONEncoder().encode(keyset) | ||
|
||
tester = Application(.testing) | ||
try tester.jwt.signers.use(jwksJSON: String(data: json, encoding: .utf8)!) | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
tester?.shutdown() | ||
} | ||
|
||
func testPayloadValidationUnexpired() throws { | ||
let testPayload = TestPayload(id: UUID(), exp: Date(timeIntervalSinceNow: 10.0).timeIntervalSince1970) | ||
|
||
tester.middleware.use(JWTMiddleware<TestPayload>()) | ||
tester.get("hello") { _ in "world" } | ||
|
||
let token = try tester.jwt.signers.sign(testPayload, kid: "jwttest") | ||
|
||
_ = try XCTUnwrap(tester.testable(method: .inMemory).test(.GET, "/hello", headers: ["Authorization": "Bearer \(token)"]) { res in | ||
XCTAssertEqual(res.body.string, "world") | ||
}) | ||
} | ||
|
||
func testPayloadValidationExpired() throws { | ||
let testPayload = TestPayload(id: UUID(), exp: Date(timeIntervalSinceNow: -10.0).timeIntervalSince1970) | ||
|
||
tester.middleware.use(JWTMiddleware<TestPayload>()) | ||
tester.get("hello") { _ in "world" } | ||
|
||
let token = try tester.jwt.signers.sign(testPayload, kid: "jwttest") | ||
|
||
_ = try XCTUnwrap(tester.testable(method: .inMemory).test(.GET, "/hello", headers: ["Authorization": "Bearer \(token)"]) { res in | ||
XCTAssertEqual(res.status, .unauthorized) | ||
|
||
struct JWTErrorResponse: Codable { | ||
let error: Bool | ||
let reason: String | ||
} | ||
|
||
guard let content = try? XCTUnwrap(res.content.decode(JWTErrorResponse.self)) else { | ||
return | ||
} | ||
XCTAssertEqual(content.error, true) | ||
XCTAssertEqual(content.reason, "exp claim verification failed: expired") | ||
}) | ||
} | ||
} |