Created
November 4, 2022 16:44
-
-
Save saltukalakus/13fa1429d76405d983a233d96572c5b0 to your computer and use it in GitHub Desktop.
Verify JWT with rs256 signature
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
async function VerifyJWT (JwtToken) { | |
const util = require('util') | |
const jwksClientFactory = require('[email protected]') | |
const jwt = require('[email protected]') | |
const verify = util.promisify(jwt.verify) | |
const jwksUri = `https://${configuration.tenant}/.well-known/jwks.json` | |
const jwksClient = jwksClientFactory({ jwksUri }) | |
const getSigningKeys = util.promisify(jwksClient.getSigningKeys).bind(jwksClient) | |
const signingKeys = await getSigningKeys() | |
const { publicKey } = signingKeys[0] | |
let validJwtToken = false | |
try { | |
await verify(JwtToken, publicKey, { | |
algorithms: ['RS256'], | |
issuer: `https://${configuration.tenant}/`, | |
audience: configuration.API_AUDIENCE | |
}) | |
validJwtToken = true | |
} catch (err) { | |
console.log('JWT verify error:', err.message) | |
} | |
return validJwtToken | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment