Member registration fixed
This commit is contained in:
@@ -2,6 +2,7 @@ import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { requireUserWithAnyRole } from '../../utils/auth.js'
|
||||
import { decryptObject } from '../../utils/encryption.js'
|
||||
import { getServerDataPath } from '../../utils/paths.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
@@ -17,7 +18,7 @@ export default defineEventHandler(async (event) => {
|
||||
})
|
||||
}
|
||||
|
||||
const dataDir = path.join(process.cwd(), 'server/data/membership-applications')
|
||||
const dataDir = getServerDataPath('membership-applications')
|
||||
|
||||
// Prüfen ob Verzeichnis existiert
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { createHmac, timingSafeEqual } from 'crypto'
|
||||
import { getUserFromToken } from '../../../utils/auth.js'
|
||||
import { getServerDataPath } from '../../../utils/paths.js'
|
||||
|
||||
@@ -47,7 +48,27 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Prüfen ob es sich um eine aktuelle Session handelt (innerhalb der letzten 24 Stunden)
|
||||
// Native apps cannot reliably reuse the httpOnly browser cookie that is
|
||||
// set when the application is created. They receive the same short-lived
|
||||
// authorization as a signed response token instead.
|
||||
const signedDownloadToken = getHeader(event, 'x-membership-download-token')
|
||||
if (signedDownloadToken) {
|
||||
try {
|
||||
const [payload, signature] = signedDownloadToken.split('.')
|
||||
const secret = process.env.ENCRYPTION_KEY || 'local_development_encryption_key_change_in_production'
|
||||
const expected = createHmac('sha256', secret).update(payload).digest('base64url')
|
||||
const validSignature = signature && timingSafeEqual(Buffer.from(signature), Buffer.from(expected))
|
||||
const decoded = JSON.parse(Buffer.from(payload, 'base64url').toString('utf8'))
|
||||
const tokenAge = Date.now() - Number(decoded.issuedAt)
|
||||
if (validSignature && decoded.fileId === fileId && tokenAge >= 0 && tokenAge < 24 * 60 * 60 * 1000) {
|
||||
isAuthorized = true
|
||||
}
|
||||
} catch (_error) {
|
||||
// Invalid download tokens are treated as unauthorized.
|
||||
}
|
||||
}
|
||||
|
||||
// Browser clients continue to use the httpOnly cookie.
|
||||
const downloadToken = getCookie(event, 'download_token')
|
||||
|
||||
if (downloadToken) {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { exec } from 'child_process'
|
||||
import { promisify } from 'util'
|
||||
import { createHmac } from 'crypto'
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'
|
||||
import { getDownloadCookieOptionsWithMaxAge } from '../../utils/cookies.js'
|
||||
import { sendMembershipEmail as sendMembershipEmailUtil } from '../../utils/email-service.js'
|
||||
import { getProjectPath, getServerDataPath } from '../../utils/paths.js'
|
||||
import { createMembershipApplication, removeMembershipApplication } from '../../utils/membership-applications.js'
|
||||
|
||||
// const require = createRequire(import.meta.url) // Nicht verwendet
|
||||
const execAsync = promisify(exec)
|
||||
@@ -310,9 +312,18 @@ function getDataPath(filename) {
|
||||
return getServerDataPath(filename)
|
||||
}
|
||||
|
||||
function createMembershipDownloadToken(fileId) {
|
||||
const issuedAt = Date.now()
|
||||
const payload = Buffer.from(JSON.stringify({ fileId, issuedAt })).toString('base64url')
|
||||
const secret = process.env.ENCRYPTION_KEY || 'local_development_encryption_key_change_in_production'
|
||||
const signature = createHmac('sha256', secret).update(payload).digest('base64url')
|
||||
return `${payload}.${signature}`
|
||||
}
|
||||
|
||||
// Use central email service
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
let application = null
|
||||
try {
|
||||
const body = await readBody(event)
|
||||
|
||||
@@ -338,6 +349,10 @@ export default defineEventHandler(async (event) => {
|
||||
...body,
|
||||
isVolljaehrig
|
||||
}
|
||||
|
||||
// Persist the pending application before generating files or sending mail.
|
||||
// This also makes repeated taps and concurrent requests idempotently fail.
|
||||
application = await createMembershipApplication(data)
|
||||
|
||||
// Eindeutige Datei-ID generieren
|
||||
const timestamp = Date.now()
|
||||
@@ -630,9 +645,11 @@ export default defineEventHandler(async (event) => {
|
||||
success: true,
|
||||
message: 'Beitrittsformular erfolgreich aus Template erzeugt und E-Mail gesendet.',
|
||||
downloadUrl: `/api/membership/download/${filename}.pdf`,
|
||||
downloadToken: createMembershipDownloadToken(`${filename}.pdf`),
|
||||
emailSuccess: emailResult.success,
|
||||
emailMessage: emailResult.message,
|
||||
usedTemplate: true
|
||||
usedTemplate: true,
|
||||
applicationId: application.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -690,8 +707,10 @@ export default defineEventHandler(async (event) => {
|
||||
success: true,
|
||||
message: 'Beitrittsformular erfolgreich erstellt und E-Mail gesendet.',
|
||||
downloadUrl: `/api/membership/download/${filename}.pdf`,
|
||||
downloadToken: createMembershipDownloadToken(`${filename}.pdf`),
|
||||
emailSuccess: emailResult.success,
|
||||
emailMessage: emailResult.message,
|
||||
applicationId: application.id
|
||||
}
|
||||
|
||||
} catch (latexError) {
|
||||
@@ -726,16 +745,22 @@ export default defineEventHandler(async (event) => {
|
||||
success: true,
|
||||
message: 'Beitrittsformular erfolgreich erstellt und E-Mail gesendet (Fallback-Lösung).',
|
||||
downloadUrl: `/api/membership/download/${fallbackFilename}`,
|
||||
downloadToken: createMembershipDownloadToken(fallbackFilename),
|
||||
emailSuccess: emailResult.success,
|
||||
emailMessage: emailResult.message,
|
||||
applicationId: application.id
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
// A failed generation must not leave a pending application that prevents
|
||||
// the applicant from trying again.
|
||||
await removeMembershipApplication(application?.id)
|
||||
console.error('Fehler beim Generieren des PDFs:', error)
|
||||
if (error?.statusCode) throw error
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Fehler beim Generieren des PDFs'
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { requireUserWithAnyRole } from '../../utils/auth.js'
|
||||
import { decryptObject } from '../../utils/encryption.js'
|
||||
import { saveMember } from '../../utils/members.js'
|
||||
import { getServerDataPath } from '../../utils/paths.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
@@ -43,9 +43,9 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
|
||||
const dataDir = path.join(process.cwd(), 'server/data/membership-applications')
|
||||
const dataDir = getServerDataPath('membership-applications')
|
||||
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
|
||||
const filePath = path.join(dataDir, `${id}.json`)
|
||||
const filePath = getServerDataPath('membership-applications', `${id}.json`)
|
||||
|
||||
// Antrag laden
|
||||
const fileContent = await fs.readFile(filePath, 'utf8')
|
||||
@@ -67,6 +67,7 @@ export default defineEventHandler(async (event) => {
|
||||
const newMember = {
|
||||
firstName: decryptedData.vorname,
|
||||
lastName: decryptedData.nachname,
|
||||
geburtsdatum: decryptedData.geburtsdatum,
|
||||
email: decryptedData.email,
|
||||
phone: decryptedData.telefon_privat || decryptedData.telefon_mobil || '',
|
||||
address: `${decryptedData.strasse}, ${decryptedData.plz} ${decryptedData.ort}`,
|
||||
|
||||
@@ -221,14 +221,23 @@ export function normalizeDate(dateString) {
|
||||
}
|
||||
|
||||
// Check for duplicate member based on firstName, lastName, and geburtsdatum
|
||||
function findDuplicateMember(members, firstName, lastName, geburtsdatum) {
|
||||
const normalizedFirstName = (firstName || '').trim().toLowerCase()
|
||||
const normalizedLastName = (lastName || '').trim().toLowerCase()
|
||||
function normalizeIdentityPart(value) {
|
||||
return String(value || '')
|
||||
.normalize('NFKD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/[^a-zA-Z0-9]+/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
export function findDuplicateMember(members, firstName, lastName, geburtsdatum) {
|
||||
const normalizedFirstName = normalizeIdentityPart(firstName)
|
||||
const normalizedLastName = normalizeIdentityPart(lastName)
|
||||
const normalizedDate = normalizeDate(geburtsdatum)
|
||||
|
||||
return members.find(m => {
|
||||
const mFirstName = (m.firstName || '').trim().toLowerCase()
|
||||
const mLastName = (m.lastName || '').trim().toLowerCase()
|
||||
const mFirstName = normalizeIdentityPart(m.firstName)
|
||||
const mLastName = normalizeIdentityPart(m.lastName)
|
||||
const mDate = normalizeDate(m.geburtsdatum)
|
||||
|
||||
return mFirstName === normalizedFirstName &&
|
||||
@@ -307,4 +316,3 @@ export async function deleteMember(id) {
|
||||
await writeMembers(filtered)
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
105
server/utils/membership-applications.js
Normal file
105
server/utils/membership-applications.js
Normal file
@@ -0,0 +1,105 @@
|
||||
import { createHash, randomUUID } from 'crypto'
|
||||
import { promises as fs } from 'fs'
|
||||
import { decryptObject, encryptObject } from './encryption.js'
|
||||
import { readMembers, findDuplicateMember, normalizeDate } from './members.js'
|
||||
import { getServerDataPath } from './paths.js'
|
||||
|
||||
const APPLICATIONS_DIR = getServerDataPath('membership-applications')
|
||||
const LOCKS_DIR = getServerDataPath('membership-application-locks')
|
||||
|
||||
function encryptionKey() {
|
||||
return process.env.ENCRYPTION_KEY || 'local_development_encryption_key_change_in_production'
|
||||
}
|
||||
|
||||
function normalizeName(value) {
|
||||
return String(value || '')
|
||||
.normalize('NFKD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/[^a-zA-Z0-9]+/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
function identityHash(data) {
|
||||
const identity = [normalizeName(data.vorname), normalizeName(data.nachname), normalizeDate(data.geburtsdatum)].join('|')
|
||||
return createHash('sha256').update(identity).digest('hex')
|
||||
}
|
||||
|
||||
async function withIdentityLock(hash, operation) {
|
||||
await fs.mkdir(LOCKS_DIR, { recursive: true })
|
||||
const lockPath = `${LOCKS_DIR}/${hash}.lock`
|
||||
let handle
|
||||
try {
|
||||
handle = await fs.open(lockPath, 'wx')
|
||||
} catch (error) {
|
||||
if (error?.code === 'EEXIST') {
|
||||
throw createError({ statusCode: 409, statusMessage: 'Für diese Person wird bereits ein Mitgliedschaftsantrag bearbeitet.' })
|
||||
}
|
||||
throw error
|
||||
}
|
||||
try {
|
||||
return await operation()
|
||||
} finally {
|
||||
await handle.close().catch(() => {})
|
||||
await fs.unlink(lockPath).catch(() => {})
|
||||
}
|
||||
}
|
||||
|
||||
async function findMatchingApplication(data) {
|
||||
let files = []
|
||||
try {
|
||||
files = await fs.readdir(APPLICATIONS_DIR)
|
||||
} catch (error) {
|
||||
if (error?.code !== 'ENOENT') throw error
|
||||
return null
|
||||
}
|
||||
const target = identityHash(data)
|
||||
for (const file of files.filter(file => file.endsWith('.json'))) {
|
||||
try {
|
||||
const application = JSON.parse(await fs.readFile(`${APPLICATIONS_DIR}/${file}`, 'utf8'))
|
||||
if (application.identityHash === target) return application
|
||||
if (application.encryptedData) {
|
||||
const personalData = decryptObject(application.encryptedData, encryptionKey())
|
||||
if (identityHash(personalData) === target) return application
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Mitgliedschaftsantrag konnte bei der Duplikatprüfung nicht gelesen werden:', { file, message: error.message })
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export async function createMembershipApplication(data) {
|
||||
const hash = identityHash(data)
|
||||
if (!normalizeName(data.vorname) || !normalizeName(data.nachname) || !normalizeDate(data.geburtsdatum)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Vorname, Nachname und ein gültiges Geburtsdatum sind erforderlich.' })
|
||||
}
|
||||
return withIdentityLock(hash, async () => {
|
||||
const members = await readMembers()
|
||||
if (findDuplicateMember(members, data.vorname, data.nachname, data.geburtsdatum)) {
|
||||
throw createError({ statusCode: 409, statusMessage: 'Für diese Person besteht bereits eine Mitgliedschaft.' })
|
||||
}
|
||||
const existing = await findMatchingApplication(data)
|
||||
if (existing) {
|
||||
throw createError({ statusCode: 409, statusMessage: 'Für diese Person liegt bereits ein Mitgliedschaftsantrag vor.' })
|
||||
}
|
||||
await fs.mkdir(APPLICATIONS_DIR, { recursive: true })
|
||||
const application = {
|
||||
id: randomUUID(),
|
||||
timestamp: new Date().toISOString(),
|
||||
status: 'pending',
|
||||
identityHash: hash,
|
||||
metadata: { mitgliedschaftsart: data.mitgliedschaftsart },
|
||||
encryptedData: encryptObject(data, encryptionKey())
|
||||
}
|
||||
await fs.writeFile(`${APPLICATIONS_DIR}/${application.id}.json`, `${JSON.stringify(application, null, 2)}\n`, { encoding: 'utf8', flag: 'wx' })
|
||||
return application
|
||||
})
|
||||
}
|
||||
|
||||
export async function removeMembershipApplication(applicationId) {
|
||||
if (!applicationId) return
|
||||
await fs.unlink(`${APPLICATIONS_DIR}/${applicationId}.json`).catch(error => {
|
||||
if (error?.code !== 'ENOENT') throw error
|
||||
})
|
||||
}
|
||||
@@ -114,14 +114,14 @@ async function sendFcmMessage({ serviceAccount, accessToken, token, title, body,
|
||||
body: JSON.stringify({
|
||||
message: {
|
||||
token,
|
||||
notification: { title, body },
|
||||
data,
|
||||
android: {
|
||||
priority: 'high',
|
||||
notification: {
|
||||
channel_id: 'harheimer_tc_updates',
|
||||
click_action: 'OPEN_NEWS'
|
||||
}
|
||||
// Keep this as a data message. With a `notification` payload FCM
|
||||
// renders background messages itself, bypassing our notification
|
||||
// channel and HarheimerMessagingService. Data messages use the
|
||||
// same app-controlled channel while the app is foregrounded and
|
||||
// backgrounded.
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -206,7 +206,11 @@ export async function sendPushToUsers({ title, body, data = {}, predicate, bodyF
|
||||
}
|
||||
}
|
||||
if (changed) await writeUsers(users)
|
||||
return { sent, failed, removed, recipients, tokenCount, skipped: false }
|
||||
const result = { sent, failed, removed, recipients, tokenCount, skipped: false }
|
||||
// This makes an absent registration immediately visible in the production
|
||||
// log without exposing tokens or user data.
|
||||
console.info('FCM Push Ergebnis:', { failureLabel, ...result })
|
||||
return result
|
||||
}
|
||||
|
||||
export async function sendNewNewsPush(news) {
|
||||
|
||||
Reference in New Issue
Block a user