Schema Custom Validation Fails Checking If Is JWT Token
See original GitHub issueI have a schema validation like this one:
token: {
in: ['body', 'header'],
isJWT: true,
optional: false,
custom: {
options: async (value, { req }) => {
const token = req.body.token || req.headers.authentication&& req.headers.authentication.split(' ')[1]; // Authentication: Bearer {token}
const isValidJWT = validator.isJWT(token) // returns true
return canUseThisToken(token) // It's a boolean method from app, works
}
},
errorMessage: 'The token is invalid'
}
The problem is I noticed the isJWT
from schema does not validates correctly the token when it comes from the body, but not when it comes from the header. And I receive the error message. But the token is valid, using the same validation lib.
Once I remove the isJWT
from the schema validation, it works well.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to check error when validating jwt token using validate ...
According to my research, If you use HS256 signing algorithms, the key must be provided inline within the policy in the base64 encoded...
Read more >Troubleshooting JWT validation - Google Cloud
This page provides troubleshooting information if the JWT validation fails and ESP returns an error in the response to the client. See RFC...
Read more >JSON web token (JWT) validation - Akamai TechDocs
Based on your JWT claim configuration, API Gateway checks the token for presence of reserved and custom claims you specified as required.
Read more >Validate JSON Web Tokens - Auth0
For obtaining claims from JWT, use the verify() method to validate the claims and the signature. Avoid using the decode() method to validate...
Read more >.NET 6.0 - Create and Validate JWT Tokens + Use Custom ...
Below is custom JWT middleware that validates the JWT token contained in the request "Authorization" header (if it exists). On successful JWT ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah. So the token would need to be valid everywhere it is present. This is a security measure – see #331 for context.
So you seem to be doing some parsing to the token when it come from headers:
Bearer {token}
.This is not valid JWT. One option is to omit it from either
req.body
orreq.headers
. Or, you could use a custom sanitiser to split the value before it’s validated:Hope it helps 😃
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.