Files
harheimertc/server/utils/webauthn-challenges.js
Torsten Schulz (local) 3586704cce
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 10m29s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped
Update file permissions for multiple server and test files to executable mode
2026-07-15 08:45:12 +02:00

71 lines
2.0 KiB
JavaScript
Executable File

const regChallenges = globalThis.__HTC_WEBAUTHN_REG_CHALLENGES__ || new Map()
const authChallenges = globalThis.__HTC_WEBAUTHN_AUTH_CHALLENGES__ || new Map()
const preRegChallenges = globalThis.__HTC_WEBAUTHN_PRE_REG__ || new Map()
globalThis.__HTC_WEBAUTHN_REG_CHALLENGES__ = regChallenges
globalThis.__HTC_WEBAUTHN_AUTH_CHALLENGES__ = authChallenges
globalThis.__HTC_WEBAUTHN_PRE_REG__ = preRegChallenges
function nowMs() {
return Date.now()
}
function cleanup(map) {
const now = nowMs()
for (const [k, v] of map.entries()) {
if (!v || !v.expiresAt || v.expiresAt <= now) map.delete(k)
}
}
export function setRegistrationChallenge(userId, challenge, ttlMs = 5 * 60 * 1000) {
cleanup(regChallenges)
regChallenges.set(String(userId), { challenge, expiresAt: nowMs() + ttlMs })
}
export function getRegistrationChallenge(userId) {
cleanup(regChallenges)
const v = regChallenges.get(String(userId))
return v?.challenge || null
}
export function clearRegistrationChallenge(userId) {
regChallenges.delete(String(userId))
}
export function setAuthChallenge(challenge, ttlMs = 5 * 60 * 1000) {
cleanup(authChallenges)
authChallenges.set(String(challenge), { expiresAt: nowMs() + ttlMs })
}
export function consumeAuthChallenge(challenge) {
cleanup(authChallenges)
const key = String(challenge)
const v = authChallenges.get(key)
if (!v) return false
authChallenges.delete(key)
return true
}
export function setPreRegistration(registrationId, payload, ttlMs = 10 * 60 * 1000) {
cleanup(preRegChallenges)
preRegChallenges.set(String(registrationId), { payload, expiresAt: nowMs() + ttlMs })
}
export function consumePreRegistration(registrationId) {
cleanup(preRegChallenges)
const key = String(registrationId)
const v = preRegChallenges.get(key)
if (!v) return null
preRegChallenges.delete(key)
return v.payload || null
}
export function getPreRegistration(registrationId) {
cleanup(preRegChallenges)
const key = String(registrationId)
const v = preRegChallenges.get(key)
if (!v) return null
return v.payload || null
}