@cfworker/jwt 5.1.1 → 5.2.1

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/dist/index.d.ts CHANGED
@@ -2,5 +2,4 @@ export { decodeJwt } from './decode.js';
2
2
  export { getJwks, getKey, importKey } from './jwks.js';
3
3
  export { parseJwt } from './parse.js';
4
4
  export * from './types.js';
5
- export { InvalidJwtReasonCode } from './types.js';
6
5
  export { verifyJwtSignature } from './verify.js';
package/dist/parse.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { algs, algToHash } from './algs.js';
2
2
  import { decodeJwt } from './decode.js';
3
3
  import { getKey } from './jwks.js';
4
+ import { InvalidJwtReasonCode } from './types.js';
4
5
  import { verifyJwtSignature } from './verify.js';
5
6
  const defaultSkewMs = 60 * 1000;
6
7
  export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, skewMs = defaultSkewMs }) {
@@ -12,7 +13,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
12
13
  return {
13
14
  valid: false,
14
15
  reason: `Unable to decode JWT.`,
15
- reasonCode: 0
16
+ reasonCode: InvalidJwtReasonCode.Other
16
17
  };
17
18
  }
18
19
  const { typ, alg } = decoded.header;
@@ -20,7 +21,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
20
21
  return {
21
22
  valid: false,
22
23
  reason: `Invalid JWT type ${JSON.stringify(typ)}. Expected "JWT".`,
23
- reasonCode: 0,
24
+ reasonCode: InvalidJwtReasonCode.Other,
24
25
  decoded
25
26
  };
26
27
  }
@@ -28,7 +29,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
28
29
  return {
29
30
  valid: false,
30
31
  reason: `Invalid JWT algorithm ${JSON.stringify(alg)}. Supported: ${algs}.`,
31
- reasonCode: 0,
32
+ reasonCode: InvalidJwtReasonCode.Other,
32
33
  decoded
33
34
  };
34
35
  }
@@ -37,7 +38,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
37
38
  return {
38
39
  valid: false,
39
40
  reason: `Subject claim (sub) is required and must be a string. Received ${JSON.stringify(sub)}.`,
40
- reasonCode: 0,
41
+ reasonCode: InvalidJwtReasonCode.Other,
41
42
  decoded
42
43
  };
43
44
  }
@@ -46,7 +47,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
46
47
  return {
47
48
  valid: false,
48
49
  reason: `Invalid JWT audience claim (aud) ${JSON.stringify(aud)}. Expected "${audience}".`,
49
- reasonCode: 0,
50
+ reasonCode: InvalidJwtReasonCode.Other,
50
51
  decoded
51
52
  };
52
53
  }
@@ -58,7 +59,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
58
59
  return {
59
60
  valid: false,
60
61
  reason: `Invalid JWT audience claim array (aud) ${JSON.stringify(aud)}. Does not include "${audience}".`,
61
- reasonCode: 0,
62
+ reasonCode: InvalidJwtReasonCode.Other,
62
63
  decoded
63
64
  };
64
65
  }
@@ -67,7 +68,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
67
68
  return {
68
69
  valid: false,
69
70
  reason: `Invalid JWT audience claim (aud) ${JSON.stringify(aud)}. Expected a string or a non-empty array of strings.`,
70
- reasonCode: 0,
71
+ reasonCode: InvalidJwtReasonCode.Other,
71
72
  decoded
72
73
  };
73
74
  }
@@ -75,7 +76,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
75
76
  return {
76
77
  valid: false,
77
78
  reason: `Invalid JWT issuer claim (iss) ${JSON.stringify(decoded.payload.iss)}. Expected ${JSON.stringify(issuer)}.`,
78
- reasonCode: 0,
79
+ reasonCode: InvalidJwtReasonCode.Other,
79
80
  decoded
80
81
  };
81
82
  }
