# mharj-jwt-util

> JWT util

Latest version **0.8.1** (published 2026-09-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install mharj-jwt-util
pnpm add mharj-jwt-util
yarn add mharj-jwt-util
bun add mharj-jwt-util
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.8.1 |
| Published | 2026-09-19 |
| First published | 2019-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 128.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | mharj |
| Maintainers | mharj |
| Keywords | jwt, jsonwebtoken, openid, util |

## Links

- npm: https://www.npmjs.com/package/mharj-jwt-util
- Repository: https://github.com/mharj/mharj-jwt-util
- Homepage: https://github.com/mharj/mharj-jwt-util#readme
- Issues: https://github.com/mharj/mharj-jwt-util/issues
- npm.io page: https://npm.io/package/mharj-jwt-util

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.8.1 (latest) — 2026-09-19
- 0.8.0 — 2026-03-03
- 0.7.3 — 2026-01-31
- 0.7.2 — 2025-03-16
- 0.7.1 — 2025-03-14
- 0.7.0 — 2024-11-19
- 0.6.2 — 2024-11-11
- 0.6.1 — 2024-10-28
- 0.6.0 — 2024-05-19
- 0.5.2 — 2024-04-12
- 0.5.1 — 2024-01-23
- 0.5.0 — 2023-12-21
- 0.4.2 — 2023-07-23
- 0.4.1 — 2023-07-15
- 0.4.0 — 2023-07-15
- … 23 more at https://npm.io/package/mharj-jwt-util/versions

## README

# mharj-jwt-util

[![Build Status](https://mharj.visualstudio.com/mharj-jwt-util/_apis/build/status/mharj.mharj-jwt-util?branchName=master)](https://mharj.visualstudio.com/mharj-jwt-util/_build/latest?definitionId=3&branchName=master) ![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/mharj/mharj-jwt-util/3) [![Maintainability](https://api.codeclimate.com/v1/badges/a60873c223b5bafadb1f/maintainability)](https://codeclimate.com/github/mharj/mharj-jwt-util/maintainability)

## Json Webtoken Utility to validate OpenID tokens against issuer public ssl keys

- Can build public PEM cert from modulus + exponent (i.e. Google)
- Caches issuer OpenID configuration 24h
- New Token "kid" forces reloading jwks_uri data.

Note: if running NodeJS less than 18.0.0 you need to install and use cross-fetch polyfill

## Usage example

```javascript
// with Bearer header
try {
  const { body, isCached } = await jwtBearerVerify(req.headers.authorization);
} catch (err) {
  console.log(err);
}
// or Just token
try {
  const { body, isCached } = await jwtVerify(process.env.GOOGLE_ID_TOKEN);
} catch (err) {
  console.log(err);
}

// attach logger to see http requests (console and log4js should be working)
setJwtLogger(console);
```

## Enable public cert file caching

```javascript
const certCacheSchema = z.object({certs: z.record(z.string(), z.record(z.string(), z.string())), _ts: z.number()}) satisfies StandardSchemaV1<
	unknown,
	CertRecords
>;
await useCache(new FileCertCache({fileName: './certCache.json', schema: certCacheSchema}));

// or with Tachyon storage driver
await useCache(new TachyonCertCache(new FileStorageDriver({name: 'FileCertCacheDriver', fileName: './unitTestCache.json'}, certCacheBufferSerializer(certCacheSchema))));
```

## Enable verified token persist caching (Tachyon storage driver with encryption)

```typescript
import { z } from "zod";
import { TachyonExpireCache } from "tachyon-expire-cache";
import {
  CryptoBufferProcessor,
  FileStorageDriver,
} from "tachyon-drive-node-fs";
import { buildTokenCacheBufferSerializer, setTokenCache } from "mharj-jwt-util";

const tokenBodySchema = z.object({}).loose(); // or build token payload schema
const tokenCacheMapSchema = z.map(z.string().refine(isRawJwtToken), z.object({expires: z.number(), data: tokenBodySchema}));
const bufferSerializer = buildTokenCacheBufferSerializer(tokenCacheMapSchema);
// const stringSerializer = buildTokenCacheStringSerializer<TokenPayload>(tokenCacheMapSchema); // if using string based Tachyon drivers
const processor = new CryptoBufferProcessor(Buffer.from("some-secret-key"));
const driver = new FileStorageDriver({name: 'TokenStorageDriver', fileName: "./tokenCache.aes" }, bufferSerializer, processor);
const cache = new TachyonExpireCache<z.infer<typeof tokenBodySchema>, RawJwtToken>({name: 'TachyonExpireCache'}, driver);
setTokenCache(cache);
```

---
_Source: https://npm.io/package/mharj-jwt-util · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
