@linkedmink/passport-mutual-key-challenge
Version:
Implements a Passport strategy to authenticate the public key of a user by issuing a dynamic generated challenge
51 lines • 1.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalChallengeCache = void 0;
/**
* A memory cache that can be used to store pending challenges. This will not work
* in a distributed system.
*/
class LocalChallengeCache {
/**
* @param ttlMs The number of milliseconds to keep the challenge in cache
* @default 120000
*/
constructor(ttlMs = 120000) {
this.ttlMs = ttlMs;
this.challengeMap = new Map();
this.clearTimeouts = new Map();
}
/**
* @param key The key to retrieve
* @return The cached challenge or null if the key is expired or does not exist
*/
get(key) {
const challenge = this.challengeMap.get(key);
if (!challenge) {
return null;
}
this.challengeMap.delete(key);
this.clearTimeouts.delete(key);
if (Date.now() - challenge.requestDateTime.getTime() > this.ttlMs) {
return null;
}
return challenge;
}
/**
* @param key The key to save
* @param value The value to save
*/
set(key, value) {
if (this.clearTimeouts.has(key)) {
this.clearTimeouts.delete(key);
}
this.challengeMap.set(key, value);
const timeout = setTimeout(() => {
this.challengeMap.delete(key);
this.clearTimeouts.delete(key);
}, this.ttlMs);
this.clearTimeouts.set(key, timeout);
}
}
exports.LocalChallengeCache = LocalChallengeCache;
//# sourceMappingURL=LocalChallengeCache.js.map