@cfworker/jwt 1.12.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  Tiny lib for decoding JWTs and verifying signatures, using native crypto APIs.
10
10
 
11
- Currently supports `alg:'RS256'` only.
11
+ Currently supports RS256, RS384, and RS512.
12
12
 
13
13
  ```js
14
14
  const jwt = request.headers.get('Authorization');
package/dist/algs.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const algToHash: Record<string, string>;
2
+ export declare const algs: string[];
package/dist/algs.js ADDED
@@ -0,0 +1,6 @@
1
+ export const algToHash = {
2
+ RS256: 'SHA-256',
3
+ RS384: 'SHA-384',
4
+ RS512: 'SHA-512'
5
+ };
6
+ export const algs = Object.keys(algToHash);
package/dist/decode.js CHANGED
@@ -1,10 +1,11 @@
1
- import { decode } from '@cfworker/base64url';
1
+ import { base64url } from 'rfc4648';
2
2
  export function decodeJwt(token) {
3
3
  const [header, payload, signature] = token.split('.');
4
+ const decoder = new TextDecoder();
4
5
  return {
5
- header: JSON.parse(decode(header)),
6
- payload: JSON.parse(decode(payload)),
7
- signature: decode(signature),
6
+ header: JSON.parse(decoder.decode(base64url.parse(header, { loose: true }))),
7
+ payload: JSON.parse(decoder.decode(base64url.parse(payload, { loose: true }))),
8
+ signature: base64url.parse(signature, { loose: true }),
8
9
  raw: { header, payload, signature }
9
10
  };
10
11
  }
package/dist/jwks.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { algToHash } from './algs.js';
1
2
  export async function getJwks(issuer) {
2
3
  const url = new URL(issuer);
3
4
  if (!url.pathname.endsWith('/')) {
@@ -12,16 +13,16 @@ export async function getJwks(issuer) {
12
13
  }
13
14
  const importedKeys = {};
14
15
  export async function importKey(iss, jwk) {
15
- const input = {
16
- kty: 'RSA',
17
- e: 'AQAB',
18
- n: jwk.n,
19
- alg: 'RS256',
20
- ext: true
21
- };
22
- const key = await crypto.subtle.importKey('jwk', input, { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, false, ['verify']);
16
+ if (jwk.kty !== 'RSA') {
17
+ throw new Error(`Unsupported jwk key type (kty) "${jwk.kty}".`);
18
+ }
19
+ const hash = jwk.alg ? algToHash[jwk.alg] : null;
20
+ if (!hash) {
21
+ throw new Error(`Unsupported jwk Algorithm (alg) "${jwk.alg}".`);
22
+ }
23
+ const key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSASSA-PKCS1-v1_5', hash }, false, ['verify']);
23
24
  importedKeys[iss] = importedKeys[iss] || {};
24
- importedKeys[iss][jwk.kid || 'default'] = key;
25
+ importedKeys[iss][jwk.kid ?? 'default'] = key;
25
26
  }
26
27
  export async function getKey(decoded) {
27
28
  let { header: { kid = 'default' }, payload: { iss } } = decoded;
package/dist/parse.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { algs, algToHash } from './algs.js';
1
2
  import { decodeJwt } from './decode.js';
2
3
  import { getKey } from './jwks.js';
3
4
  import { verifyJwtSignature } from './verify.js';
@@ -16,10 +17,10 @@ export async function parseJwt(encodedToken, issuer, audience) {
16
17
  reason: `Invalid JWT type "${decoded.header.typ}". Expected "JWT".`
17
18
  };
18
19
  }
19
- if (decoded.header.alg !== 'RS256') {
20
+ if (!algToHash[decoded.header.alg]) {
20
21
  return {
21
22
  valid: false,
22
- reason: `Invalid JWT algorithm "${decoded.header.alg}". Expected "RS256".`
23
+ reason: `Invalid JWT algorithm "${decoded.header.alg}". Supported: ${algs}.`
23
24
  };
24
25
  }
25
26
  if (typeof decoded.payload.aud === 'string' &&
package/dist/types.d.ts CHANGED
@@ -18,7 +18,7 @@ export interface RawJwt {
18
18
  export interface DecodedJwt {
19
19
  header: JwtHeader;
20
20
  payload: JwtPayload;
21
- signature: string;
21
+ signature: Uint8Array;
22
22
  raw: RawJwt;
23
23
  }
24
24
  export interface JsonWebKeyset {
package/dist/verify.js CHANGED
@@ -1,6 +1,5 @@
1
1
  export async function verifyJwtSignature(decoded, key) {
2
2
  const encoder = new TextEncoder();
3
3
  const data = encoder.encode(`${decoded.raw.header}.${decoded.raw.payload}`);
4
- const signature = new Uint8Array(Array.from(decoded.signature).map(c => c.charCodeAt(0)));
5
- return crypto.subtle.verify('RSASSA-PKCS1-v1_5', key, signature, data);
4
+ return crypto.subtle.verify('RSASSA-PKCS1-v1_5', key, decoded.signature, data);
6
5
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfworker/jwt",
3
- "version": "1.12.2",
3
+ "version": "2.0.0",
4
4
  "description": "Tiny lib for decoding JWTs and verifying signatures, using native crypto APIs",
5
5
  "keywords": [
6
6
  "jwt",
@@ -34,9 +34,9 @@
34
34
  "test": "cfworker test test/**/*.spec.ts --nocheck"
35
35
  },
36
36
  "dependencies": {
37
- "@cfworker/base64url": "^1.12.2",
38
37
  "@cfworker/worker-types": "^1.12.1",
39
- "@cloudflare/workers-types": "^2.2.2"
38
+ "@cloudflare/workers-types": "^2.2.2",
39
+ "rfc4648": "^1.5.1"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@cfworker/dev": "^1.14.0",
package/src/algs.ts ADDED
@@ -0,0 +1,7 @@
1
+ export const algToHash: Record<string, string> = {
2
+ RS256: 'SHA-256',
3
+ RS384: 'SHA-384',
4
+ RS512: 'SHA-512'
5
+ };
6
+
7
+ export const algs = Object.keys(algToHash);
package/src/decode.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { decode } from '@cfworker/base64url';
1
+ import { base64url } from 'rfc4648';
2
2
  import { DecodedJwt } from './types.js';
3
3
 
4
4
  /**
@@ -6,10 +6,15 @@ import { DecodedJwt } from './types.js';
6
6
  */
7
7
  export function decodeJwt(token: string): DecodedJwt {
8
8
  const [header, payload, signature] = token.split('.');
9
+ const decoder = new TextDecoder();
9
10
  return {
10
- header: JSON.parse(decode(header)),
11
- payload: JSON.parse(decode(payload)),
12
- signature: decode(signature),
11
+ header: JSON.parse(
12
+ decoder.decode(base64url.parse(header, { loose: true }))
13
+ ),
14
+ payload: JSON.parse(
15
+ decoder.decode(base64url.parse(payload, { loose: true }))
16
+ ),
17
+ signature: base64url.parse(signature, { loose: true }),
13
18
  raw: { header, payload, signature }
14
19
  };
15
20
  }
package/src/jwks.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { algToHash } from './algs.js';
1
2
  import { DecodedJwt, JsonWebKeyset } from './types.js';
2
3
 
3
4
  /**
@@ -26,22 +27,22 @@ const importedKeys: Record<string, Record<string, CryptoKey>> = {};
26
27
  * @param jwks The JsonWebKeyset to import.
27
28
  */
28
29
  export async function importKey(iss: string, jwk: JsonWebKey) {
29
- const input = {
30
- kty: 'RSA',
31
- e: 'AQAB',
32
- n: jwk.n,
33
- alg: 'RS256',
34
- ext: true
35
- };
30
+ if (jwk.kty !== 'RSA') {
31
+ throw new Error(`Unsupported jwk key type (kty) "${jwk.kty}".`);
32
+ }
33
+ const hash = jwk.alg ? algToHash[jwk.alg] : null;
34
+ if (!hash) {
35
+ throw new Error(`Unsupported jwk Algorithm (alg) "${jwk.alg}".`);
36
+ }
36
37
  const key = await crypto.subtle.importKey(
37
38
  'jwk',
38
- input,
39
- { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },
39
+ jwk,
40
+ { name: 'RSASSA-PKCS1-v1_5', hash },
40
41
  false,
41
42
  ['verify']
42
43
  );
43
44
  importedKeys[iss] = importedKeys[iss] || {};
44
- importedKeys[iss][jwk.kid || 'default'] = key;
45
+ importedKeys[iss][jwk.kid ?? 'default'] = key;
45
46
  }
46
47
 
47
48
  /**
package/src/parse.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { algs, algToHash } from './algs.js';
1
2
  import { decodeJwt } from './decode.js';
2
3
  import { getKey } from './jwks.js';
3
4
  import { DecodedJwt, JwtParseResult } from './types.js';
@@ -26,10 +27,10 @@ export async function parseJwt(
26
27
  reason: `Invalid JWT type "${decoded.header.typ}". Expected "JWT".`
27
28
  };
28
29
  }
29
- if (decoded.header.alg !== 'RS256') {
30
+ if (!algToHash[decoded.header.alg]) {
30
31
  return {
31
32
  valid: false,
32
- reason: `Invalid JWT algorithm "${decoded.header.alg}". Expected "RS256".`
33
+ reason: `Invalid JWT algorithm "${decoded.header.alg}". Supported: ${algs}.`
33
34
  };
34
35
  }
35
36
  if (
package/src/types.ts CHANGED
@@ -28,7 +28,7 @@ export interface RawJwt {
28
28
  export interface DecodedJwt {
29
29
  header: JwtHeader;
30
30
  payload: JwtPayload;
31
- signature: string;
31
+ signature: Uint8Array;
32
32
  raw: RawJwt;
33
33
  }
34
34
 
package/src/verify.ts CHANGED
@@ -7,9 +7,11 @@ import { DecodedJwt } from './types.js';
7
7
  export async function verifyJwtSignature(decoded: DecodedJwt, key: CryptoKey) {
8
8
  const encoder = new TextEncoder();
9
9
  const data = encoder.encode(`${decoded.raw.header}.${decoded.raw.payload}`);
10
- const signature = new Uint8Array(
11
- Array.from(decoded.signature).map(c => c.charCodeAt(0))
12
- );
13
10
 
14
- return crypto.subtle.verify('RSASSA-PKCS1-v1_5', key, signature, data);
11
+ return crypto.subtle.verify(
12
+ 'RSASSA-PKCS1-v1_5',
13
+ key,
14
+ decoded.signature,
15
+ data
16
+ );
15
17
  }