fix(andreknie.de): harden personal data lifecycle
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { Router } from 'express'
|
||||
import { contactLimiter } from '../middleware/rateLimiter.js'
|
||||
import { antiSpam } from '../middleware/antiSpam.js'
|
||||
import { createToken, verifyToken } from '../services/confirmationToken.js'
|
||||
import { sendConfirmationEmail, sendStakeholderNotification } from '../services/mailer.js'
|
||||
import { createToken, reserveToken, completeToken, releaseToken } from '../services/confirmationToken.js'
|
||||
import { sendConfirmationEmail, sendStakeholderNotification, MailerNotReadyError } from '../services/mailer.js'
|
||||
|
||||
const router = Router()
|
||||
|
||||
@@ -11,27 +11,34 @@ router.post('/', contactLimiter, antiSpam, async (req, res) => {
|
||||
const errors = {}
|
||||
if (!name || name.trim().length === 0) errors.name = 'Name ist erforderlich.'
|
||||
if (name && name.length > 100) errors.name = 'Max. 100 Zeichen.'
|
||||
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gültige E-Mail erforderlich.'
|
||||
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gueltige E-Mail erforderlich.'
|
||||
if (!message || message.trim().length === 0) errors.message = 'Nachricht ist erforderlich.'
|
||||
if (message && message.length > 2000) errors.message = 'Max. 2000 Zeichen.'
|
||||
|
||||
if (Object.keys(errors).length > 0) {
|
||||
return res.status(400).json({ errors })
|
||||
}
|
||||
if (Object.keys(errors).length > 0) return res.status(400).json({ errors })
|
||||
|
||||
const token = await createToken('contact', { name, email, message }, 24)
|
||||
await sendConfirmationEmail(email, 'contact', token)
|
||||
res.status(201).json({ ok: true, message: 'Bestätigungs-E-Mail gesendet.' })
|
||||
try {
|
||||
const token = await createToken('contact', { name, email, message }, 24)
|
||||
await sendConfirmationEmail(email, 'contact', token)
|
||||
res.status(201).json({ ok: true, message: 'Bestaetigungs-E-Mail gesendet.' })
|
||||
} catch (error) {
|
||||
res.status(error instanceof MailerNotReadyError ? 503 : 502).json({ error: 'Die Anfrage konnte derzeit nicht versendet werden.' })
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/confirm/:token', async (req, res) => {
|
||||
const baseUrl = process.env.BASE_URL || ''
|
||||
const entry = await verifyToken(req.params.token)
|
||||
if (!entry) {
|
||||
return res.redirect(baseUrl + '/bestaetigung?status=error')
|
||||
const entry = await reserveToken(req.params.token)
|
||||
if (!entry) return res.redirect(baseUrl + '/bestaetigung?status=error')
|
||||
|
||||
try {
|
||||
await sendStakeholderNotification('contact', entry.data)
|
||||
await completeToken(req.params.token)
|
||||
res.redirect(baseUrl + '/bestaetigung?status=success')
|
||||
} catch (error) {
|
||||
await releaseToken(req.params.token)
|
||||
res.status(error instanceof MailerNotReadyError ? 503 : 502).redirect(baseUrl + '/bestaetigung?status=error')
|
||||
}
|
||||
await sendStakeholderNotification('contact', entry.data)
|
||||
res.redirect(baseUrl + '/bestaetigung?status=success')
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
@@ -5,8 +5,8 @@ import { fileURLToPath } from 'url'
|
||||
import { Mutex } from 'async-mutex'
|
||||
import { newsletterLimiter } from '../middleware/rateLimiter.js'
|
||||
import { antiSpam } from '../middleware/antiSpam.js'
|
||||
import { createToken, verifyToken } from '../services/confirmationToken.js'
|
||||
import { sendConfirmationEmail } from '../services/mailer.js'
|
||||
import { createToken, reserveToken, completeToken, releaseToken } from '../services/confirmationToken.js'
|
||||
import { sendConfirmationEmail, MailerNotReadyError } from '../services/mailer.js'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const SUBS_FILE = join(__dirname, '..', 'data', 'subscribers.json')
|
||||
@@ -27,38 +27,46 @@ const router = Router()
|
||||
router.post('/', newsletterLimiter, antiSpam, async (req, res) => {
|
||||
const { email } = req.body
|
||||
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
return res.status(400).json({ errors: { email: 'Gültige E-Mail erforderlich.' } })
|
||||
return res.status(400).json({ errors: { email: 'Gueltige E-Mail erforderlich.' } })
|
||||
}
|
||||
|
||||
// Check for existing subscription
|
||||
const subs = loadSubscribers()
|
||||
if (subs.find(s => s.email === email && s.active)) {
|
||||
return res.status(409).json({ error: 'Diese E-Mail ist bereits abonniert.' })
|
||||
}
|
||||
|
||||
const token = await createToken('newsletter', { email }, 48)
|
||||
await sendConfirmationEmail(email, 'newsletter', token)
|
||||
res.status(201).json({ ok: true, message: 'Bestätigungs-E-Mail gesendet.' })
|
||||
try {
|
||||
const token = await createToken('newsletter', { email }, 48)
|
||||
await sendConfirmationEmail(email, 'newsletter', token)
|
||||
res.status(201).json({ ok: true, message: 'Bestaetigungs-E-Mail gesendet.' })
|
||||
} catch (error) {
|
||||
res.status(error instanceof MailerNotReadyError ? 503 : 502).json({ error: 'Die Anfrage konnte derzeit nicht versendet werden.' })
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/confirm/:token', async (req, res) => {
|
||||
const baseUrl = process.env.BASE_URL || ''
|
||||
const entry = await verifyToken(req.params.token)
|
||||
if (!entry) {
|
||||
return res.redirect(baseUrl + '/bestaetigung?status=error')
|
||||
}
|
||||
const entry = await reserveToken(req.params.token)
|
||||
if (!entry) return res.redirect(baseUrl + '/bestaetigung?status=error')
|
||||
|
||||
await subsMutex.runExclusive(async () => {
|
||||
const subs = loadSubscribers()
|
||||
subs.push({
|
||||
email: entry.data.email,
|
||||
subscribedAt: new Date().toISOString(),
|
||||
active: true,
|
||||
try {
|
||||
await subsMutex.runExclusive(async () => {
|
||||
const subs = loadSubscribers()
|
||||
if (!subs.find(s => s.email === entry.data.email && s.active)) {
|
||||
subs.push({
|
||||
email: entry.data.email,
|
||||
subscribedAt: new Date().toISOString(),
|
||||
active: true,
|
||||
})
|
||||
saveSubscribers(subs)
|
||||
}
|
||||
})
|
||||
saveSubscribers(subs)
|
||||
})
|
||||
|
||||
res.redirect(baseUrl + '/bestaetigung?status=success')
|
||||
await completeToken(req.params.token)
|
||||
res.redirect(baseUrl + '/bestaetigung?status=success')
|
||||
} catch {
|
||||
await releaseToken(req.params.token)
|
||||
res.status(502).redirect(baseUrl + '/bestaetigung?status=error')
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
@@ -38,15 +38,22 @@ router.post('/', resourceLimiter, antiSpam, async (req, res) => {
|
||||
}
|
||||
|
||||
const lead = { name, email, message, timestamp: new Date().toISOString() }
|
||||
await leadsMutex.runExclusive(async () => {
|
||||
const leads = loadLeads()
|
||||
leads.push(lead)
|
||||
saveLeads(leads)
|
||||
})
|
||||
try {
|
||||
const result = await sendSpeakerCv(email, lead, PDF_PATH)
|
||||
if (result.pdfMissing) {
|
||||
return res.status(502).json({ error: 'Der Speaker CV ist derzeit nicht verfügbar.' })
|
||||
}
|
||||
|
||||
await sendSpeakerCv(email, lead, PDF_PATH)
|
||||
await leadsMutex.runExclusive(async () => {
|
||||
const leads = loadLeads()
|
||||
leads.push(lead)
|
||||
saveLeads(leads)
|
||||
})
|
||||
|
||||
res.status(201).json({ ok: true, message: 'Danke! Der Speaker CV ist auf dem Weg in dein Postfach.' })
|
||||
res.status(201).json({ ok: true, message: 'Danke! Der Speaker CV ist auf dem Weg in dein Postfach.' })
|
||||
} catch {
|
||||
res.status(503).json({ error: 'Der Versand ist derzeit nicht verfügbar.' })
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Router } from 'express'
|
||||
import { talkRequestLimiter } from '../middleware/rateLimiter.js'
|
||||
import { antiSpam } from '../middleware/antiSpam.js'
|
||||
import { createToken, verifyToken } from '../services/confirmationToken.js'
|
||||
import { sendConfirmationEmail, sendStakeholderNotification } from '../services/mailer.js'
|
||||
import { createToken, reserveToken, completeToken, releaseToken } from '../services/confirmationToken.js'
|
||||
import { sendConfirmationEmail, sendStakeholderNotification, MailerNotReadyError } from '../services/mailer.js'
|
||||
|
||||
const router = Router()
|
||||
|
||||
@@ -10,29 +10,36 @@ router.post('/', talkRequestLimiter, antiSpam, async (req, res) => {
|
||||
const { name, email, event_name, topic, message } = req.body
|
||||
const errors = {}
|
||||
if (!name || name.trim().length === 0) errors.name = 'Name ist erforderlich.'
|
||||
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gültige E-Mail erforderlich.'
|
||||
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gueltige E-Mail erforderlich.'
|
||||
if (!event_name || event_name.trim().length === 0) errors.event_name = 'Eventname ist erforderlich.'
|
||||
if (!topic || topic.trim().length === 0) errors.topic = 'Thema ist erforderlich.'
|
||||
if (!message || message.trim().length === 0) errors.message = 'Nachricht ist erforderlich.'
|
||||
if (message && message.length > 2000) errors.message = 'Max. 2000 Zeichen.'
|
||||
|
||||
if (Object.keys(errors).length > 0) {
|
||||
return res.status(400).json({ errors })
|
||||
}
|
||||
if (Object.keys(errors).length > 0) return res.status(400).json({ errors })
|
||||
|
||||
const token = await createToken('talk-request', { name, email, event_name, topic, message }, 48)
|
||||
await sendConfirmationEmail(email, 'talk-request', token)
|
||||
res.status(201).json({ ok: true, message: 'Bestätigungs-E-Mail gesendet.' })
|
||||
try {
|
||||
const token = await createToken('talk-request', { name, email, event_name, topic, message }, 48)
|
||||
await sendConfirmationEmail(email, 'talk-request', token)
|
||||
res.status(201).json({ ok: true, message: 'Bestaetigungs-E-Mail gesendet.' })
|
||||
} catch (error) {
|
||||
res.status(error instanceof MailerNotReadyError ? 503 : 502).json({ error: 'Die Anfrage konnte derzeit nicht versendet werden.' })
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/confirm/:token', async (req, res) => {
|
||||
const baseUrl = process.env.BASE_URL || ''
|
||||
const entry = await verifyToken(req.params.token)
|
||||
if (!entry) {
|
||||
return res.redirect(baseUrl + '/bestaetigung?status=error')
|
||||
const entry = await reserveToken(req.params.token)
|
||||
if (!entry) return res.redirect(baseUrl + '/bestaetigung?status=error')
|
||||
|
||||
try {
|
||||
await sendStakeholderNotification('talk-request', entry.data)
|
||||
await completeToken(req.params.token)
|
||||
res.redirect(baseUrl + '/bestaetigung?status=success')
|
||||
} catch (error) {
|
||||
await releaseToken(req.params.token)
|
||||
res.status(error instanceof MailerNotReadyError ? 503 : 502).redirect(baseUrl + '/bestaetigung?status=error')
|
||||
}
|
||||
await sendStakeholderNotification('talk-request', entry.data)
|
||||
res.redirect(baseUrl + '/bestaetigung?status=success')
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user