Füge myTischtennis-Verbindung hinzu: Implementiere Authentifizierung, Import von TTR-Werten und speichere Verbindungsdaten sicher
This commit is contained in:
44
server/utils/mytischtennis-connection.js
Normal file
44
server/utils/mytischtennis-connection.js
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user