-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve withApiKey
auth middleware
#226
base: main
Are you sure you want to change the base?
Conversation
andrii-balitskyi
commented
Nov 29, 2024
- Improve withApiKey auth middleware
- ci: Format code
- Test
Bundle |
const invalidKeyErr = await t.throwsAsync<SimpleAxiosError>( | ||
axios.get("/devices/list", { | ||
headers: { | ||
Authorization: "Bearer invalid_api_key", |
Check failure
Code scanning / CodeQL
Hard-coded credentials Critical test
authorization header
const clientSessionErr = await t.throwsAsync<SimpleAxiosError>( | ||
axios.get("/devices/list", { | ||
headers: { | ||
Authorization: `Bearer seam_cst1_123`, |
Check failure
Code scanning / CodeQL
Hard-coded credentials Critical test
authorization header
const accessTokenErr = await t.throwsAsync<SimpleAxiosError>( | ||
axios.get("/devices/list", { | ||
headers: { | ||
Authorization: `Bearer seam_at_123`, |
Check failure
Code scanning / CodeQL
Hard-coded credentials Critical test
authorization header
t.is(accessTokenErr?.response.error.type, "access_token_used_for_api_key") | ||
|
||
// Test using JWT instead of API key | ||
const token = jwt.sign({ some: "payload" }, "secret") |
Check failure
Code scanning / CodeQL
Hard-coded credentials Critical test
jwt key
let decodedJwt | ||
try { | ||
decodedJwt = jwt.decode(token) | ||
} catch {} | ||
if (decodedJwt != null) { | ||
throw new UnauthorizedException({ | ||
type: "unauthorized", | ||
message: "A JWT was used instead of an API key", | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block is incorrect in seam-connect but I fixed it here.
In seam-connect it'll never throw when JWT is provided because the error gets always caught being in the try block: https://github.com/seamapi/seam-connect/blob/486c564a7138bca4cd93ce51d6370bee4405fcbd/lib/middlewares/api-key-auth.ts#L54-L61.
Created an issue: https://github.com/seamapi/seam-connect/issues/9520