fix token keypair issue
This commit is contained in:
parent
311d7f1960
commit
e30f6b4142
@ -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);
|
||||
|
||||
@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user