Files
harheimertc/server/utils/passkey-recovery.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

22 lines
760 B
JavaScript
Executable File

import crypto from 'crypto'
export function hashRecoveryToken(token) {
return crypto.createHash('sha256').update(String(token), 'utf8').digest('hex')
}
export function generateRecoveryToken() {
// URL-safe (hex)
return crypto.randomBytes(32).toString('hex')
}
export function pruneRecoveryTokens(user, maxTokens = 10) {
const list = Array.isArray(user.passkeyRecoveryTokens) ? user.passkeyRecoveryTokens : []
const now = Date.now()
const filtered = list.filter(t => t && t.tokenHash && t.expiresAt && new Date(t.expiresAt).getTime() > now)
// keep newest first
filtered.sort((a, b) => new Date(b.createdAt || 0) - new Date(a.createdAt || 0))
user.passkeyRecoveryTokens = filtered.slice(0, maxTokens)
return user.passkeyRecoveryTokens
}