# azure-ad-verify-token

> Verify JWT issued by Azure Active Directory B2C.

Latest version **3.0.3** (published 2023-07-28) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install azure-ad-verify-token
pnpm add azure-ad-verify-token
yarn add azure-ad-verify-token
bun add azure-ad-verify-token
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.0.3 |
| Published | 2023-07-28 |
| First published | 2020-03-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 3 |
| Unpacked size | 12 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Justin Lettau |
| Maintainers | justinlettau |
| Keywords | azure, ad, b2c, jwt, verify, token |

## Links

- npm: https://www.npmjs.com/package/azure-ad-verify-token
- Repository: https://github.com/justinlettau/azure-ad-verify-token
- Issues: https://github.com/justinlettau/azure-ad-verify-token/issues
- npm.io page: https://npm.io/package/azure-ad-verify-token

## Dependencies (3)

- [node-fetch](https://npm.io/package/node-fetch.md) ^3.3.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.0
- [rsa-pem-from-mod-exp](https://npm.io/package/rsa-pem-from-mod-exp.md) ^0.8.5

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 3.0.3 (latest) — 2023-07-28
- 3.0.2 — 2023-06-26
- 3.0.1 — 2023-02-23
- 3.0.0 — 2023-01-26
- 2.0.1 — 2022-07-16
- 2.0.0 — 2021-10-29
- 1.1.2 — 2021-08-27
- 1.1.1 — 2020-11-28
- 1.1.0 — 2020-03-28
- 1.0.1 — 2020-03-22
- 1.0.0 — 2020-03-22

## README

[![NPM Version](https://badge.fury.io/js/azure-ad-verify-token.svg)](https://badge.fury.io/js/azure-ad-verify-token)
![CI](https://github.com/justinlettau/azure-ad-verify-token/workflows/CI/badge.svg)
[![Codecov](https://codecov.io/gh/justinlettau/azure-ad-verify-token/branch/master/graph/badge.svg)](https://codecov.io/gh/justinlettau/azure-ad-verify-token)

# Azure AD Verify Token

Verify JWT issued by Azure Active Directory B2C.

# Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [References](#references)
- [Development](#development)

# Features

- 🎉 **Verify JWT** issued by Azure Active Directory B2C.
- 🚀 Automatically use the **rotated public keys** from Azure.
- 💪 Written in **TypeScript**.
- ♻️ **Configurable cache** for public keys.

# Installation

```bash
npm install azure-ad-verify-token --save
```

# Usage

### Verify

```ts
import { verify, VerifyOptions } from 'azure-ad-verify-token';

const options: VerifyOptions = {
  jwksUri:
    'https://contoso.b2clogin.com/contoso.onmicrosoft.com/discovery/v2.0/keys?p=b2c_1_signupsignin1',
  issuer:
    'https://contoso.b2clogin.com/3285c484-dce5-4abb-a341-bbe4f2bc8554/v2.0/',
  audience: '99d1275c-e805-483f-b832-600f8130829c',
};

verify(token, options)
  .then((decoded) => {
    // verified and decoded token
    console.log(decoded);
  })
  .catch((error) => {
    // invalid token
    console.error(error);
  });
```

Verify options:

| Property   | Type     | Description                                                 |
| ---------- | -------- | ----------------------------------------------------------- |
| `jwksUri`  | `string` | `jwk_uri` value obtained from B2C policy metadata endpoint. |
| `issuer`   | `string` | `issuer` value obtained from B2C policy metadata endpoint.  |
| `audience` | `string` | Application ID of the application accessing the tenant.     |

Example metadata endpoints:

- https://login.microsoftonline.com/common/.well-known/openid-configuration
- https://login.microsoftonline.com/common/discovery/keys

### Configuration

```ts
import { setConfig } from 'azure-ad-verify-token';

setConfig({
  cacheLifetime: 12 * (60 * 60 * 1000), // 12 hours
});
```

Configuration options:

| Property        | Type     | Description                                  | Default |
| --------------- | -------- | -------------------------------------------- | ------- |
| `cacheLifetime` | `number` | Number of milliseconds to cache public keys. | 1 hour  |

# References

- [Overview of tokens in Azure Active Directory B2C](https://docs.microsoft.com/en-us/azure/active-directory-b2c/tokens-overview)
- [Microsoft identity platform access tokens](https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens)
- [RSA Key Converter](https://superdry.apphb.com/tools/online-rsa-key-converter)

# Development

```
npm install
npm run build
```

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