-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
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
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({}); | ||
}); | ||
}); | ||
``` |
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