0.5.2 • Published 27 days ago
mharj-jwt-util v0.5.2
mharj-jwt-util
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 file caching
await useCache(new FileCertCache({fileName: './certCache.json'}));
// or with Tachyon drive
await useCache(new TachyonCertCache(new FileStorageDriver('FileCertCacheDriver', './certCache.json', certCacheBufferSerializer)));
Enable verified token persist caching (Tachyon drive with encryption)
import {CacheMap, TachyonExpireCache} from 'tachyon-expire-cache';
import {CryptoBufferProcessor, FileStorageDriver} from 'tachyon-drive-node-fs';
import {IPersistSerializer} from 'tachyon-drive';
function cachePayloadSchema<T>(data: z.Schema<T>) {
return z.object({
data,
expires: z.number().optional(),
});
}
const anyObjectSchema = z.object({}).passthrough(); // or build token payload schema
const bufferSerializer: IPersistSerializer<CacheMap<TokenPayload, RawJwtToken>, Buffer> = {
serialize: (data: CacheMap<TokenPayload, RawJwtToken>) => Buffer.from(JSON.stringify(Array.from(data))),
deserialize: (buffer: Buffer) => new Map(JSON.parse(buffer.toString())),
validator: (data: CacheMap<TokenPayload, RawJwtToken>) => z.map(z.string(), cachePayloadSchema(anyObjectSchema)).safeParse(data).success,
};
const processor = new CryptoBufferProcessor(Buffer.from('some-secret-key'));
const driver = new FileStorageDriver('TokenStorageDriver', './tokenCache.aes', bufferSerializer, processor);
const cache = new TachyonExpireCache<TokenPayload, RawJwtToken>(driver);
await setTokenCache(cache);
0.5.2
27 days ago
0.5.1
4 months ago
0.5.0
5 months ago
0.4.1
10 months ago
0.3.2
10 months ago
0.4.0
10 months ago
0.3.1
11 months ago
0.4.2
10 months ago
0.2.7
1 year ago
0.2.6
1 year ago
0.2.8
1 year ago
0.2.5
2 years ago
0.2.3
2 years ago
0.2.4
2 years ago
0.2.1
2 years ago
0.2.0
2 years ago
0.2.2
2 years ago
0.1.6
3 years ago
0.1.5
3 years ago
0.1.4
3 years ago
0.1.3
4 years ago
0.1.2
4 years ago
0.1.1
4 years ago
0.1.0
4 years ago
0.0.5
5 years ago
0.0.4
5 years ago
0.0.3
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago