Füge myTischtennis-Verbindung hinzu: Implementiere Authentifizierung, Import von TTR-Werten und speichere Verbindungsdaten sicher
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 8m54s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped

This commit is contained in:
Torsten Schulz (local)
2026-09-04 12:45:22 +02:00
parent 3314aa06b4
commit 8f6fda2e61
10 changed files with 301 additions and 4 deletions

View File

@@ -0,0 +1,44 @@
import { promises as fs } from 'fs'
import { encryptObject, decryptObject } from './encryption.js'
import { getServerDataPath } from './paths.js'
const FILE = getServerDataPath('mytischtennis-connection.json')
function encryptionKey() {
const key = process.env.ENCRYPTION_KEY
if (!key) throw new Error('ENCRYPTION_KEY ist für die myTischtennis-Verbindung erforderlich.')
return key
}
export async function readMyTischtennisConnection() {
try {
const stored = JSON.parse(await fs.readFile(FILE, 'utf8'))
return decryptObject(stored.encrypted, encryptionKey())
} catch (error) {
if (error?.code === 'ENOENT') return null
throw error
}
}
export async function saveMyTischtennisConnection(connection) {
const payload = {
version: 1,
encrypted: encryptObject(connection, encryptionKey())
}
await fs.mkdir(getServerDataPath(), { recursive: true })
await fs.writeFile(FILE, `${JSON.stringify(payload)}\n`, { encoding: 'utf8', mode: 0o600 })
}
export function publicConnectionStatus(connection) {
if (!connection) return { configured: false }
const email = String(connection.email || '')
const [name, domain] = email.split('@')
return {
configured: true,
email: name ? `${name.slice(0, 2)}${'•'.repeat(Math.max(1, name.length - 2))}${domain ? `@${domain}` : ''}` : null,
association: connection.association || null,
clubId: connection.clubId || null,
lastSuccessfulImportAt: connection.lastSuccessfulImportAt || null,
lastImportError: connection.lastImportError || null
}
}