@@ -83,7 +84,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
83
84
  return {
84
85
  valid: false,
85
86
  reason: `Invalid JWT expiry date claim (exp) ${JSON.stringify(exp)}. Expected number.`,
86
- reasonCode: 0,
87
+ reasonCode: InvalidJwtReasonCode.Other,
87
88
  decoded
88
89
  };
89
90
  }
@@ -95,7 +96,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
95
96
  return {
96
97
  valid: false,
97
98
  reason: `JWT is expired. Expiry date: ${expiryDate}; Current date: ${currentDate};`,
98
- reasonCode: 1,
99
+ reasonCode: InvalidJwtReasonCode.Expired,
99
100
  decoded
100
101
  };
101
102
  }
@@ -104,7 +105,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
104
105
  return {
105
106
  valid: false,
106
107
  reason: `Invalid JWT not before date claim (nbf) ${JSON.stringify(nbf)}. Expected number.`,
107
- reasonCode: 0,
108
+ reasonCode: InvalidJwtReasonCode.Other,
108
109
  decoded
109
110
  };
110
111
  }
@@ -115,7 +116,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
115
116
  return {
116
117
  valid: false,
117
118
  reason: `JWT cannot be used prior to not before date claim (nbf). Not before date: ${notBeforeDate}; Current date: ${currentDate};`,
118
- reasonCode: 0,
119
+ reasonCode: InvalidJwtReasonCode.Other,
119
120
  decoded
120
121
  };
121
122
  }
@@ -125,7 +126,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
125
126
  return {
126
127
  valid: false,
127
128
  reason: `Invalid JWT issued at date claim (iat) ${JSON.stringify(iat)}. Expected number.`,
128
- reasonCode: 0,
129
+ reasonCode: InvalidJwtReasonCode.Other,
129
130
  decoded
130
131
  };
131
132
  }
@@ -136,7 +137,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
136
137
  return {
137
138
  valid: false,
138
139
  reason: `JWT issued at date claim (iat) is in the future. Issued at date: ${issuedAtDate}; Current date: ${currentDate};`,
139
- reasonCode: 0,
140
+ reasonCode: InvalidJwtReasonCode.Other,
140
141
  decoded
141
142
  };
142
143
  }
@@ -149,7 +150,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
149
150
  return {
150
151
  valid: false,
151
152
  reason: `Error retrieving public key to verify JWT signature: ${e instanceof Error ? e.message : e}`,
152
- reasonCode: 0,
153
+ reasonCode: InvalidJwtReasonCode.Other,
153
154
  decoded
154
155
  };
155
156
  }
@@ -157,7 +158,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
157
158
  return {
158
159
  valid: false,
159
160
  reason: `Unable to resolve public key to verify JWT signature.`,
160
- reasonCode: 0,
161
+ reasonCode: InvalidJwtReasonCode.Other,
161
162
  decoded
162
163
  };
163
164
  }
@@ -169,7 +170,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
169
170
  return {
170
171
  valid: false,
171
172
  reason: `Error verifying JWT signature.`,
172
- reasonCode: 0,
173
+ reasonCode: InvalidJwtReasonCode.Other,
173
174
  decoded
174
175
  };
175
176
  }
