Skip to content

Commit

Permalink
Fix JWK import/export for api
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Oct 13, 2023
1 parent 735baeb commit 615b592
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions source/main/services/browserAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ export async function encryptPayload(
}

async function exportECDHKey(key: CryptoKey): Promise<string> {
if (key.type === "private") {
const exported = await webcrypto.subtle.exportKey("jwk", key);
return Buffer.from(exported.d).toString("base64");
}
const exported = await webcrypto.subtle.exportKey("raw", key);
return Buffer.from(exported).toString("base64");
const exported = await webcrypto.subtle.exportKey("jwk", key);
return JSON.stringify(exported);
}

export async function generateBrowserKeys(): Promise<void> {
Expand Down Expand Up @@ -85,15 +81,17 @@ export async function getBrowserPublicKeyString(): Promise<string> {
}

async function importECDHKey(key: string): Promise<CryptoKey> {
const buffer = Buffer.from(key, "base64").buffer;
const jwk = JSON.parse(key) as JsonWebKey;
const usages: Array<KeyUsage> =
jwk.key_ops && jwk.key_ops.includes("deriveKey") ? ["deriveKey"] : [];
return webcrypto.subtle.importKey(
"raw",
buffer,
"jwk",
jwk,
{
name: API_KEY_ALGO,
namedCurve: API_KEY_CURVE
},
false,
["deriveKey"]
true,
usages
);
}

0 comments on commit 615b592

Please sign in to comment.