@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 +1 -1
- package/dist/algs.d.ts +2 -0
- package/dist/algs.js +6 -0
- package/dist/decode.js +5 -4
- package/dist/jwks.js +10 -9
- package/dist/parse.js +3 -2
- package/dist/types.d.ts +1 -1
- package/dist/verify.js +1 -2
- package/package.json +3 -3
- package/src/algs.ts +7 -0
- package/src/decode.ts +9 -4
- package/src/jwks.ts +11 -10
- package/src/parse.ts +3 -2
- package/src/types.ts +1 -1
- package/src/verify.ts +6 -4
package/README.md
CHANGED
package/dist/algs.d.ts
ADDED
package/dist/algs.js
ADDED
package/dist/decode.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
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:
|
|
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
|
-
|
|
16
|
-
kty
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
const key = await crypto.subtle.importKey('jwk',
|
|
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
|
|
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
|
|
20
|
+
if (!algToHash[decoded.header.alg]) {
|
|
20
21
|
return {
|
|
21
22
|
valid: false,
|
|
22
|
-
reason: `Invalid JWT algorithm "${decoded.header.alg}".
|
|
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
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
|
-
|
|
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": "
|
|
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
package/src/decode.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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(
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
30
|
-
kty
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
39
|
-
{ name: 'RSASSA-PKCS1-v1_5', hash
|
|
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
|
|
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
|
|
30
|
+
if (!algToHash[decoded.header.alg]) {
|
|
30
31
|
return {
|
|
31
32
|
valid: false,
|
|
32
|
-
reason: `Invalid JWT algorithm "${decoded.header.alg}".
|
|
33
|
+
reason: `Invalid JWT algorithm "${decoded.header.alg}". Supported: ${algs}.`
|
|
33
34
|
};
|
|
34
35
|
}
|
|
35
36
|
if (
|
package/src/types.ts
CHANGED
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(
|
|
11
|
+
return crypto.subtle.verify(
|
|
12
|
+
'RSASSA-PKCS1-v1_5',
|
|
13
|
+
key,
|
|
14
|
+
decoded.signature,
|
|
15
|
+
data
|
|
16
|
+
);
|
|
15
17
|
}
|