-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathverifyJwt.ts
More file actions
192 lines (167 loc) · 5.98 KB
/
verifyJwt.ts
File metadata and controls
192 lines (167 loc) · 5.98 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type { Jwt, JwtPayload } from '@clerk/shared/types';
import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors';
import { runtime } from '../runtime';
import { base64url } from '../util/rfc4648';
import { getCryptoAlgorithm } from './algorithms';
import {
assertActivationClaim,
assertAudienceClaim,
assertAuthorizedPartiesClaim,
assertExpirationClaim,
assertHeaderAlgorithm,
assertHeaderType,
assertIssuedAtClaim,
assertSubClaim,
} from './assertions';
import { importKey } from './cryptoKeys';
import type { JwtReturnType } from './types';
const DEFAULT_CLOCK_SKEW_IN_MS = 5 * 1000;
export async function hasValidSignature(jwt: Jwt, key: JsonWebKey | string): Promise<JwtReturnType<boolean, Error>> {
const { header, signature, raw } = jwt;
const encoder = new TextEncoder();
const data = encoder.encode([raw.header, raw.payload].join('.'));
const algorithm = getCryptoAlgorithm(header.alg);
try {
const cryptoKey = await importKey(key, algorithm, 'verify');
const verified = await runtime.crypto.subtle.verify(
algorithm.name,
cryptoKey,
signature as Uint8Array<ArrayBuffer>,
data,
);
return { data: verified };
} catch (error) {
return {
errors: [
new TokenVerificationError({
reason: TokenVerificationErrorReason.TokenInvalidSignature,
message: (error as Error)?.message,
}),
],
};
}
}
export function decodeJwt(token: string): JwtReturnType<Jwt, TokenVerificationError> {
const tokenParts = (token || '').toString().split('.');
if (tokenParts.length !== 3) {
return {
errors: [
new TokenVerificationError({
reason: TokenVerificationErrorReason.TokenInvalid,
message: `Invalid JWT form. A JWT consists of three parts separated by dots.`,
}),
],
};
}
const [rawHeader, rawPayload, rawSignature] = tokenParts;
const decoder = new TextDecoder();
// To verify a JWS with SubtleCrypto you need to be careful to encode and decode
// the data properly between binary and base64url representation. Unfortunately
// the standard implementation in the V8 of btoa() and atob() are difficult to
// work with as they use "a Unicode string containing only characters in the
// range U+0000 to U+00FF, each representing a binary byte with values 0x00 to
// 0xFF respectively" as the representation of binary data.
// A better solution to represent binary data in Javascript is to use ES6 TypedArray
// and use a Javascript library to convert them to base64url that honors RFC 4648.
// Side note: The difference between base64 and base64url is the characters selected
// for value 62 and 63 in the standard, base64 encode them to + and / while base64url
// encode - and _.
// More info at https://stackoverflow.com/questions/54062583/how-to-verify-a-signed-jwt-with-subtlecrypto-of-the-web-crypto-API
const header = JSON.parse(decoder.decode(base64url.parse(rawHeader, { loose: true })));
const payload = JSON.parse(decoder.decode(base64url.parse(rawPayload, { loose: true })));
const signature = base64url.parse(rawSignature, { loose: true });
const data = {
header,
payload,
signature,
raw: {
header: rawHeader,
payload: rawPayload,
signature: rawSignature,
text: token,
},
} satisfies Jwt;
return { data };
}
/**
* @inline
*/
export type VerifyJwtOptions = {
/**
* A string or list of [audiences](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3). If passed, it is checked against the `aud` claim in the token.
*/
audience?: string | string[];
/**
* An allowlist of origins to verify against, to protect your application from the subdomain cookie leaking attack.
* @example
* ```ts
* ['http://localhost:3000', 'https://example.com']
* ```
*/
authorizedParties?: string[];
/**
* Specifies the allowed time difference (in milliseconds) between the Clerk server (which generates the token) and the clock of the user's application server when validating a token.
* @default 5000
*/
clockSkewInMs?: number;
/**
* @internal
*/
key: JsonWebKey | string;
/**
* A string or list of allowed [header types](https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.9).
* @default 'JWT'
*/
headerType?: string | string[];
};
export async function verifyJwt(
token: string,
options: VerifyJwtOptions,
): Promise<JwtReturnType<JwtPayload, TokenVerificationError>> {
const { audience, authorizedParties, clockSkewInMs, key, headerType } = options;
const clockSkew = clockSkewInMs || DEFAULT_CLOCK_SKEW_IN_MS;
const { data: decoded, errors } = decodeJwt(token);
if (errors) {
return { errors };
}
const { header, payload } = decoded;
try {
// Header verifications
const { typ, alg } = header;
assertHeaderType(typ, headerType);
assertHeaderAlgorithm(alg);
// Payload verifications
const { azp, sub, aud, iat, exp, nbf } = payload;
assertSubClaim(sub);
assertAudienceClaim([aud], [audience]);
assertAuthorizedPartiesClaim(azp, authorizedParties);
assertExpirationClaim(exp, clockSkew);
assertActivationClaim(nbf, clockSkew);
assertIssuedAtClaim(iat, clockSkew);
} catch (err) {
return { errors: [err as TokenVerificationError] };
}
const { data: signatureValid, errors: signatureErrors } = await hasValidSignature(decoded, key);
if (signatureErrors) {
return {
errors: [
new TokenVerificationError({
action: TokenVerificationErrorAction.EnsureClerkJWT,
reason: TokenVerificationErrorReason.TokenVerificationFailed,
message: `Error verifying JWT signature. ${signatureErrors[0]}`,
}),
],
};
}
if (!signatureValid) {
return {
errors: [
new TokenVerificationError({
reason: TokenVerificationErrorReason.TokenInvalidSignature,
message: 'JWT signature is invalid.',
}),
],
};
}
return { data: payload };
}