fix(andreknie.de): harden personal data lifecycle

This commit is contained in:
2026-07-28 11:13:27 +02:00
parent 772a63c622
commit b31da6175a
11 changed files with 276 additions and 124 deletions
@@ -9,7 +9,6 @@ const DATA_DIR = join(__dirname, '..', 'data')
const TOKENS_FILE = join(DATA_DIR, 'pending-tokens.json')
const tokenMutex = new Mutex()
// Ensure data directory exists
mkdirSync(DATA_DIR, { recursive: true })
function loadTokens() {
@@ -22,13 +21,6 @@ function saveTokens(tokens) {
writeFileSync(TOKENS_FILE, JSON.stringify(tokens, null, 2), 'utf-8')
}
/**
* Create a confirmation token.
* @param {string} type - 'contact' | 'talk-request' | 'newsletter'
* @param {object} data - The form data to store
* @param {number} expiresInHours - Token expiration (default: 48h)
* @returns {string} The generated token
*/
export async function createToken(type, data, expiresInHours = 48) {
const token = crypto.randomUUID()
await tokenMutex.runExclusive(async () => {
@@ -39,51 +31,64 @@ export async function createToken(type, data, expiresInHours = 48) {
data,
createdAt: new Date().toISOString(),
expiresAt: new Date(Date.now() + expiresInHours * 60 * 60 * 1000).toISOString(),
confirmed: false,
status: 'pending',
})
saveTokens(tokens)
})
return token
}
/**
* Verify and consume a token.
* @returns {object|null} The stored data if valid, null if expired/invalid
*/
export async function verifyToken(token) {
/** Reserve a valid token for exactly one confirmation flow. */
export async function reserveToken(token) {
return await tokenMutex.runExclusive(async () => {
const tokens = loadTokens()
const idx = tokens.findIndex(t => t.token === token && !t.confirmed)
const idx = tokens.findIndex(t => t.token === token && (t.status || (t.confirmed ? 'completed' : 'pending')) === 'pending')
if (idx === -1) return null
const entry = tokens[idx]
if (new Date(entry.expiresAt) < new Date()) {
// Expired — remove it
if (new Date(entry.expiresAt) <= new Date()) {
tokens.splice(idx, 1)
saveTokens(tokens)
return null
}
// Mark as confirmed
tokens[idx].confirmed = true
tokens[idx].status = 'processing'
saveTokens(tokens)
return entry
})
}
/**
* Clean up expired tokens (run periodically).
*/
/** Remove a successfully processed token and its personal payload. */
export async function completeToken(token) {
await tokenMutex.runExclusive(async () => {
const tokens = loadTokens()
const remaining = tokens.filter(t => t.token !== token)
if (remaining.length !== tokens.length) saveTokens(remaining)
})
}
/** Return a failed confirmation to pending so it can be retried. */
export async function releaseToken(token) {
await tokenMutex.runExclusive(async () => {
const tokens = loadTokens()
const entry = tokens.find(t => t.token === token)
if (!entry) return
entry.status = 'pending'
saveTokens(tokens)
})
}
// Compatibility for code that only needs the reservation behavior.
export const verifyToken = reserveToken
export async function cleanupExpiredTokens() {
await tokenMutex.runExclusive(async () => {
const tokens = loadTokens()
const now = new Date()
const valid = tokens.filter(t => new Date(t.expiresAt) > now || t.confirmed)
if (valid.length !== tokens.length) {
saveTokens(valid)
}
const valid = tokens.filter(t => !t.confirmed && new Date(t.expiresAt) > now)
if (valid.length !== tokens.length) saveTokens(valid)
})
}
// Run cleanup every hour
setInterval(cleanupExpiredTokens, 60 * 60 * 1000)
const cleanupTimer = setInterval(cleanupExpiredTokens, 60 * 60 * 1000)
cleanupTimer.unref?.()