@cfworker/jwt 2.0.0 → 4.0.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/discovery.d.ts +2 -0
- package/dist/discovery.js +12 -0
- package/dist/jwks.js +25 -10
- package/dist/parse.d.ts +2 -2
- package/dist/parse.js +84 -20
- package/dist/types.d.ts +28 -0
- package/package.json +1 -1
- package/src/discovery.ts +21 -0
- package/src/jwks.ts +38 -10
- package/src/parse.ts +112 -25
- package/src/types.ts +30 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export async function getIssuerMetadata(issuer) {
|
|
2
|
+
const url = new URL(issuer);
|
|
3
|
+
if (!url.pathname.endsWith('/')) {
|
|
4
|
+
url.pathname += '/';
|
|
5
|
+
}
|
|
6
|
+
url.pathname += '.well-known/openid-configuration';
|
|
7
|
+
const response = await fetch(url.href);
|
|
8
|
+
if (!response.ok) {
|
|
9
|
+
throw new Error(`Error loading OpenID discovery document at ${url.href}. ${response.status} ${response.statusText}`);
|
|
10
|
+
}
|
|
11
|
+
return response.json();
|
|
12
|
+
}
|
package/dist/jwks.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { algToHash } from './algs.js';
|
|
2
|
+
import { getIssuerMetadata } from './discovery.js';
|
|
2
3
|
export async function getJwks(issuer) {
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const issuerMetadata = await getIssuerMetadata(issuer);
|
|
5
|
+
let url;
|
|
6
|
+
if (issuerMetadata.jwks_uri) {
|
|
7
|
+
url = new URL(issuerMetadata.jwks_uri);
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
url = new URL(issuer);
|
|
11
|
+
if (!url.pathname.endsWith('/')) {
|
|
12
|
+
url.pathname += '/';
|
|
13
|
+
}
|
|
14
|
+
url.pathname += '.well-known/jwks.json';
|
|
6
15
|
}
|
|
7
|
-
url.pathname += '.well-known/jwks.json';
|
|
8
16
|
const response = await fetch(url.href);
|
|
9
17
|
if (!response.ok) {
|
|
10
18
|
throw new Error(`Error loading jwks at ${url.href}. ${response.status} ${response.statusText}`);
|
|
@@ -14,21 +22,28 @@ export async function getJwks(issuer) {
|
|
|
14
22
|
const importedKeys = {};
|
|
15
23
|
export async function importKey(iss, jwk) {
|
|
16
24
|
if (jwk.kty !== 'RSA') {
|
|
17
|
-
throw new Error(`Unsupported jwk key type (kty) "${jwk.kty}"
|
|
25
|
+
throw new Error(`Unsupported jwk key type (kty) "${jwk.kty}": Full JWK was ${JSON.stringify(jwk)}`);
|
|
18
26
|
}
|
|
19
|
-
const hash = jwk.alg ? algToHash[jwk.alg] :
|
|
27
|
+
const hash = jwk.alg ? algToHash[jwk.alg] : 'SHA-256';
|
|
20
28
|
if (!hash) {
|
|
21
|
-
throw new Error(`Unsupported jwk Algorithm (alg) "${jwk.alg}"
|
|
29
|
+
throw new Error(`Unsupported jwk Algorithm (alg) "${jwk.alg}": Full JWK was ${JSON.stringify(jwk)}`);
|
|
22
30
|
}
|
|
23
31
|
const key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSASSA-PKCS1-v1_5', hash }, false, ['verify']);
|
|
24
32
|
importedKeys[iss] = importedKeys[iss] || {};
|
|
25
33
|
importedKeys[iss][jwk.kid ?? 'default'] = key;
|
|
26
34
|
}
|
|
35
|
+
const jwksImports = {};
|
|
27
36
|
export async function getKey(decoded) {
|
|
28
|
-
|
|
37
|
+
const { header: { kid = 'default' }, payload: { iss } } = decoded;
|
|
29
38
|
if (!importedKeys[iss]) {
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
if (!jwksImports[iss]) {
|
|
40
|
+
jwksImports[iss] = getJwks(iss)
|
|
41
|
+
.then(jwks => Promise.all(jwks.keys.map(jwk => importKey(iss, jwk))))
|
|
42
|
+
.finally(() => {
|
|
43
|
+
delete jwksImports[iss];
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
await jwksImports[iss];
|
|
32
47
|
}
|
|
33
48
|
const key = importedKeys[iss][kid];
|
|
34
49
|
if (!key) {
|
package/dist/parse.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { JwtParseResult } from './types.js';
|
|
2
|
-
export declare function parseJwt(encodedToken: string, issuer: string, audience: string): Promise<JwtParseResult>;
|
|
1
|
+
import { DecodedJwt, JwtParseResult } from './types.js';
|
|
2
|
+
export declare function parseJwt(encodedToken: string, issuer: string, audience: string, resolveKey?: (decoded: DecodedJwt) => Promise<CryptoKey | null>): Promise<JwtParseResult>;
|
package/dist/parse.js
CHANGED
|
@@ -2,7 +2,8 @@ import { algs, algToHash } from './algs.js';
|
|
|
2
2
|
import { decodeJwt } from './decode.js';
|
|
3
3
|
import { getKey } from './jwks.js';
|
|
4
4
|
import { verifyJwtSignature } from './verify.js';
|
|
5
|
-
|
|
5
|
+
const skewMs = 30 * 1000;
|
|
6
|
+
export async function parseJwt(encodedToken, issuer, audience, resolveKey = getKey) {
|
|
6
7
|
let decoded;
|
|
7
8
|
try {
|
|
8
9
|
decoded = decodeJwt(encodedToken);
|
|
@@ -10,57 +11,120 @@ export async function parseJwt(encodedToken, issuer, audience) {
|
|
|
10
11
|
catch {
|
|
11
12
|
return { valid: false, reason: `Unable to decode JWT.` };
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
const { typ, alg } = decoded.header;
|
|
15
|
+
if (typeof typ !== 'undefined' && typ !== 'JWT') {
|
|
15
16
|
return {
|
|
16
17
|
valid: false,
|
|
17
|
-
reason: `Invalid JWT type
|
|
18
|
+
reason: `Invalid JWT type ${JSON.stringify(typ)}. Expected "JWT".`
|
|
18
19
|
};
|
|
19
20
|
}
|
|
20
|
-
if (!algToHash[
|
|
21
|
+
if (!algToHash[alg]) {
|
|
21
22
|
return {
|
|
22
23
|
valid: false,
|
|
23
|
-
reason: `Invalid JWT algorithm
|
|
24
|
+
reason: `Invalid JWT algorithm ${JSON.stringify(alg)}. Supported: ${algs}.`
|
|
24
25
|
};
|
|
25
26
|
}
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
const { sub, aud, iss, iat, exp, nbf } = decoded.payload;
|
|
28
|
+
if (typeof sub !== 'string') {
|
|
28
29
|
return {
|
|
29
30
|
valid: false,
|
|
30
|
-
reason: `
|
|
31
|
+
reason: `Subject claim (sub) is required and must be a string. Received ${JSON.stringify(sub)}.`
|
|
31
32
|
};
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
if (typeof aud === 'string') {
|
|
35
|
+
if (aud !== audience) {
|
|
36
|
+
return {
|
|
37
|
+
valid: false,
|
|
38
|
+
reason: `Invalid JWT audience claim (aud) ${JSON.stringify(aud)}. Expected "${audience}".`
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else if (Array.isArray(aud) &&
|
|
43
|
+
aud.length > 0 &&
|
|
44
|
+
aud.every(a => typeof a === 'string')) {
|
|
45
|
+
if (!aud.includes(audience)) {
|
|
46
|
+
return {
|
|
47
|
+
valid: false,
|
|
48
|
+
reason: `Invalid JWT audience claim array (aud) ${JSON.stringify(aud)}. Does not include "${audience}".`
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
35
53
|
return {
|
|
36
54
|
valid: false,
|
|
37
|
-
reason: `Invalid JWT audience
|
|
55
|
+
reason: `Invalid JWT audience claim (aud) ${JSON.stringify(aud)}. Expected a string or a non-empty array of strings.`
|
|
38
56
|
};
|
|
39
57
|
}
|
|
40
|
-
if (
|
|
58
|
+
if (iss !== issuer) {
|
|
41
59
|
return {
|
|
42
60
|
valid: false,
|
|
43
|
-
reason: `Invalid JWT issuer
|
|
61
|
+
reason: `Invalid JWT issuer claim (iss) ${JSON.stringify(decoded.payload.iss)}. Expected "${issuer}".`
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (typeof exp !== 'number') {
|
|
65
|
+
return {
|
|
66
|
+
valid: false,
|
|
67
|
+
reason: `Invalid JWT expiry date claim (exp) ${JSON.stringify(exp)}. Expected number.`
|
|
44
68
|
};
|
|
45
69
|
}
|
|
46
|
-
const expiryDate = new Date(0);
|
|
47
|
-
expiryDate.setUTCSeconds(decoded.payload.exp);
|
|
48
70
|
const currentDate = new Date(Date.now());
|
|
49
|
-
const
|
|
71
|
+
const expiryDate = new Date(0);
|
|
72
|
+
expiryDate.setUTCSeconds(exp);
|
|
73
|
+
const expired = expiryDate.getTime() <= currentDate.getTime() - skewMs;
|
|
50
74
|
if (expired) {
|
|
51
75
|
return {
|
|
52
76
|
valid: false,
|
|
53
77
|
reason: `JWT is expired. Expiry date: ${expiryDate}; Current date: ${currentDate};`
|
|
54
78
|
};
|
|
55
79
|
}
|
|
80
|
+
if (nbf !== undefined) {
|
|
81
|
+
if (typeof nbf !== 'number') {
|
|
82
|
+
return {
|
|
83
|
+
valid: false,
|
|
84
|
+
reason: `Invalid JWT not before date claim (nbf) ${JSON.stringify(nbf)}. Expected number.`
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const notBeforeDate = new Date(0);
|
|
88
|
+
notBeforeDate.setUTCSeconds(nbf);
|
|
89
|
+
const early = notBeforeDate.getTime() > currentDate.getTime() + skewMs;
|
|
90
|
+
if (early) {
|
|
91
|
+
return {
|
|
92
|
+
valid: false,
|
|
93
|
+
reason: `JWT cannot be used prior to not before date claim (nbf). Not before date: ${notBeforeDate}; Current date: ${currentDate};`
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (iat !== undefined) {
|
|
98
|
+
if (typeof iat !== 'number') {
|
|
99
|
+
return {
|
|
100
|
+
valid: false,
|
|
101
|
+
reason: `Invalid JWT issued at date claim (iat) ${JSON.stringify(iat)}. Expected number.`
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const issuedAtDate = new Date(0);
|
|
105
|
+
issuedAtDate.setUTCSeconds(iat);
|
|
106
|
+
const postIssued = issuedAtDate.getTime() > currentDate.getTime() + skewMs;
|
|
107
|
+
if (postIssued) {
|
|
108
|
+
return {
|
|
109
|
+
valid: false,
|
|
110
|
+
reason: `JWT issued at date claim (iat) is in the future. Issued at date: ${issuedAtDate}; Current date: ${currentDate};`
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
56
114
|
let key;
|
|
57
115
|
try {
|
|
58
|
-
key = await
|
|
116
|
+
key = await resolveKey(decoded);
|
|
59
117
|
}
|
|
60
|
-
catch {
|
|
118
|
+
catch (e) {
|
|
119
|
+
return {
|
|
120
|
+
valid: false,
|
|
121
|
+
reason: `Error retrieving public key to verify JWT signature: ${e instanceof Error ? e.message : e}`
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (!key) {
|
|
61
125
|
return {
|
|
62
126
|
valid: false,
|
|
63
|
-
reason: `
|
|
127
|
+
reason: `Unable to resolve public key to verify JWT signature.`
|
|
64
128
|
};
|
|
65
129
|
}
|
|
66
130
|
let signatureValid;
|
package/dist/types.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export interface JwtPayload {
|
|
|
4
4
|
aud: string | Array<string>;
|
|
5
5
|
iat: number;
|
|
6
6
|
exp: number;
|
|
7
|
+
nbf?: number;
|
|
8
|
+
jti?: string;
|
|
7
9
|
}
|
|
8
10
|
export interface JwtHeader {
|
|
9
11
|
typ?: string;
|
|
@@ -32,4 +34,30 @@ export interface InvalidJwtParseResult {
|
|
|
32
34
|
valid: false;
|
|
33
35
|
reason: string;
|
|
34
36
|
}
|
|
37
|
+
export interface IssuerMetadata {
|
|
38
|
+
issuer: string;
|
|
39
|
+
authorization_endpoint?: string;
|
|
40
|
+
token_endpoint?: string;
|
|
41
|
+
jwks_uri?: string;
|
|
42
|
+
userinfo_endpoint?: string;
|
|
43
|
+
revocation_endpoint?: string;
|
|
44
|
+
end_session_endpoint?: string;
|
|
45
|
+
registration_endpoint?: string;
|
|
46
|
+
token_endpoint_auth_methods_supported?: string[];
|
|
47
|
+
token_endpoint_auth_signing_alg_values_supported?: string[];
|
|
48
|
+
introspection_endpoint_auth_methods_supported?: string[];
|
|
49
|
+
introspection_endpoint_auth_signing_alg_values_supported?: string[];
|
|
50
|
+
revocation_endpoint_auth_methods_supported?: string[];
|
|
51
|
+
revocation_endpoint_auth_signing_alg_values_supported?: string[];
|
|
52
|
+
request_object_signing_alg_values_supported?: string[];
|
|
53
|
+
mtls_endpoint_aliases?: MtlsEndpointAliases;
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
export interface MtlsEndpointAliases {
|
|
57
|
+
token_endpoint?: string;
|
|
58
|
+
userinfo_endpoint?: string;
|
|
59
|
+
revocation_endpoint?: string;
|
|
60
|
+
introspection_endpoint?: string;
|
|
61
|
+
device_authorization_endpoint?: string;
|
|
62
|
+
}
|
|
35
63
|
export declare type JwtParseResult = ValidJwtParseResult | InvalidJwtParseResult;
|
package/package.json
CHANGED
package/src/discovery.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { IssuerMetadata } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fetch an oidc discovery document
|
|
5
|
+
*/
|
|
6
|
+
export async function getIssuerMetadata(
|
|
7
|
+
issuer: string
|
|
8
|
+
): Promise<IssuerMetadata> {
|
|
9
|
+
const url = new URL(issuer);
|
|
10
|
+
if (!url.pathname.endsWith('/')) {
|
|
11
|
+
url.pathname += '/';
|
|
12
|
+
}
|
|
13
|
+
url.pathname += '.well-known/openid-configuration';
|
|
14
|
+
const response = await fetch(url.href);
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
`Error loading OpenID discovery document at ${url.href}. ${response.status} ${response.statusText}`
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
return response.json();
|
|
21
|
+
}
|
package/src/jwks.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
import { algToHash } from './algs.js';
|
|
2
|
+
import { getIssuerMetadata } from './discovery.js';
|
|
2
3
|
import { DecodedJwt, JsonWebKeyset } from './types.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Fetch a json web keyset.
|
|
6
7
|
*/
|
|
7
8
|
export async function getJwks(issuer: string): Promise<JsonWebKeyset> {
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
const issuerMetadata = await getIssuerMetadata(issuer);
|
|
10
|
+
|
|
11
|
+
let url;
|
|
12
|
+
if (issuerMetadata.jwks_uri) {
|
|
13
|
+
url = new URL(issuerMetadata.jwks_uri);
|
|
14
|
+
} else {
|
|
15
|
+
url = new URL(issuer);
|
|
16
|
+
if (!url.pathname.endsWith('/')) {
|
|
17
|
+
url.pathname += '/';
|
|
18
|
+
}
|
|
19
|
+
url.pathname += '.well-known/jwks.json';
|
|
11
20
|
}
|
|
12
|
-
|
|
21
|
+
|
|
13
22
|
const response = await fetch(url.href);
|
|
14
23
|
if (!response.ok) {
|
|
15
24
|
throw new Error(
|
|
@@ -28,11 +37,22 @@ const importedKeys: Record<string, Record<string, CryptoKey>> = {};
|
|
|
28
37
|
*/
|
|
29
38
|
export async function importKey(iss: string, jwk: JsonWebKey) {
|
|
30
39
|
if (jwk.kty !== 'RSA') {
|
|
31
|
-
throw new Error(
|
|
40
|
+
throw new Error(
|
|
41
|
+
`Unsupported jwk key type (kty) "${
|
|
42
|
+
jwk.kty
|
|
43
|
+
}": Full JWK was ${JSON.stringify(jwk)}`
|
|
44
|
+
);
|
|
32
45
|
}
|
|
33
|
-
|
|
46
|
+
// alg is not mandatory in a JWK but is available in the JWT, for now we default to
|
|
47
|
+
// SHA-256 (RS256) because this is the most common but evaluating the key length
|
|
48
|
+
// of the JWKS is probably an ideal way of identifying what the alg is.
|
|
49
|
+
const hash = jwk.alg ? algToHash[jwk.alg] : 'SHA-256';
|
|
34
50
|
if (!hash) {
|
|
35
|
-
throw new Error(
|
|
51
|
+
throw new Error(
|
|
52
|
+
`Unsupported jwk Algorithm (alg) "${
|
|
53
|
+
jwk.alg
|
|
54
|
+
}": Full JWK was ${JSON.stringify(jwk)}`
|
|
55
|
+
);
|
|
36
56
|
}
|
|
37
57
|
const key = await crypto.subtle.importKey(
|
|
38
58
|
'jwk',
|
|
@@ -45,18 +65,26 @@ export async function importKey(iss: string, jwk: JsonWebKey) {
|
|
|
45
65
|
importedKeys[iss][jwk.kid ?? 'default'] = key;
|
|
46
66
|
}
|
|
47
67
|
|
|
68
|
+
const jwksImports: Record<string, Promise<unknown>> = {};
|
|
69
|
+
|
|
48
70
|
/**
|
|
49
71
|
* Get the CryptoKey associated with the JWT's issuer.
|
|
50
72
|
*/
|
|
51
73
|
export async function getKey(decoded: DecodedJwt): Promise<CryptoKey> {
|
|
52
|
-
|
|
74
|
+
const {
|
|
53
75
|
header: { kid = 'default' },
|
|
54
76
|
payload: { iss }
|
|
55
77
|
} = decoded;
|
|
56
78
|
|
|
57
79
|
if (!importedKeys[iss]) {
|
|
58
|
-
|
|
59
|
-
|
|
80
|
+
if (!jwksImports[iss]) {
|
|
81
|
+
jwksImports[iss] = getJwks(iss)
|
|
82
|
+
.then(jwks => Promise.all(jwks.keys.map(jwk => importKey(iss, jwk))))
|
|
83
|
+
.finally(() => {
|
|
84
|
+
delete jwksImports[iss];
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
await jwksImports[iss];
|
|
60
88
|
}
|
|
61
89
|
|
|
62
90
|
const key = importedKeys[iss][kid];
|
package/src/parse.ts
CHANGED
|
@@ -4,13 +4,16 @@ import { getKey } from './jwks.js';
|
|
|
4
4
|
import { DecodedJwt, JwtParseResult } from './types.js';
|
|
5
5
|
import { verifyJwtSignature } from './verify.js';
|
|
6
6
|
|
|
7
|
+
const skewMs = 30 * 1000;
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* Parse a JWT.
|
|
9
11
|
*/
|
|
10
12
|
export async function parseJwt(
|
|
11
13
|
encodedToken: string,
|
|
12
14
|
issuer: string,
|
|
13
|
-
audience: string
|
|
15
|
+
audience: string,
|
|
16
|
+
resolveKey: (decoded: DecodedJwt) => Promise<CryptoKey | null> = getKey
|
|
14
17
|
): Promise<JwtParseResult> {
|
|
15
18
|
let decoded: DecodedJwt;
|
|
16
19
|
try {
|
|
@@ -18,62 +21,146 @@ export async function parseJwt(
|
|
|
18
21
|
} catch {
|
|
19
22
|
return { valid: false, reason: `Unable to decode JWT.` };
|
|
20
23
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
decoded.header.typ !== 'JWT'
|
|
24
|
-
) {
|
|
24
|
+
const { typ, alg } = decoded.header;
|
|
25
|
+
if (typeof typ !== 'undefined' && typ !== 'JWT') {
|
|
25
26
|
return {
|
|
26
27
|
valid: false,
|
|
27
|
-
reason: `Invalid JWT type
|
|
28
|
+
reason: `Invalid JWT type ${JSON.stringify(typ)}. Expected "JWT".`
|
|
28
29
|
};
|
|
29
30
|
}
|
|
30
|
-
if (!algToHash[
|
|
31
|
+
if (!algToHash[alg]) {
|
|
31
32
|
return {
|
|
32
33
|
valid: false,
|
|
33
|
-
reason: `Invalid JWT algorithm
|
|
34
|
+
reason: `Invalid JWT algorithm ${JSON.stringify(
|
|
35
|
+
alg
|
|
36
|
+
)}. Supported: ${algs}.`
|
|
34
37
|
};
|
|
35
38
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
) {
|
|
39
|
+
|
|
40
|
+
const { sub, aud, iss, iat, exp, nbf } = decoded.payload;
|
|
41
|
+
if (typeof sub !== 'string') {
|
|
40
42
|
return {
|
|
41
43
|
valid: false,
|
|
42
|
-
reason: `
|
|
44
|
+
reason: `Subject claim (sub) is required and must be a string. Received ${JSON.stringify(
|
|
45
|
+
sub
|
|
46
|
+
)}.`
|
|
43
47
|
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (typeof aud === 'string') {
|
|
51
|
+
if (aud !== audience) {
|
|
52
|
+
return {
|
|
53
|
+
valid: false,
|
|
54
|
+
reason: `Invalid JWT audience claim (aud) ${JSON.stringify(
|
|
55
|
+
aud
|
|
56
|
+
)}. Expected "${audience}".`
|
|
57
|
+
};
|
|
58
|
+
}
|
|
44
59
|
} else if (
|
|
45
|
-
Array.isArray(
|
|
46
|
-
|
|
60
|
+
Array.isArray(aud) &&
|
|
61
|
+
aud.length > 0 &&
|
|
62
|
+
aud.every(a => typeof a === 'string')
|
|
47
63
|
) {
|
|
64
|
+
if (!aud.includes(audience)) {
|
|
65
|
+
return {
|
|
66
|
+
valid: false,
|
|
67
|
+
reason: `Invalid JWT audience claim array (aud) ${JSON.stringify(
|
|
68
|
+
aud
|
|
69
|
+
)}. Does not include "${audience}".`
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
48
73
|
return {
|
|
49
74
|
valid: false,
|
|
50
|
-
reason: `Invalid JWT audience
|
|
75
|
+
reason: `Invalid JWT audience claim (aud) ${JSON.stringify(
|
|
76
|
+
aud
|
|
77
|
+
)}. Expected a string or a non-empty array of strings.`
|
|
51
78
|
};
|
|
52
79
|
}
|
|
53
80
|
|
|
54
|
-
if (
|
|
81
|
+
if (iss !== issuer) {
|
|
55
82
|
return {
|
|
56
83
|
valid: false,
|
|
57
|
-
reason: `Invalid JWT issuer
|
|
84
|
+
reason: `Invalid JWT issuer claim (iss) ${JSON.stringify(
|
|
85
|
+
decoded.payload.iss
|
|
86
|
+
)}. Expected "${issuer}".`
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (typeof exp !== 'number') {
|
|
91
|
+
return {
|
|
92
|
+
valid: false,
|
|
93
|
+
reason: `Invalid JWT expiry date claim (exp) ${JSON.stringify(
|
|
94
|
+
exp
|
|
95
|
+
)}. Expected number.`
|
|
58
96
|
};
|
|
59
97
|
}
|
|
60
|
-
const expiryDate = new Date(0);
|
|
61
|
-
expiryDate.setUTCSeconds(decoded.payload.exp);
|
|
62
98
|
const currentDate = new Date(Date.now());
|
|
63
|
-
const
|
|
99
|
+
const expiryDate = new Date(0);
|
|
100
|
+
expiryDate.setUTCSeconds(exp);
|
|
101
|
+
const expired = expiryDate.getTime() <= currentDate.getTime() - skewMs;
|
|
64
102
|
if (expired) {
|
|
65
103
|
return {
|
|
66
104
|
valid: false,
|
|
67
105
|
reason: `JWT is expired. Expiry date: ${expiryDate}; Current date: ${currentDate};`
|
|
68
106
|
};
|
|
69
107
|
}
|
|
70
|
-
|
|
108
|
+
|
|
109
|
+
if (nbf !== undefined) {
|
|
110
|
+
if (typeof nbf !== 'number') {
|
|
111
|
+
return {
|
|
112
|
+
valid: false,
|
|
113
|
+
reason: `Invalid JWT not before date claim (nbf) ${JSON.stringify(
|
|
114
|
+
nbf
|
|
115
|
+
)}. Expected number.`
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const notBeforeDate = new Date(0);
|
|
119
|
+
notBeforeDate.setUTCSeconds(nbf);
|
|
120
|
+
const early = notBeforeDate.getTime() > currentDate.getTime() + skewMs;
|
|
121
|
+
if (early) {
|
|
122
|
+
return {
|
|
123
|
+
valid: false,
|
|
124
|
+
reason: `JWT cannot be used prior to not before date claim (nbf). Not before date: ${notBeforeDate}; Current date: ${currentDate};`
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (iat !== undefined) {
|
|
130
|
+
if (typeof iat !== 'number') {
|
|
131
|
+
return {
|
|
132
|
+
valid: false,
|
|
133
|
+
reason: `Invalid JWT issued at date claim (iat) ${JSON.stringify(
|
|
134
|
+
iat
|
|
135
|
+
)}. Expected number.`
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const issuedAtDate = new Date(0);
|
|
139
|
+
issuedAtDate.setUTCSeconds(iat);
|
|
140
|
+
const postIssued = issuedAtDate.getTime() > currentDate.getTime() + skewMs;
|
|
141
|
+
if (postIssued) {
|
|
142
|
+
return {
|
|
143
|
+
valid: false,
|
|
144
|
+
reason: `JWT issued at date claim (iat) is in the future. Issued at date: ${issuedAtDate}; Current date: ${currentDate};`
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let key: CryptoKey | null;
|
|
71
150
|
try {
|
|
72
|
-
key = await
|
|
73
|
-
} catch {
|
|
151
|
+
key = await resolveKey(decoded);
|
|
152
|
+
} catch (e) {
|
|
153
|
+
return {
|
|
154
|
+
valid: false,
|
|
155
|
+
reason: `Error retrieving public key to verify JWT signature: ${
|
|
156
|
+
e instanceof Error ? e.message : e
|
|
157
|
+
}`
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if (!key) {
|
|
74
161
|
return {
|
|
75
162
|
valid: false,
|
|
76
|
-
reason: `
|
|
163
|
+
reason: `Unable to resolve public key to verify JWT signature.`
|
|
77
164
|
};
|
|
78
165
|
}
|
|
79
166
|
let signatureValid: boolean;
|
package/src/types.ts
CHANGED
|
@@ -4,6 +4,8 @@ export interface JwtPayload {
|
|
|
4
4
|
aud: string | Array<string>;
|
|
5
5
|
iat: number;
|
|
6
6
|
exp: number;
|
|
7
|
+
nbf?: number;
|
|
8
|
+
jti?: string;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
export interface JwtHeader {
|
|
@@ -46,4 +48,32 @@ export interface InvalidJwtParseResult {
|
|
|
46
48
|
reason: string;
|
|
47
49
|
}
|
|
48
50
|
|
|
51
|
+
export interface IssuerMetadata {
|
|
52
|
+
issuer: string;
|
|
53
|
+
authorization_endpoint?: string;
|
|
54
|
+
token_endpoint?: string;
|
|
55
|
+
jwks_uri?: string;
|
|
56
|
+
userinfo_endpoint?: string;
|
|
57
|
+
revocation_endpoint?: string;
|
|
58
|
+
end_session_endpoint?: string;
|
|
59
|
+
registration_endpoint?: string;
|
|
60
|
+
token_endpoint_auth_methods_supported?: string[];
|
|
61
|
+
token_endpoint_auth_signing_alg_values_supported?: string[];
|
|
62
|
+
introspection_endpoint_auth_methods_supported?: string[];
|
|
63
|
+
introspection_endpoint_auth_signing_alg_values_supported?: string[];
|
|
64
|
+
revocation_endpoint_auth_methods_supported?: string[];
|
|
65
|
+
revocation_endpoint_auth_signing_alg_values_supported?: string[];
|
|
66
|
+
request_object_signing_alg_values_supported?: string[];
|
|
67
|
+
mtls_endpoint_aliases?: MtlsEndpointAliases;
|
|
68
|
+
[key: string]: unknown;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface MtlsEndpointAliases {
|
|
72
|
+
token_endpoint?: string;
|
|
73
|
+
userinfo_endpoint?: string;
|
|
74
|
+
revocation_endpoint?: string;
|
|
75
|
+
introspection_endpoint?: string;
|
|
76
|
+
device_authorization_endpoint?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
49
79
|
export type JwtParseResult = ValidJwtParseResult | InvalidJwtParseResult;
|