fixes and patches n stuff

This commit is contained in:
tyuXX 2026-10-21 21:07:54 +00:00
parent 7c7ae8e051
commit 1af973d001
14 changed files with 571 additions and 268 deletions

View file

@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import {
DatabaseSchema,
StudentStore,
@ -9,11 +10,54 @@ import {
PrivateLessonsStore,
ExamReportDetail,
ExamStore,
ExamTopicWeakness
ExamTopicWeakness,
ExamQuestionItem
} from '../types';
const DB_FILE = path.resolve(process.cwd(), 'data', 'db.json');
// Derive 32-byte AES key from environment secret
function getEncryptionKey(): Buffer {
const secret = process.env.ENCRYPTION_SECRET || process.env.SESSION_SECRET || 'bekultur_secret_key_change_in_production';
return crypto.scryptSync(secret, 'bekultur_credential_salt', 32);
}
function encryptCredential(plaintext: string): string {
try {
const key = getEncryptionKey();
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
let encrypted = cipher.update(plaintext, 'utf8', 'hex');
encrypted += cipher.final('hex');
const authTag = cipher.getAuthTag().toString('hex');
return `enc:${iv.toString('hex')}:${authTag}:${encrypted}`;
} catch (err) {
console.error('Failed to encrypt credential:', err);
return plaintext;
}
}
function decryptCredential(stored: string): string {
if (!stored || !stored.startsWith('enc:')) {
// Legacy plaintext support
return stored;
}
try {
const parts = stored.split(':');
if (parts.length !== 4) return stored;
const [, ivHex, authTagHex, ciphertextHex] = parts;
const key = getEncryptionKey();
const decipher = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(ivHex, 'hex'));
decipher.setAuthTag(Buffer.from(authTagHex, 'hex'));
let decrypted = decipher.update(ciphertextHex, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
} catch (err) {
console.error('Failed to decrypt credential:', err);
return '';
}
}
export class LocalDatabase {
private static instance: LocalDatabase;
private cache: DatabaseSchema = { students: {} };
@ -60,7 +104,7 @@ export class LocalDatabase {
private save(): void {
try {
const tmpFile = `${DB_FILE}.tmp`;
const tmpFile = `${DB_FILE}.${process.pid}.${Date.now()}.tmp`;
fs.writeFileSync(tmpFile, JSON.stringify(this.cache, null, 2), 'utf-8');
fs.renameSync(tmpFile, DB_FILE);
} catch (err) {
@ -138,7 +182,8 @@ export class LocalDatabase {
public saveCredentials(studentId: string, password: string): void {
const student = this.getStudentStore(studentId);
student.rememberedCredentials = { studentId, password };
const encryptedPassword = encryptCredential(password);
student.rememberedCredentials = { studentId, password: encryptedPassword };
this.save();
}
@ -150,7 +195,11 @@ export class LocalDatabase {
public getRememberedCredentials(studentId: string): { studentId: string; password: string } | null {
const student = this.cache.students[studentId];
return student?.rememberedCredentials || null;
if (!student?.rememberedCredentials) return null;
return {
studentId: student.rememberedCredentials.studentId,
password: decryptCredential(student.rememberedCredentials.password)
};
}
public saveSyllabus(
@ -285,4 +334,26 @@ export class LocalDatabase {
const student = this.cache.students[studentId];
return student?.exams?.topicWeaknesses?.[examId] || null;
}
public saveExamQuestions(studentId: string, examId: string, questions: ExamQuestionItem[]): void {
const student = this.getStudentStore(studentId);
if (!student.exams) {
student.exams = {
lastSynced: new Date().toISOString(),
reports: {},
topicWeaknesses: {},
questions: {}
};
}
if (!student.exams.questions) {
student.exams.questions = {};
}
student.exams.questions[examId] = questions;
this.save();
}
public getExamQuestions(studentId: string, examId: string): ExamQuestionItem[] | null {
const student = this.cache.students[studentId];
return student?.exams?.questions?.[examId] || null;
}
}