From a5f4258647ab507f6d46e1345544bfe7ac306cb2 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 10 Jul 2026 08:21:30 +0200 Subject: [PATCH] Add manager class for JWT keypair to have a startup hook to avoid a race condition --- src/util/util/Token.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/util/util/Token.ts b/src/util/util/Token.ts index d0dd7a81a..1fe2d71b8 100644 --- a/src/util/util/Token.ts +++ b/src/util/util/Token.ts @@ -190,10 +190,11 @@ export async function generateToken(id: string, isAdminSession: boolean = false) export class JwtKeypairManager { private static isLocked = false; - static #keypair: JwtKeypair; + static #keypair?: JwtKeypair; static #filesystemCheckInterval: NodeJS.Timeout; public static get keypair() { + if (!this.#keypair) throw new Error("JwtKeypairManager#keypair.get called before being initialized."); return this.#keypair; } @@ -256,8 +257,8 @@ export class JwtKeypairManager { if (!existsSync("jwt.key") || !existsSync("jwt.key.pub")) { console.log("[JWT] Keypair files disappeared... Saving them again."); await Promise.all([ - fs.writeFile("jwt.key", this.#keypair.privateKey.export({ format: "pem", type: "sec1" })), - fs.writeFile("jwt.key.pub", this.#keypair.publicKey.export({ format: "pem", type: "spki" })), + fs.writeFile("jwt.key", this.keypair.privateKey.export({ format: "pem", type: "sec1" })), + fs.writeFile("jwt.key.pub", this.keypair.publicKey.export({ format: "pem", type: "spki" })), ]); } } catch (e) {