@@ -177,7 +178,7 @@ export async function parseJwt({ jwt, issuer, audience, resolveKey = getKey, ske
177
178
  return {
178
179
  valid: false,
179
180
  reason: `JWT signature is invalid.`,
180
- reasonCode: 0,
181
+ reasonCode: InvalidJwtReasonCode.Other,
181
182
  decoded
182
183
  };
183
184
  }
package/dist/types.d.ts CHANGED
@@ -38,7 +38,7 @@ export interface InvalidJwtParseResult {
38
38
  reasonCode: InvalidJwtReasonCode;
39
39
  decoded?: DecodedJwt;
40
40
  }
41
- export declare const enum InvalidJwtReasonCode {
41
+ export declare enum InvalidJwtReasonCode {
42
42
  Other = 0,
43
43
  Expired = 1
44
44
  }
package/dist/types.js CHANGED
@@ -1 +1,5 @@
1
- export {};
1
+ export var InvalidJwtReasonCode;
2
+ (function (InvalidJwtReasonCode) {
3
+ InvalidJwtReasonCode[InvalidJwtReasonCode["Other"] = 0] = "Other";
4
+ InvalidJwtReasonCode[InvalidJwtReasonCode["Expired"] = 1] = "Expired";
5
+ })(InvalidJwtReasonCode || (InvalidJwtReasonCode = {}));
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@cfworker/jwt",
3
- "version": "5.1.1",
3
+ "type": "module",
4
+ "version": "5.2.1",
4
5
  "description": "Tiny lib for decoding JWTs and verifying signatures, using native crypto APIs",
5
6
  "keywords": [
6
7
  "jwt",
@@ -17,7 +18,6 @@
17
18
  "files": [
18
19
  "dist/**/*",
19
20
  "src/**/*",
20
- "tsconfig.json",
21
21
  "README.md",
22
22
  "package.json"
23
23
  ],
@@ -44,6 +44,6 @@
44
44
  "esbuild": "^0.23.1",
45
45
  "mocha": "^10.7.3",
46
46
  "typescript": "^5.5.4",
47
- "wrangler": "^3.72.0"
47
+ "wrangler": "^3.72.1"
48
48
  }
49
49
  }
package/src/algs.ts CHANGED
@@ -4,4 +4,4 @@ export const algToHash: Record<string, string> = {
4
4
  RS512: 'SHA-512'
5
5
  };
6
6
 
7
- export const algs = Object.keys(algToHash);
7
+ export const algs: string[] = Object.keys(algToHash);
package/src/index.ts CHANGED
@@ -2,5 +2,4 @@ export { decodeJwt } from './decode.js';
2
2
  export { getJwks, getKey, importKey } from './jwks.js';
3
3
  export { parseJwt } from './parse.js';
4
4
  export * from './types.js';
5
- export { InvalidJwtReasonCode } from './types.js';
6
5
  export { verifyJwtSignature } from './verify.js';
package/src/jwks.ts CHANGED
@@ -35,7 +35,7 @@ const importedKeys: Record<string, Record<string, CryptoKey>> = {};
35
35
  * @param iss The issuer. Serves as the first-level cache key.
36
36
  * @param jwks The JsonWebKeyset to import.
37
37
  */
38
- export async function importKey(iss: string, jwk: JsonWebKey) {
38
+ export async function importKey(iss: string, jwk: JsonWebKey): Promise<void> {
39
39
  if (jwk.kty !== 'RSA') {
40
40
  throw new Error(
41
41
  `Unsupported jwk key type (kty) "${
package/src/types.ts CHANGED
@@ -52,7 +52,7 @@ export interface InvalidJwtParseResult {
52
52
  decoded?: DecodedJwt;
53
53
  }
54
54
 
55
- export const enum InvalidJwtReasonCode {
55
+ export enum InvalidJwtReasonCode {
56
56
  Other = 0,
57
57
  Expired = 1
58
58
  }
package/src/verify.ts CHANGED
@@ -4,7 +4,10 @@ import { DecodedJwt } from './types.js';
4
4
  * Verify the JWT's signature.
5
5
  * @param {DecodedJwt} decoded
6
6
  */
7
- export async function verifyJwtSignature(decoded: DecodedJwt, key: CryptoKey) {
7
+ export async function verifyJwtSignature(
8
+ decoded: DecodedJwt,
9
+ key: CryptoKey
10
+ ): Promise<boolean> {
8
11
  const encoder = new TextEncoder();
9
12
  const data = encoder.encode(`${decoded.raw.header}.${decoded.raw.payload}`);
10
13
 
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "extends": "../../tsconfig-base",
3
- "compilerOptions": {
4
- "target": "ESNext",
5
- "lib": ["ESNext", "WebWorker", "Webworker.Iterable"],
6
- "outDir": "dist",
7
- "rootDir": "./src"
8
- },
9
- "include": ["src"],
10
- "references": []
11
- }