-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathBase.ts
342 lines (306 loc) · 11.2 KB
/
Base.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import type { ClerkJWTClaims } from '@clerk/types';
import type { CryptoKey as PeculiarCryptoKey } from '@peculiar/webcrypto';
import {
AuthErrorReason,
AuthState,
AuthStateParams,
AuthStatus,
BuildAuthenticatedStateOptions,
JWT,
TokenVerificationErrorReason,
VerifySessionTokenOptions,
} from './types';
import { parse } from './util/base64url';
import { isDevelopmentOrStaging, isProduction } from './util/clerkApiKey';
import { mapErrorReasonResponse, TokenVerificationError } from './util/errors';
import { checkClaims, isExpired } from './util/jwt';
import { checkCrossOrigin } from './util/request';
export const API_KEY = process.env.CLERK_API_KEY || '';
type ImportKeyFunction = (...args: any[]) => Promise<CryptoKey | PeculiarCryptoKey>;
type LoadCryptoKeyFunction = (token: string) => Promise<CryptoKey>;
type DecodeBase64Function = (base64Encoded: string) => string;
type VerifySignatureFunction = (...args: any[]) => Promise<boolean>;
export class Base {
importKeyFunction: ImportKeyFunction;
verifySignatureFunction: VerifySignatureFunction;
decodeBase64Function: DecodeBase64Function;
loadCryptoKeyFunction?: LoadCryptoKeyFunction;
/**
* Creates an instance of a Clerk Base.
* @param {ImportKeyFunction} importKeyFunction Function to import a PEM. Should have a similar result to crypto.subtle.importKey
* @param {LoadCryptoKeyFunction} loadCryptoKeyFunction Function load a PK CryptoKey from the host environment. Used for JWK clients etc.
* @param {VerifySignatureFunction} verifySignatureFunction Function to verify a CryptoKey or a similar structure later on. Should have a similar result to crypto.subtle.verify
* @param {DecodeBase64Function} decodeBase64Function Function to decode a Base64 string. Similar to atob
*/
constructor(
importKeyFunction: ImportKeyFunction,
verifySignatureFunction: VerifySignatureFunction,
decodeBase64Function: DecodeBase64Function,
loadCryptoKeyFunction?: LoadCryptoKeyFunction,
) {
this.importKeyFunction = importKeyFunction;
this.verifySignatureFunction = verifySignatureFunction;
this.decodeBase64Function = decodeBase64Function;
this.loadCryptoKeyFunction = loadCryptoKeyFunction;
}
/**
*
* Verify the session token retrieved using the public key.
*
* The public key will be supplied in the form of CryptoKey or will be loaded from the CLERK_JWT_KEY environment variable.
*
* @param {string} token
* @param {VerifySessionTokenOptions} verifySessionTokenOptions
* @return {Promise<ClerkJWTClaims>} claims
* @throws {TokenVerificationError|Error}
*/
verifySessionToken = async (
token: string,
{ authorizedParties, jwtKey }: VerifySessionTokenOptions = {},
): Promise<ClerkJWTClaims> => {
/**
* Priority of JWT key search
* 1. Use supplied key
* 2. Use load function
* 3. Try to load from env
*/
let availableKey: CryptoKey;
if (!jwtKey && this.loadCryptoKeyFunction) {
try {
availableKey = await this.loadCryptoKeyFunction(token);
} catch (_) {
throw new TokenVerificationError(TokenVerificationErrorReason.PublicKeyFetchError);
}
} else {
availableKey = await this.loadCryptoKey(jwtKey || process.env.CLERK_JWT_KEY);
}
const claims = await this.verifyJwt(availableKey, token);
checkClaims(claims, authorizedParties);
return claims;
};
/**
*
* @param {string} token Clerk JWT verification token
* Modify the RSA public key from the Clerk PEM supplied and return a contructed CryptoKey.
* You will find that at your application dashboard (https://dashboard.clerk.dev) under Settings -> API keys
*
*/
loadCryptoKey = async (key?: string): Promise<CryptoKey> => {
if (!key) {
throw new TokenVerificationError(TokenVerificationErrorReason.JWTKeyMissing);
}
// Next.js in development mode currently cannot parse PEM, but it can
// parse JWKs. This is a simple way to convert our PEM keys to JWKs
// until the bug is resolved.
const RSA_PREFIX = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA';
const RSA_SUFFIX = 'IDAQAB';
// JWK https://datatracker.ietf.org/doc/html/rfc7517
const jwk = {
kty: 'RSA',
n: key
.slice(RSA_PREFIX.length, RSA_SUFFIX.length * -1)
.replace(/\+/g, '-')
.replace(/\//g, '_'),
e: 'AQAB',
};
// Algorithm https://developer.mozilla.org/en-US/docs/Web/api/RsaHashedImportParams
const algorithm = {
name: 'RSASSA-PKCS1-v1_5',
hash: 'SHA-256',
};
// Based on https://developer.mozilla.org/en-US/docs/Web/api/SubtleCrypto/importKey#subjectpublickeyinfo_import
try {
return this.importKeyFunction(jwk, algorithm);
} catch (_) {
throw new TokenVerificationError(TokenVerificationErrorReason.ImportKeyError);
}
};
decodeJwt = (token: string): JWT => {
const tokenParts = token.split('.');
if (tokenParts.length !== 3) {
throw new TokenVerificationError(TokenVerificationErrorReason.MalformedToken);
}
const [rawHeader, rawPayload, rawSignature] = tokenParts;
const header = JSON.parse(this.decodeBase64Function(rawHeader));
const payload = JSON.parse(this.decodeBase64Function(rawPayload));
const signature = this.decodeBase64Function(rawSignature.replace(/_/g, '/').replace(/-/g, '+'));
return {
header,
payload,
signature,
};
};
verifyJwtSignature = async (key: CryptoKey, token: string) => {
const [rawHeader, rawPayload, rawSignature] = token.split('.');
const encoder = new TextEncoder();
const data = encoder.encode([rawHeader, rawPayload].join('.'));
const signature = parse(rawSignature);
const isVerified = await this.verifySignatureFunction('RSASSA-PKCS1-v1_5', key, signature, data);
if (!isVerified) {
throw new TokenVerificationError(TokenVerificationErrorReason.VerificationFailed);
}
};
verifyJwt = async (key: CryptoKey, token: string): Promise<ClerkJWTClaims> => {
const { payload } = this.decodeJwt(token);
await this.verifyJwtSignature(key, token);
isExpired(payload);
return payload;
};
/**
*
* Retrieve the authentication state for a request by using client specific information.
*
* @throws {Error} Token expired, Wrong azp, Malformed token. All of these cases should result in signed out state.
*
* @param {AuthStateParams}
* @returns {Promise<AuthState>}
*/
getAuthState = async ({
cookieToken,
clientUat,
headerToken,
origin,
host,
forwardedHost,
forwardedPort,
forwardedProto,
referrer,
userAgent,
authorizedParties,
fetchInterstitial,
jwtKey,
}: AuthStateParams): Promise<AuthState> => {
if (headerToken) {
return this.buildAuthenticatedState(headerToken, {
jwtKey,
authorizedParties,
fetchInterstitial,
tokenType: 'header',
});
}
const isDevelopmentKey = isDevelopmentOrStaging(API_KEY);
const isProductionKey = isProduction(API_KEY);
// In development or staging environments only, based on the request's
// User Agent, detect non-browser requests (e.g. scripts). If there
// is no Authorization header, consider the user as signed out and
// prevent interstitial rendering
if (isDevelopmentKey && !userAgent?.startsWith('Mozilla/')) {
return { status: AuthStatus.SignedOut, errorReason: AuthErrorReason.HeaderMissingNonBrowser };
}
// In cross-origin requests the use of Authorization header is mandatory
if (
origin &&
checkCrossOrigin({
originURL: new URL(origin),
host,
forwardedHost,
forwardedPort,
forwardedProto,
})
) {
return { status: AuthStatus.SignedOut, errorReason: AuthErrorReason.HeaderMissingCORS };
}
// First load of development. Could be logged in on Clerk-hosted UI.
if (isDevelopmentKey && !clientUat) {
return {
status: AuthStatus.Interstitial,
interstitial: await fetchInterstitial(),
errorReason: AuthErrorReason.UATMissing,
};
}
// Potentially arriving after a sign-in or sign-out on Clerk-hosted UI.
if (
isDevelopmentKey &&
referrer &&
checkCrossOrigin({
originURL: new URL(referrer),
host,
forwardedHost,
forwardedPort,
forwardedProto,
})
) {
return {
status: AuthStatus.Interstitial,
interstitial: await fetchInterstitial(),
errorReason: AuthErrorReason.CrossOriginReferrer,
};
}
// Probably first load for production
if (isProductionKey && !clientUat && !cookieToken) {
return { status: AuthStatus.SignedOut, errorReason: AuthErrorReason.CookieAndUATMissing };
}
// Signed out on a different subdomain but Clerk.js has not run to remove the cookie yet.
// TBD: Can enable if we do not want the __session cookie to be inspected.
// if (clientUat === '0' && cookieToken) {
// return {
// status: AuthStatus.Interstitial,
// interstitial: await fetchInterstitial(),
// };
// }
if (clientUat === '0') {
return { status: AuthStatus.SignedOut, errorReason: AuthErrorReason.StandardSignedOut };
}
if (isProductionKey && clientUat && !cookieToken) {
return {
status: AuthStatus.Interstitial,
interstitial: await fetchInterstitial(),
errorReason: AuthErrorReason.CookieMissing,
};
}
const authenticatedState = await this.buildAuthenticatedState(cookieToken as string, {
jwtKey,
authorizedParties,
fetchInterstitial,
tokenType: 'cookie',
});
if (authenticatedState.sessionClaims && authenticatedState.sessionClaims.iat >= Number(clientUat)) {
return authenticatedState;
} else if (authenticatedState.sessionClaims && authenticatedState.sessionClaims.iat < Number(clientUat)) {
return {
status: AuthStatus.Interstitial,
interstitial: await fetchInterstitial(),
errorReason: AuthErrorReason.CookieOutDated,
};
} else if (!authenticatedState.sessionClaims) {
return authenticatedState;
}
return {
status: AuthStatus.Interstitial,
interstitial: await fetchInterstitial(),
errorReason: AuthErrorReason.Unknown,
};
};
buildAuthenticatedState = async (
token: string,
{ authorizedParties, jwtKey, fetchInterstitial, tokenType }: BuildAuthenticatedStateOptions,
): Promise<AuthState> => {
try {
const sessionClaims = await this.verifySessionToken(token, {
authorizedParties,
jwtKey,
});
return {
status: AuthStatus.SignedIn,
session: {
id: sessionClaims.sid,
userId: sessionClaims.sub,
},
sessionClaims,
};
} catch (err) {
if (err instanceof TokenVerificationError) {
const { errorReason, shouldSignout } = mapErrorReasonResponse(err.reason, tokenType);
if (shouldSignout) {
return { status: AuthStatus.SignedOut, errorReason };
}
return {
status: AuthStatus.Interstitial,
interstitial: await fetchInterstitial(),
errorReason,
};
}
return { status: AuthStatus.SignedOut, errorReason: AuthErrorReason.InternalError };
}
};
}