azure-ad-verify-token
Version:
Verify JWT issued by Azure Active Directory B2C.
74 lines (73 loc) • 1.32 kB
JavaScript
import { getConfig } from './config.js';
/**
* Public key cache.
*/
const cache = new Map();
/**
* Get expiry.
*/
function getExpiry() {
const now = new Date().getTime();
const config = getConfig();
return now + config.cacheLifetime;
}
/**
* Set cache item.
*
* @param key Cache item key.
* @param value Cache item value.
*/
export function setItem(key, value) {
return cache.set(key, {
result: Promise.resolve(value),
expiry: getExpiry(),
});
}
/**
* Set deferred cache item.
*
* @param key Cache item key.
*/
export function setDeferredItem(key) {
let done;
const result = new Promise((resolve) => {
done = resolve;
});
return cache.set(key, {
result,
done,
expiry: getExpiry(),
});
}
/**
* Get cache item.
*
* @param key Cache item key.
*/
export function getItem(key) {
const value = cache.get(key);
const now = new Date().getTime();
if (!value) {
return null;
}
if (value.expiry < now) {
// expired
cache.delete(key);
return null;
}
return value;
}
/**
* Remove cache item.
*
* @param key Cache item key.
*/
export function removeItem(key) {
return cache.delete(key);
}
/**
* Clear all items.
*/
export function clear() {
cache.clear();
}