summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-07-10 08:21:30 +0200
committerRory& <root@rory.gay>2026-07-10 08:21:30 +0200
commita5f4258647ab507f6d46e1345544bfe7ac306cb2 (patch)
tree7f241bdb773f56bfdeafbf6542b87a9e16acb9d8
parentAdd JWT keypair init hooks to various services (diff)
downloadserver-ts-a5f4258647ab507f6d46e1345544bfe7ac306cb2.tar.xz
Add manager class for JWT keypair to have a startup hook to avoid a race condition
-rw-r--r--src/util/util/Token.ts7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/util/util/Token.ts b/src/util/util/Token.ts

index d0dd7a81..1fe2d71b 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) {