fix token keypair issue

This commit is contained in:
murdle 2025-12-16 11:28:35 +02:00
parent 311d7f1960
commit e30f6b4142
2 changed files with 25 additions and 18 deletions

View File

@ -61,7 +61,7 @@
anchor.parentNode.insertBefore(infoSection, anchor);
}
infoText.style.marginTop = isNewSection ? "0px" : "5px";
infoText.style.marginTop = isNewSection ? "0px" : "10px";
infoSection.append(infoText, dateText);
} else {
anchor.parentNode.insertBefore(infoText, anchor);

View File

@ -149,40 +149,47 @@ export async function generateToken(id: string) {
// Get ECDSA keypair from file or generate it
export async function loadOrGenerateKeypair() {
let privateKey: crypto.KeyObject;
let publicKey: crypto.KeyObject;
let privateKey: string | crypto.KeyObject;
let publicKey: string | crypto.KeyObject;
if (existsSync("jwt.key") && existsSync("jwt.key.pub")) {
const [loadedPrivateKey, loadedPublicKey] = await Promise.all([
fs.readFile("jwt.key"),
fs.readFile("jwt.key.pub"),
fs.readFile("jwt.key", "utf-8"),
fs.readFile("jwt.key.pub", "utf-8"),
]);
privateKey = crypto.createPrivateKey({ key: loadedPrivateKey, type: "sec1" });
publicKey = crypto.createPublicKey({ key: loadedPublicKey, type: "spki" });
privateKey = loadedPrivateKey;
publicKey = loadedPublicKey;
} else {
console.log("[JWT] Generating new keypair");
const res = crypto.generateKeyPairSync("ec", {
namedCurve: "secp521r1",
publicKeyEncoding: {
type: "spki",
format: "pem"
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem"
}
});
privateKey = res.privateKey;
publicKey = res.publicKey;
await Promise.all([
fs.writeFile(
"jwt.key",
privateKey.export({ format: "pem", type: "sec1" }),
),
fs.writeFile(
"jwt.key.pub",
publicKey.export({ format: "pem", type: "spki" }),
),
fs.writeFile("jwt.key", res.privateKey),
fs.writeFile("jwt.key.pub", res.publicKey),
]);
privateKey = res.privateKey;
publicKey = res.publicKey;
}
const publicKeyForHash = typeof publicKey === 'string'
? crypto.createPublicKey(publicKey)
: publicKey;
const fingerprint = crypto
.createHash("sha256")
.update(publicKey.export({ format: "pem", type: "spki" }))
.update(publicKeyForHash.export({ format: "pem", type: "spki" }))
.digest("hex");
return { privateKey, publicKey, fingerprint };