Skip to content

Commit

Permalink
v1.1.2 add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
frankleng committed Feb 7, 2022
1 parent 7a7b046 commit b98a72a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# cloudflare-cognito-jwt-verifier
JWT verifier for AWS Cognito running on Cloudflare Workers
A lightweight JWT verifier for AWS Cognito running on Cloudflare Workers.
This lib fetches and caches JWKS from AWS Cognito, and verifies the JWT token.


## Why
Cloudflare Workers runtime doesn't support Node.js core modules, which means we cannot use common libs like `jsonwebtoken`.

## Install
```shell
npm i --save cloudflare-cognito-jwt-verifier
```
```shell
yarn add cloudflare-cognito-jwt-verifier
```
## Usage
```javascript
import { getVerifier, JwtInvalidError } from 'cloudflare-cognito-jwt-verifier';

const { verify } = getVerifier({
appClientId: COGNITO_USER_POOL_CLIENT_ID,
awsRegion: 'us-east-1',
// see auth tokens
// https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html
tokenType: 'access',
userPoolId: COGNITO_USER_POOL_ID,
});

export async function verifyAuth(request: Request) {
const header = request.headers.get('Authorization');
if (!header) {
throw new JwtInvalidError();
}
return await verify(header);
}

addEventListener('fetch', (event) => {
event.passThroughOnException();
event.respondWith(async (event) => {
const auth = await verifyAuth(request);
const userId = auth?.payload.sub;
return new Response({});
});
});
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cloudflare-cognito-jwt-verifier",
"version": "1.1.1",
"version": "1.1.2",
"description": "JWT verifier for AWS Cognito running on Cloudflare Workers",
"main": "dist/index.js",
"typings": "dist/types/index.d.ts",
Expand Down

0 comments on commit b98a72a

Please sign in to comment.