jwt-validate

Validate JWT tokens in Node.js.
Installation
npm install jwt-validate
Usage
Validate a Microsoft Entra token
import { TokenValidator, getEntraJwksUri } from 'jwt-validate';
const entraJwksUri = await getEntraJwksUri();
const validator = new TokenValidator({
jwksUri: entraJwksUri
});
try {
const options = {
audience: '00000000-0000-0000-0000-000000000000',
issuer: 'https://login.microsoftonline.com/00000000-0000-0000-0000-000000000000/v2.0'
};
const validToken = await validator.validateToken(token, options);
}
catch (ex) {
console.error(ex);
}
Validate that the token is an application token
Validate that the token is an application token by checking the idtyp
claim. Requires the idtyp
claim to be present in the token.
import { TokenValidator, getEntraJwksUri } from 'jwt-validate';
const entraJwksUri = await getEntraJwksUri();
const validator = new TokenValidator({
jwksUri: entraJwksUri
});
try {
const options = {
idtyp: 'app'
};
const validToken = await validator.validateToken(token, options);
}
catch (ex) {
console.error(ex);
}
Validate that the token is a v2.0 token
import { TokenValidator, getEntraJwksUri } from 'jwt-validate';
const entraJwksUri = await getEntraJwksUri();
const validator = new TokenValidator({
jwksUri: entraJwksUri
});
try {
const options = {
ver: '2.0'
};
const validToken = await validator.validateToken(token, options);
}
catch (ex) {
console.error(ex);
}
API Reference
Classes
TokenValidator
Responsible for validating JWT tokens using JWKS (JSON Web Key Set).
Constructor
constructor(options)
- Parameters
options
: Object - Configuration options for the TokenValidator.
cache
: boolean (optional, default=true
) - Whether to cache the JWKS keys.
cacheMaxAge
: number (optional, default=86400000
) - The maximum age of the cache in milliseconds (default is 24 hours).
jwksUri
: string - The URI to fetch the JWKS keys from.
- Throws
Error
- If the options parameter is not provided.
Methods
Functions
getEntraJwksUri
- Description
- Gets the JWKS URL for the Microsoft Entra common tenant.
- Returns
Promise<string>
- The JWKS URI.
License
This project is licensed under the MIT License.