Skip to content

Commit

Permalink
Correctly conform JWTError to AbortError (#162)
Browse files Browse the repository at this point in the history
* Update SIWA test with new token and testing the body so we know when it expires

* Wrap JWTError for conformance

* Wrap more errors

* Better workaround

* Remove unused import

* Fix lint error

* Do it 'correctly'

* Fix linting
  • Loading branch information
0xTim authored Nov 16, 2024
1 parent 8ce7280 commit ec5a9d4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Package.resolved
DerivedData
.swiftpm
Tests/LinuxMain.swift
.vscode/
4 changes: 4 additions & 0 deletions Sources/JWT/JWTError+Vapor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ extension JWTError: @retroactive AbortError {
public var status: HTTPResponseStatus {
.unauthorized
}

@_implements(AbortError,reason) public var abortErrorReason: String {
self.reason ?? self.description
}
}
4 changes: 3 additions & 1 deletion Tests/JWTTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,17 @@ struct JWTTests {
var headers = HTTPHeaders()
headers.bearerAuthorization = .init(
token: """
eyJraWQiOiJmaDZCczhDIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiZGV2LnRpbWMuc2l3YS1kZW1vLlRJTGlPUyIsImV4cCI6MTcwODUxNTY3NiwiaWF0IjoxNzA4NDI5Mjc2LCJzdWIiOiIwMDE1NDIuYjA0MTAwYzUxYWNiNDhkM2E1NzA2ODRmMTdkNjM5NGQuMTYwMyIsImNfaGFzaCI6ImFxQjM1RXR1bWFtVUg0VjZBYklmaXciLCJlbWFpbCI6Ijh5c2JjaHZjMm1AcHJpdmF0ZXJlbGF5LmFwcGxlaWQuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImlzX3ByaXZhdGVfZW1haWwiOnRydWUsImF1dGhfdGltZSI6MTcwODQyO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AMXSZTCli9HwRNeZikmbY6wBS5Ltg4VI5Z8Usge4eS2HINdHIPSCadYf858pZ8huAaj5Jm4t_5j988khwgqBMc9haTHZgiUpK7SZDePuRsAAQQVCXnRsuibxFX66ugo5BEEKCdK-xg66iAstb_mC_628gMrybC-_w
""")

try await app.test(.GET, "test", headers: headers) { res async in
#expect(res.status == .unauthorized)
#expect(res.body.string.contains("expired"))
}

try await app.test(.GET, "test2", headers: headers) { res async in
#expect(res.status == .unauthorized)
#expect(res.body.string.contains("expired"))
}
}
}
Expand Down

0 comments on commit ec5a9d4

Please sign in to comment.