npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

azure-ad-jwt-lite

v1.2.1

Published

Lightweight (<320kb unzipped) library to validate Microsoft AzureAD. Written in typescript fully-tested with 100% code coverage.

Downloads

1,825

Readme

azure-ad-jwt-lite

npm bundle size npm Snyk Vulnerabilities for GitHub Repo

GitHub Workflow Status Coverage Duplicated Lines (%) Maintainability Rating Reliability Rating Security Rating Technical Debt Bugs Code Smells

Lightweight library to verify AzureAD JSON Web Tokens.

It weights around 12KB alone and less than 320KB with its only one dependeny: jsonwebtoken

Other libraries generally includes request and are bloated. I decided to write this lib because the previous helper I used weighted more than 4MB with all its dependencies!

Futhermore, it is written in typescript and provide its own type definitions.

Last but not least, it is unit tested with one-hundred percent test coverage.

Getting started

Install the package using yarn or NPM: npm i azure-ad-jwt-lite

Do not forget to install jsonwebtoken types definitions as dev dependency if you are using Typescript: npm i -D @types/jsonwebtoken.

In your authentication middleware decode and verify the token using:

import { verifyAzureToken } from 'azure-ad-jwt-lite';

const decoded = await verifyAzureToken(token);

You can add any option supported by jsonwebtoken:

import { verifyAzureToken } from 'azure-ad-jwt-lite';

const decoded = await verifyAzureToken(token, {
  audience: process.env.JWT_AUD,
  issuer: process.env.JWT_ISS,
});

Additional options

  • Discovery URL: The URL to fetch Microsoft public keys (defaults to https://login.microsoftonline.com/common/discovery/keys)

  • Retries on 5xx: set the number of retries when request to fetch keys returns a 5xx response (defaults to 2)

import { verifyAzureToken } from 'azure-ad-jwt-lite';

const decoded = await verifyAzureToken(token, {
  discoveryUrl: `https://login.microsoftonline.com/${process.env.TENANT}/discovery/keys?appid=${process.env.APP_ID}`,
  maxRetries: 5,
  audience: process.env.JWT_AUD,
  issuer: process.env.JWT_ISS,
});

Caching keys

Public keys from discovery endpoint calls are cached for a default TTL of 5 minutes.

You can disable caching using useCache: false in options, or modify TTL using cacheTtl option.

Error reference

The lib will throw the following errors if something wrong happends during decoding token:

  • InvalidToken: the token provided is not a non-empty string.
  • TokenNotDecoded: the token cannot be decoded. This usually means the token is ill-formed.
  • MissingKeyID: no kid (Microsoft Key ID) field is present in JWT header.
  • ErrorFetchingKeys: API call to fetch Microsoft public keys failed.
  • NotMatchingKey: no matching key is found in Microsoft response.
  • JsonWebTokenError: token cannot be verified, the human-readable reason is provided (expired, audience mismatch etc...)