jwks-client-browser

Library to retrieve public key from a JWKS endpoint in the browser.

Usage no npm install needed!

<script type="module">
  import jwksClientBrowser from 'https://cdn.skypack.dev/jwks-client-browser';
</script>

README

jwks-client-browser npm

CircleCI codecov Dependencies install size npm downloads Known Vulnerabilities NPM

This pacakge was created, because the current JWKS clients available only work in node, but not in the browser. This client is specifically designed to work in the browser. Given a JWKS endpoint URL and a kid it will retrieve the public key that you can use for JWT verification.

Installation

npm i jwks-client-browser

or

yarn add jwks-client-browser

Usage

JavaScript

import JwksClient from 'jwks-client-browser';

const kid = JSON.parse(atob(token.split('.')[0])).kid

const jwksClient = new JwksClient({ url: '[your_jwks_endpoint]' });

const signingKey = jwksClient.getSigningKey(kid);

TypeScript

import JwksClient, { ISigningKeyPem } from 'jwks-client-browser';

const kid: string = JSON.parse(atob(token.split('.')[0])).kid

const jwksClient: JwksClient = new JwksClient({ url: '[your_jwks_endpoint]' });

const signingKey: ISigningKeyPem = jwksClient.getSigningKey(kid);

Once you acquired the public key, you can use it to verify the token. Here's an example using the jsonwebtoken package:

import jwt from 'jsonwebtoken';

const signingKey: ISigningKeyPem = jwksClient.getSigningKey(kid);

jwt.verify(token, signingKey.publicKey);

Disclaimer

This is still a draft version of the package. Contributions are welcome!