fix token keypair issue
This commit is contained in:
parent
311d7f1960
commit
e30f6b4142
@ -61,7 +61,7 @@
|
|||||||
anchor.parentNode.insertBefore(infoSection, anchor);
|
anchor.parentNode.insertBefore(infoSection, anchor);
|
||||||
}
|
}
|
||||||
|
|
||||||
infoText.style.marginTop = isNewSection ? "0px" : "5px";
|
infoText.style.marginTop = isNewSection ? "0px" : "10px";
|
||||||
infoSection.append(infoText, dateText);
|
infoSection.append(infoText, dateText);
|
||||||
} else {
|
} else {
|
||||||
anchor.parentNode.insertBefore(infoText, anchor);
|
anchor.parentNode.insertBefore(infoText, anchor);
|
||||||
|
|||||||
@ -149,40 +149,47 @@ export async function generateToken(id: string) {
|
|||||||
|
|
||||||
// Get ECDSA keypair from file or generate it
|
// Get ECDSA keypair from file or generate it
|
||||||
export async function loadOrGenerateKeypair() {
|
export async function loadOrGenerateKeypair() {
|
||||||
let privateKey: crypto.KeyObject;
|
let privateKey: string | crypto.KeyObject;
|
||||||
let publicKey: crypto.KeyObject;
|
let publicKey: string | crypto.KeyObject;
|
||||||
|
|
||||||
if (existsSync("jwt.key") && existsSync("jwt.key.pub")) {
|
if (existsSync("jwt.key") && existsSync("jwt.key.pub")) {
|
||||||
const [loadedPrivateKey, loadedPublicKey] = await Promise.all([
|
const [loadedPrivateKey, loadedPublicKey] = await Promise.all([
|
||||||
fs.readFile("jwt.key"),
|
fs.readFile("jwt.key", "utf-8"),
|
||||||
fs.readFile("jwt.key.pub"),
|
fs.readFile("jwt.key.pub", "utf-8"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
privateKey = crypto.createPrivateKey({ key: loadedPrivateKey, type: "sec1" });
|
privateKey = loadedPrivateKey;
|
||||||
publicKey = crypto.createPublicKey({ key: loadedPublicKey, type: "spki" });
|
publicKey = loadedPublicKey;
|
||||||
} else {
|
} else {
|
||||||
console.log("[JWT] Generating new keypair");
|
console.log("[JWT] Generating new keypair");
|
||||||
const res = crypto.generateKeyPairSync("ec", {
|
const res = crypto.generateKeyPairSync("ec", {
|
||||||
namedCurve: "secp521r1",
|
namedCurve: "secp521r1",
|
||||||
|
publicKeyEncoding: {
|
||||||
|
type: "spki",
|
||||||
|
format: "pem"
|
||||||
|
},
|
||||||
|
privateKeyEncoding: {
|
||||||
|
type: "pkcs8",
|
||||||
|
format: "pem"
|
||||||
|
}
|
||||||
});
|
});
|
||||||
privateKey = res.privateKey;
|
|
||||||
publicKey = res.publicKey;
|
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
fs.writeFile(
|
fs.writeFile("jwt.key", res.privateKey),
|
||||||
"jwt.key",
|
fs.writeFile("jwt.key.pub", res.publicKey),
|
||||||
privateKey.export({ format: "pem", type: "sec1" }),
|
|
||||||
),
|
|
||||||
fs.writeFile(
|
|
||||||
"jwt.key.pub",
|
|
||||||
publicKey.export({ format: "pem", type: "spki" }),
|
|
||||||
),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
privateKey = res.privateKey;
|
||||||
|
publicKey = res.publicKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const publicKeyForHash = typeof publicKey === 'string'
|
||||||
|
? crypto.createPublicKey(publicKey)
|
||||||
|
: publicKey;
|
||||||
|
|
||||||
const fingerprint = crypto
|
const fingerprint = crypto
|
||||||
.createHash("sha256")
|
.createHash("sha256")
|
||||||
.update(publicKey.export({ format: "pem", type: "spki" }))
|
.update(publicKeyForHash.export({ format: "pem", type: "spki" }))
|
||||||
.digest("hex");
|
.digest("hex");
|
||||||
|
|
||||||
return { privateKey, publicKey, fingerprint };
|
return { privateKey, publicKey, fingerprint };
|
||||||
|
|||||||
Reference in New Issue
Block a user