0.2.0 • Published 3 years ago
cognito-jwt-verify v0.2.0
Cognito JWT Verify utility
Minimal AWS Cognito JWT token verify utilities built in Typescript.
Description
This package is intended to validate the JWTs tokens (id token and access token) released by cognito upon authenticating the user. The validation follows the official AWS documentation
Installation
Make sure you have Node >= 12.0.0 installed
npm install cognito-jwt-verify
Usage
The library is very simple and minimal to use. Can be used in two flavours: As a class or as a stand alone method
Class
import { CognitoJwtVerifier } from 'cognito-jwt-verify';
const verifier = new CognitoJwtVerifier(
'eu-south-1',
'cognitoUserPoolId',
'myuserappid',
false // Optional! if true, skip expiration verification for dev purposes
);
// Here you have your decoded token
const token = await verifier.verify(token);
Stand alone method
import { verifyCognitoToken } from 'cognito-jwt-verify';
// Pass the token that the Cognito API has given to you (either id or access one)
verifyCognitoToken(
'eu-south-1',
'cognitoUserPoolId',
token,
'myuserappid',
true // Optional! if true, skips expiration verification for dev purposes
).then(decodedToken => {
// Here you have your decoded token
}).catch(error => {
// Error while verifying the token
})
Or using the async-await construct
import { verifyCognitoToken } from 'cognito-jwt-verify';
var token = await verifyCognitoToken('eu-south-1', 'cognitoUserPoolId', token, 'myuserappid');
Features
- Validate token as per AWS documentation.
- Cache JWK for subsequent token validations.
- Handle errors.
Contributing
Keep the unit test coverage to 100!
Run:
npm run format
npm run lint
npm run test
Feel free to submit issues and enhancement requests 🚀🚀!.
License
MIT license, for more information pelase read LICENSE file