# cognito-jwt-verify

> Minimal AWS Cognito JWT token verify utilities built in Typescript.

Latest version **0.2.0** (published 2022-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install cognito-jwt-verify
pnpm add cognito-jwt-verify
yarn add cognito-jwt-verify
bun add cognito-jwt-verify
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2022-03-13 |
| First published | 2021-02-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 12.0.0 |
| Dependencies | 3 |
| Unpacked size | 19.5 KB |
| Known vulnerabilities | 0 (+26 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Giovanni Patruno |
| Maintainers | giopat |
| Keywords | aws, jwt, cognito |

## Links

- npm: https://www.npmjs.com/package/cognito-jwt-verify
- Repository: https://github.com/GioPat/cognito-jwt-verify
- Homepage: https://github.com/GioPat/cognito-jwt-verify#readme
- Issues: https://github.com/GioPat/cognito-jwt-verify/issues
- npm.io page: https://npm.io/package/cognito-jwt-verify

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) ^0.21.1
- [jwk-to-pem](https://npm.io/package/jwk-to-pem.md) ^2.0.4
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2022-03-13
- 0.1.4 — 2021-02-27
- 0.1.3 — 2021-02-27
- 0.1.2 — 2021-02-27
- 0.1.1 — 2021-02-27
- 0.1.0 — 2021-02-27

## README

# Cognito JWT Verify utility

Minimal AWS Cognito JWT token verify utilities built in Typescript.

[![npm Package][npm-image]][npm-url]
[![node version][node-image]][node-url]
[![Build Status][travis-image]][travis-url]
[![Coverage Status][coverage-image]][coverage-url]

[npm-image]:      https://img.shields.io/npm/v/cognito-jwt-verify.svg?style=flat-square
[npm-url]:        https://www.npmjs.com/package/cognito-jwt-verify
[node-image]:     https://img.shields.io/badge/node.js-%3E=_12.0.0-green.svg?style=flat-square
[node-url]:       http://nodejs.org/download/
[travis-image]:   https://travis-ci.org/GioPat/cognito-jwt-verify.svg?branch=master
[travis-url]:     https://travis-ci.org/github/GioPat/cognito-jwt-verify
[coverage-image]: https://coveralls.io/repos/github/GioPat/cognito-jwt-verify/badge.svg?branch=master
[coverage-url]:   https://coveralls.io/github/GioPat/cognito-jwt-verify?branch=master

## 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](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html)

## 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

```ts
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

```ts
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

```ts
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:

```sh
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

---
_Source: https://npm.io/package/cognito-jwt-verify · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
