forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthMiddleware.ts
More file actions
345 lines (306 loc) · 13.7 KB
/
Copy pathauthMiddleware.ts
File metadata and controls
345 lines (306 loc) · 13.7 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
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
343
344
345
import type { AuthenticateRequestOptions, AuthObject, ClerkRequest } from '@clerk/backend/internal';
import { AuthStatus, constants, createClerkRequest, createRedirect } from '@clerk/backend/internal';
import { isDevelopmentFromSecretKey } from '@clerk/shared/keys';
import { eventMethodCalled } from '@clerk/shared/telemetry';
import type { NextFetchEvent, NextMiddleware, NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { isRedirect, mergeResponses, serverRedirectWithAuth, setHeader, stringifyHeaders } from '../utils';
import { withLogger } from '../utils/debugLogger';
import { clerkClient } from './clerkClient';
import { createAuthenticateRequestOptions } from './clerkMiddleware';
import { PUBLISHABLE_KEY, SECRET_KEY, SIGN_IN_URL, SIGN_UP_URL } from './constants';
import { informAboutProtectedRouteInfo, receivedRequestForIgnoredRoute } from './errors';
import { errorThrower } from './errorThrower';
import type { RouteMatcherParam } from './routeMatcher';
import { createRouteMatcher } from './routeMatcher';
import type { NextMiddlewareReturn } from './types';
import {
apiEndpointUnauthorizedNextResponse,
assertKey,
decorateRequest,
redirectAdapter,
setRequestHeadersOnNextResponse,
} from './utils';
/**
* The default ideal matcher that excludes the _next directory (internals) and all static files,
* but it will match the root route (/) and any routes that start with /api or /trpc.
*/
export const DEFAULT_CONFIG_MATCHER = ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'];
/**
* Any routes matching this path will be ignored by the middleware.
* This is the inverted version of DEFAULT_CONFIG_MATCHER.
*/
export const DEFAULT_IGNORED_ROUTES = [`/((?!api|trpc))(_next.*|.+\\.[\\w]+$)`];
/**
* Any routes matching this path will be treated as API endpoints by the middleware.
*/
export const DEFAULT_API_ROUTES = ['/api/(.*)', '/trpc/(.*)'];
type IgnoredRoutesParam = Array<RegExp | string> | RegExp | string | ((req: NextRequest) => boolean);
type ApiRoutesParam = IgnoredRoutesParam;
type WithExperimentalClerkUrl<T> = T & {
/**
* When a NextJs app is hosted on a platform different from Vercel
* or inside a container (Netlify, Fly.io, AWS Amplify, docker etc),
* req.url is always set to `localhost:3000` instead of the actual host of the app.
*
* The `authMiddleware` uses the value of the available req.headers in order to construct
* and use the correct url internally. This url is then exposed as `experimental_clerkUrl`,
* intended to be used within `beforeAuth` and `afterAuth` if needed.
*/
experimental_clerkUrl: NextRequest['nextUrl'];
};
type BeforeAuthHandler = (
req: WithExperimentalClerkUrl<NextRequest>,
evt: NextFetchEvent,
) => NextMiddlewareReturn | false | Promise<false>;
type AfterAuthHandler = (
auth: AuthObject & { isPublicRoute: boolean; isApiRoute: boolean },
req: WithExperimentalClerkUrl<NextRequest>,
evt: NextFetchEvent,
) => NextMiddlewareReturn;
type AuthMiddlewareParams = AuthenticateRequestOptions & {
/**
* A function that is called before the authentication middleware is executed.
* If a redirect response is returned, the middleware will respect it and redirect the user.
* If false is returned, the auth middleware will not execute and the request will be handled as if the auth middleware was not present.
*/
beforeAuth?: BeforeAuthHandler;
/**
* A function that is called after the authentication middleware is executed.
* This function has access to the auth object and can be used to execute logic based on the auth state.
*/
afterAuth?: AfterAuthHandler;
/**
* A list of routes that should be accessible without authentication.
* You can use glob patterns to match multiple routes or a function to match against the request object.
* Path patterns and regular expressions are supported, for example: `['/foo', '/bar(.*)'] or `[/^\/foo\/.*$/]`
* The sign in and sign up URLs are included by default, unless a function is provided.
* For more information, see: https://clerk.com/docs
*/
publicRoutes?: RouteMatcherParam;
/**
* A list of routes that should be ignored by the middleware.
* This list typically includes routes for static files or Next.js internals.
* For improved performance, these routes should be skipped using the default config.matcher instead.
*/
ignoredRoutes?: IgnoredRoutesParam;
/**
* A list of routes that should be treated as API endpoints.
* When user is signed out, the middleware will return a 401 response for these routes, instead of redirecting the user.
*
* If omitted, the following heuristics will be used to determine an API endpoint:
* - The route path is ['/api/(.*)', '/trpc/(.*)'],
* - or the request has `Content-Type` set to `application/json`,
* - or the request method is not one of: `GET`, `OPTIONS` ,` HEAD`
*
* @default undefined
*/
apiRoutes?: ApiRoutesParam;
/**
* Enables extra debug logging.
*/
debug?: boolean;
};
export interface AuthMiddleware {
(params?: AuthMiddlewareParams): NextMiddleware;
}
/**
* @deprecated `authMiddleware` is deprecated and will be removed in the next major version.
* Use {@link clerkMiddleware}` instead.
* Migration guide: https://clerk.com/docs/upgrade-guides/upgrading-from-v4-to-v5
*/
const authMiddleware: AuthMiddleware = (...args: unknown[]) => {
const [params = {}] = args as [AuthMiddlewareParams?];
const publishableKey = assertKey(params.publishableKey || PUBLISHABLE_KEY, () =>
errorThrower.throwMissingPublishableKeyError(),
);
const secretKey = assertKey(params.secretKey || SECRET_KEY, () => errorThrower.throwMissingPublishableKeyError());
const signInUrl = params.signInUrl || SIGN_IN_URL;
const signUpUrl = params.signUpUrl || SIGN_UP_URL;
const options = { ...params, publishableKey, secretKey, signInUrl, signUpUrl };
const isIgnoredRoute = createRouteMatcher(options.ignoredRoutes || DEFAULT_IGNORED_ROUTES);
const isPublicRoute = createRouteMatcher(withDefaultPublicRoutes(options.publicRoutes));
const isApiRoute = createApiRoutes(options.apiRoutes);
const defaultAfterAuth = createDefaultAfterAuth(isPublicRoute, isApiRoute, options);
clerkClient.telemetry.record(
eventMethodCalled('authMiddleware', {
publicRoutes: Boolean(options.publicRoutes),
ignoredRoutes: Boolean(options.ignoredRoutes),
beforeAuth: Boolean(options.beforeAuth),
afterAuth: Boolean(options.afterAuth),
}),
);
return withLogger('authMiddleware', logger => async (_req: NextRequest, evt: NextFetchEvent) => {
if (options.debug) {
logger.enable();
}
const clerkRequest = createClerkRequest(_req);
const nextRequest = withNormalizedClerkUrl(clerkRequest, _req);
logger.debug('URL debug', {
url: nextRequest.nextUrl.href,
method: nextRequest.method,
headers: stringifyHeaders(nextRequest.headers),
nextUrl: nextRequest.nextUrl.href,
clerkUrl: nextRequest.experimental_clerkUrl.href,
});
logger.debug('Options debug', { ...options, beforeAuth: !!options.beforeAuth, afterAuth: !!options.afterAuth });
if (isIgnoredRoute(nextRequest)) {
logger.debug({ isIgnoredRoute: true });
if (isDevelopmentFromSecretKey(options.secretKey) && !options.ignoredRoutes) {
console.warn(
receivedRequestForIgnoredRoute(
nextRequest.experimental_clerkUrl.href,
JSON.stringify(DEFAULT_CONFIG_MATCHER),
),
);
}
return setHeader(NextResponse.next(), constants.Headers.AuthReason, 'ignored-route');
}
const beforeAuthRes = await (options.beforeAuth && options.beforeAuth(nextRequest, evt));
if (beforeAuthRes === false) {
logger.debug('Before auth returned false, skipping');
return setHeader(NextResponse.next(), constants.Headers.AuthReason, 'skip');
} else if (beforeAuthRes && isRedirect(beforeAuthRes)) {
logger.debug('Before auth returned redirect, following redirect');
return setHeader(beforeAuthRes, constants.Headers.AuthReason, 'before-auth-redirect');
}
const requestState = await clerkClient.authenticateRequest(
clerkRequest,
createAuthenticateRequestOptions(clerkRequest, options),
);
const locationHeader = requestState.headers.get('location');
if (locationHeader) {
// triggering a handshake redirect
return new Response(null, { status: 307, headers: requestState.headers });
}
if (requestState.status === AuthStatus.Handshake) {
throw new Error('Clerk: unexpected handshake without redirect');
}
const auth = Object.assign(requestState.toAuth(), {
isPublicRoute: isPublicRoute(nextRequest),
isApiRoute: isApiRoute(nextRequest),
});
logger.debug(() => ({ auth: JSON.stringify(auth), debug: auth.debug() }));
const afterAuthRes = await (options.afterAuth || defaultAfterAuth)(auth, nextRequest, evt);
const finalRes = mergeResponses(beforeAuthRes, afterAuthRes) || NextResponse.next();
logger.debug(() => ({ mergedHeaders: stringifyHeaders(finalRes.headers) }));
if (isRedirect(finalRes)) {
logger.debug('Final response is redirect, following redirect');
return serverRedirectWithAuth(clerkRequest, finalRes, options);
}
if (options.debug) {
setRequestHeadersOnNextResponse(finalRes, nextRequest, { [constants.Headers.EnableDebug]: 'true' });
logger.debug(`Added ${constants.Headers.EnableDebug} on request`);
}
const result = decorateRequest(clerkRequest, finalRes, requestState) || NextResponse.next();
if (requestState.headers) {
requestState.headers.forEach((value, key) => {
result.headers.append(key, value);
});
}
return result;
});
};
export { authMiddleware };
const createDefaultAfterAuth = (
isPublicRoute: ReturnType<typeof createRouteMatcher>,
isApiRoute: ReturnType<typeof createApiRoutes>,
options: { signInUrl: string; signUpUrl: string; publishableKey: string; secretKey: string },
) => {
return (auth: AuthObject, req: WithExperimentalClerkUrl<NextRequest>) => {
if (!auth.userId && !isPublicRoute(req)) {
if (isApiRoute(req)) {
informAboutProtectedRoute(req.experimental_clerkUrl.pathname, options, true);
return apiEndpointUnauthorizedNextResponse();
} else {
informAboutProtectedRoute(req.experimental_clerkUrl.pathname, options, false);
}
return createRedirect({
redirectAdapter,
signInUrl: options.signInUrl,
signUpUrl: options.signUpUrl,
publishableKey: options.publishableKey,
// We're setting baseUrl to '' here as we want to keep the legacy behavior of
// the redirectToSignIn, redirectToSignUp helpers in the backend package.
baseUrl: '',
}).redirectToSignIn({ returnBackUrl: req.experimental_clerkUrl.href });
}
return NextResponse.next();
};
};
const matchRoutesStartingWith = (path: string) => {
path = path.replace(/\/$/, '');
return new RegExp(`^${path}(/.*)?$`);
};
const withDefaultPublicRoutes = (publicRoutes: RouteMatcherParam | undefined) => {
if (typeof publicRoutes === 'function') {
return publicRoutes;
}
const routes = [publicRoutes || ''].flat().filter(Boolean);
// TODO: refactor it to use common config file eg SIGN_IN_URL from ./clerkClient
// we use process.env for now to support testing
const signInUrl = process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL || '';
if (signInUrl) {
routes.push(matchRoutesStartingWith(signInUrl));
}
// TODO: refactor it to use common config file eg SIGN_UP_URL from ./clerkClient
// we use process.env for now to support testing
const signUpUrl = process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL || '';
if (signUpUrl) {
routes.push(matchRoutesStartingWith(signUpUrl));
}
return routes;
};
// - Default behavior:
// If the route path is `['/api/(.*)*', '*/trpc/(.*)']`
// or Request has `Content-Type: application/json`
// or Request method is not-GET,OPTIONS,HEAD,
// then this is considered an API route.
//
// - If the user has provided a specific `apiRoutes` prop in `authMiddleware` then all the above are discarded,
// and only routes that match the user’s provided paths are considered API routes.
const createApiRoutes = (
apiRoutes: RouteMatcherParam | undefined,
): ((req: WithExperimentalClerkUrl<NextRequest>) => boolean) => {
if (apiRoutes) {
return createRouteMatcher(apiRoutes);
}
const isDefaultApiRoute = createRouteMatcher(DEFAULT_API_ROUTES);
return (req: WithExperimentalClerkUrl<NextRequest>) =>
isDefaultApiRoute(req) || isRequestMethodIndicatingApiRoute(req) || isRequestContentTypeJson(req);
};
const isRequestContentTypeJson = (req: NextRequest): boolean => {
const requestContentType = req.headers.get(constants.Headers.ContentType);
return requestContentType === constants.ContentTypes.Json;
};
const isRequestMethodIndicatingApiRoute = (req: NextRequest): boolean => {
const requestMethod = req.method.toLowerCase();
return !['get', 'head', 'options'].includes(requestMethod);
};
const withNormalizedClerkUrl = (
clerkRequest: ClerkRequest,
nextRequest: NextRequest,
): WithExperimentalClerkUrl<NextRequest> => {
const res = nextRequest.nextUrl.clone();
res.port = clerkRequest.clerkUrl.port;
res.protocol = clerkRequest.clerkUrl.protocol;
res.host = clerkRequest.clerkUrl.host;
return Object.assign(nextRequest, { experimental_clerkUrl: res });
};
const informAboutProtectedRoute = (
path: string,
options: AuthMiddlewareParams & { secretKey: string },
isApiRoute: boolean,
) => {
if (options.debug || isDevelopmentFromSecretKey(options.secretKey)) {
console.warn(
informAboutProtectedRouteInfo(
path,
!!options.publicRoutes,
!!options.ignoredRoutes,
isApiRoute,
DEFAULT_IGNORED_ROUTES,
),
);
}
};