How to verify decoded token #625
-
I want to decode the token first to get an unverified claim out, then verify the token. Since I already decoded the token I want to avoid the cost of decoding it one more time during verification and just verify the already decoded token. Is there some way to do this? The reason for decoding first is that we use a tenant claim to fill in the url template for the jwksuri. I think of it something like this in code (does not work): const decodedToken = Jose.decodeJwt(encodedToken);
// Codes that builds jwksUri as a function of decoded token
const jwksUri = buildUrl(decodedToken);
const jwks = Jose.createRemoteJWKSet(new URL(jwksUri));
const { payload, protectedHeader } = await Jose.jwtVerify(decodedToken , jwks); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
There's compactVerify, but I recommend against it in your case since you're losing on validating the JWT Claims Set that, as documented, is not part of decodeJwt. You'd only be saving yourself one JSON.parse anyway so this is not worth it. |
Beta Was this translation helpful? Give feedback.
You're focusing entirely on the wrong optimization. What you want to focus on instead is ensuring that you're using a trusted jwksUri and then caching the result of
createRemoteJWKSet
, using the jwksUri as the cache key.Trusting any jwksUri is clearly a problem, you need to have an allow list or somehow ensure you want to accept a specific URL.
It has a minimal impact even when run tons of times.