0.7.2 • Published 4 months ago

mharj-jwt-util v0.7.2

Weekly downloads
14
License
MIT
Repository
github
Last release
4 months ago

mharj-jwt-util

Build Status Azure DevOps coverage 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

// 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

await useCache(new FileCertCache({fileName: './certCache.json'}));

// or with Tachyon storage driver
await useCache(new TachyonCertCache(new FileStorageDriver('FileCertCacheDriver', './certCache.json', certCacheBufferSerializer)));

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

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 anyObjectSchema = z.object({}).passthrough(); // or build token payload schema
const bufferSerializer = buildTokenCacheBufferSerializer<TokenPayload>(anyObjectSchema);
// const stringSerializer = buildTokenCacheStringSerializer<TokenPayload>(anyObjectSchema); // if using string based Tachyon drivers
const processor = new CryptoBufferProcessor(Buffer.from('some-secret-key'));
const driver = new FileStorageDriver('TokenStorageDriver', {fileName: './tokenCache.aes'}, bufferSerializer, processor);
const cache = new TachyonExpireCache<TokenPayload, RawJwtToken>('TachyonExpireCache', driver);
setTokenCache(cache);
0.7.2

4 months ago

0.7.1

4 months ago

0.6.2

8 months ago

0.7.0

7 months ago

0.6.1

8 months ago

0.6.0

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.5.0

2 years ago

0.4.1

2 years ago

0.3.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.4.2

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.8

2 years ago

0.2.5

3 years ago

0.2.3

3 years ago

0.2.4

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.2

3 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago