-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwk.ts
More file actions
26 lines (24 loc) · 1001 Bytes
/
jwk.ts
File metadata and controls
26 lines (24 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { exportPublicJwk } from './crypto.ts';
// the jwk exported by subtle crypto is not exactly the format we need for our public jwk
// tweak it a bit for export
export async function generateJwkSet(keyId: string, key: CryptoKey) {
if (!/^[A-Za-z0-9_.+/=-]+$/.test(keyId)) throw new Error(`Bad keyId: ${keyId}, expected a non-empty string with no whitespace or special characters`);
const jwk = await exportPublicJwk(key);
const { kty, alg, n, e } = jwk;
if (kty !== 'RSA') throw new Error(`Expected RSA public key, found ${kty}`);
if (alg !== 'RS256') throw new Error(`Expected RS256 algorithm, found ${alg}`);
if (typeof n !== 'string') throw new Error(`Expected RSA n parameter`);
if (typeof e !== 'string') throw new Error(`Expected RSA e parameter`);
return {
keys: [
{
kty,
kid: keyId,
use: 'sig',
alg,
n,
e,
}
]
}
}