-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmiddleware.tsx
More file actions
423 lines (388 loc) · 14.4 KB
/
middleware.tsx
File metadata and controls
423 lines (388 loc) · 14.4 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import { NextResponse, NextRequest } from 'next/server';
const authWeb = `${process.env.NEXT_PUBLIC_APP_URI}/user`;
export type MiddlewareHook = (req: NextRequest) => Promise<{
activated: boolean;
response: NextResponse;
}>;
export const generateCookieString = (key: string, value: string, age: string): string =>
`${key}=${value}; Domain=${process.env.NEXT_PUBLIC_COOKIE_DOMAIN}; Path=/; Max-Age=${age}; SameSite=Lax;`;
export const getQueryParams = (req: NextRequest): any =>
req.url.includes('?')
? Object.assign(
{},
...req.url
.split('?')[1]
.split('&')
.map((param) => ({ [param.split('=')[0]]: param.split('=')[1] })),
)
: {};
export const getRequestedURI = (req: NextRequest): string => {
return req.url
.split('?')[0]
.replace(/localhost:\d+/, (process.env.APP_URI || '').replace('https://', '').replace('http://', ''));
};
export const getJWT = (req: NextRequest) => {
const rawJWT = req.cookies.get('jwt')?.value;
// Strip any and all 'Bearer 's off of JWT.
const jwt = rawJWT?.split(' ')[rawJWT?.split(' ').length - 1] ?? rawJWT ?? '';
return jwt;
};
export const verifyJWT = async (jwt: string): Promise<Response> => {
if (!process.env.SERVERSIDE_AGIXT_SERVER) {
process.env.SERVERSIDE_AGIXT_SERVER = ['agixt', 'localhost', 'back-end', 'boilerplate', 'back-end-image'].join(',');
}
const containerNames = process.env.SERVERSIDE_AGIXT_SERVER.split(',');
const responses = {} as any;
const authEndpoint = `${process.env.AGIXT_SERVER}/v1/user`;
let response;
for (const containerName of containerNames) {
const testEndpoint = authEndpoint.replace('localhost', containerName);
try {
response = await fetch(testEndpoint, {
headers: {
'Content-Type': 'application/json',
Authorization: `${jwt}`,
},
});
if (response.status === 200 || [401, 402, 403].includes(response.status)) {
if (Object.keys(responses).length > 0) {
containerNames.sort((a, b) => {
if (a === containerName) {
return -1;
} else if (b === containerName) {
return 1;
} else {
return 0;
}
});
process.env.SERVERSIDE_AGIXT_SERVER = containerNames.join(',');
}
return response;
} else {
responses[testEndpoint] = await response.text();
console.error(`Failed to contact server at ${testEndpoint}.`);
}
} catch (exception) {
responses[testEndpoint] = exception;
}
}
console.error('Failed to contact any of the following servers: ', JSON.stringify(responses));
for (const key of Object.keys(responses)) {
console.error(key, responses[key]);
}
return new Response();
};
export const useAuth: MiddlewareHook = async (req) => {
// Initialize response object to redirect to auth page
const toReturn = {
activated: false,
response: NextResponse.redirect(new URL(authWeb as string), {
headers: {
'Set-Cookie': [generateCookieString('jwt', '', '0')], // Always clear JWT on redirect to auth
},
}),
};
const requestedURI = getRequestedURI(req);
const queryParams = getQueryParams(req);
// Skip auth check for logout path
if (requestedURI.endsWith('/user/logout')) {
return toReturn;
}
// Handle email verification
if (queryParams.verify_email && queryParams.email) {
await fetch(`${process.env.AGIXT_SERVER}/v1/user/verify/email`, {
method: 'POST',
body: JSON.stringify({
email: queryParams.email,
code: queryParams.verify_email,
}),
headers: {
'Content-Type': 'application/json',
},
});
}
if (queryParams.invitation_id && queryParams.email) {
const cookieArray = [
generateCookieString('email', queryParams.email, (86400).toString()),
generateCookieString('invitation', queryParams.invitation_id, (86400).toString()),
];
if (queryParams.company) {
cookieArray.push(generateCookieString('company', queryParams.company, (86400).toString()));
}
toReturn.activated = true;
toReturn.response = NextResponse.redirect(`${authWeb}/register`, {
// @ts-expect-error NextJS' types are wrong.
headers: {
'Set-Cookie': cookieArray,
},
});
return toReturn;
}
// Check if the route requires authentication
const isPrivateRoute = process.env.PRIVATE_ROUTES?.split(',').some((path) => req.nextUrl.pathname.startsWith(path));
// Skip auth for public user routes (login, register) and OAuth close page
if (
req.nextUrl.pathname.startsWith('/user/close') ||
req.nextUrl.pathname.startsWith('/user/mobile') ||
req.nextUrl.pathname === '/user' ||
req.nextUrl.pathname === '/user/login' ||
req.nextUrl.pathname === '/user/register'
) {
toReturn.activated = false;
return toReturn;
}
// Skip auth check for non-private routes that aren't in /user path
if (!isPrivateRoute && !req.nextUrl.pathname.startsWith('/user')) {
toReturn.activated = false;
return toReturn;
}
// Get JWT from cookies
const jwt = getJWT(req);
// If no JWT and we're on a private route or protected user route, redirect to auth and activate
if (
!jwt &&
(isPrivateRoute ||
(req.nextUrl.pathname.startsWith('/user') &&
!req.nextUrl.pathname.startsWith('/user/register') &&
!req.nextUrl.pathname.startsWith('/user/login') &&
req.nextUrl.pathname !== '/user'))
) {
toReturn.activated = true;
toReturn.response.headers.set('Set-Cookie', [
generateCookieString('jwt', '', '0'),
generateCookieString('href', requestedURI, (86400).toString()),
]);
return toReturn;
}
// If JWT exists, verify it
if (jwt) {
try {
const response = await verifyJWT(jwt);
const responseJSON = await response.json();
if (response.status === 401 || response.status === 403) {
// Unauthorized/Forbidden - Clear JWT and redirect to auth page
toReturn.response = NextResponse.redirect(new URL(authWeb, req.url), {
headers: {
'Set-Cookie': [
generateCookieString('jwt', '', '0'),
generateCookieString('href', requestedURI, (86400).toString()),
],
},
});
toReturn.activated = true;
console.error(
`${response.status === 401 ? 'Unauthorized' : 'Forbidden'} access, status ${response.status}, detail ${responseJSON.detail}. Clearing JWT and redirecting to auth.`,
);
} else if (response.status === 402) {
// Payment Required
if (!requestedURI.startsWith(`${authWeb}/subscribe`)) {
toReturn.response = NextResponse.redirect(
new URL(
`${authWeb}/subscribe${
responseJSON.detail.customer_session.client_secret
? '?customer_session=' + responseJSON.detail.customer_session.client_secret
: ''
}`,
),
);
toReturn.activated = true;
}
} else if (response.status === 502) {
const cookieArray = [generateCookieString('href', requestedURI, (86400).toString())];
toReturn.activated = true;
toReturn.response = NextResponse.redirect(new URL(`${authWeb}/down`, req.url), {
// @ts-expect-error NextJS' types are wrong.
headers: {
'Set-Cookie': cookieArray,
},
});
} else if (response.status >= 500 && response.status < 600) {
// Internal Server Error - Don't delete JWT for server errors
console.error(
`Invalid token response, status ${response.status}, detail ${responseJSON.detail}. Server error, please try again later.`,
);
toReturn.response = NextResponse.redirect(new URL(`${authWeb}/error`, req.url));
toReturn.activated = true;
} else if (response.status !== 200) {
// Invalid JWT - clear JWT and redirect to auth page
toReturn.response = NextResponse.redirect(new URL(authWeb, req.url), {
headers: {
'Set-Cookie': [
generateCookieString('jwt', '', '0'),
generateCookieString('href', requestedURI, (86400).toString()),
],
},
});
toReturn.activated = true;
console.error(`Invalid token response, status ${response.status}, detail ${responseJSON.detail}.`);
} else if (requestedURI.startsWith(authWeb) && jwt.length > 0 && !['/user/manage'].includes(req.nextUrl.pathname)) {
// Valid JWT but on auth page - redirect to manage
toReturn.response = NextResponse.redirect(new URL(`${authWeb}/manage`));
toReturn.activated = true;
}
} catch (exception) {
// Handle JWT verification errors
logJwtError(exception, authWeb);
// Clear JWT and redirect to auth
toReturn.response = NextResponse.redirect(new URL(authWeb, req.url), {
headers: {
'Set-Cookie': [
generateCookieString('jwt', '', '0'),
generateCookieString('href', requestedURI, (86400).toString()),
],
},
});
toReturn.activated = true;
}
}
return toReturn;
};
// Helper function to log JWT errors
function logJwtError(exception: any, authWeb: string) {
if (exception instanceof TypeError && exception.cause instanceof AggregateError) {
console.error(
`Invalid token. Failed with TypeError>AggregateError. Logging out and redirecting to authentication at ${authWeb}. ${exception.message} Exceptions to follow.`,
);
for (const anError of exception.cause.errors) {
console.error(anError.message);
}
} else if (exception instanceof AggregateError) {
console.error(
`Invalid token. Failed with AggregateError. Logging out and redirecting to authentication at ${authWeb}. ${exception.message} Exceptions to follow.`,
);
for (const anError of exception.errors) {
console.error(anError.message);
}
} else if (exception instanceof TypeError) {
console.error(
`Invalid token. Failed with TypeError. Logging out and redirecting to authentication at ${authWeb}. ${exception.message} Cause: ${exception.cause}.`,
);
} else {
console.error(`Invalid token. Logging out and redirecting to authentication at ${authWeb}.`, exception);
}
}
export const useOAuth2: MiddlewareHook = async (req) => {
const provider = req.nextUrl.pathname.split('?')[0].split('/').pop();
const redirect = req.nextUrl.pathname.startsWith('/user/mobile')
? new URL(`${authWeb}/mobile/${provider}`)
: new URL(`${authWeb}/close/${provider}`);
let toReturn = {
activated: false,
response: NextResponse.redirect(redirect),
};
const queryParams = getQueryParams(req);
if (queryParams.code) {
const oAuthEndpoint = `${process.env.AGIXT_SERVER || ''.replace('localhost', (process.env.SERVERSIDE_AGIXT_SERVER || '').split(',')[0])}/v1/oauth2/${provider}`;
const jwt = getJWT(req);
try {
const response = await fetch(oAuthEndpoint, {
method: 'POST',
body: JSON.stringify({
code: queryParams.code,
referrer: redirect.toString(),
state: queryParams.state,
invitation: req.cookies.get('invitation')?.value,
}),
headers: {
'Content-Type': 'application/json',
Authorization: jwt || '',
},
});
const auth = await response.json();
if (response.status !== 200) {
throw new Error(`Invalid token response, status ${response.status}.`);
}
// Forward the original JWT in the response if present
const headers = new Headers();
if (jwt) {
headers.set('Authorization', jwt);
}
toReturn = {
activated: true,
response: NextResponse.redirect(auth.detail, {
headers: headers,
}),
};
} catch (error) {
console.error('Middleware OAuth2 error:', error);
}
}
return toReturn;
};
export const useJWTQueryParam: MiddlewareHook = async (req) => {
const queryParams = getQueryParams(req);
const requestedURI = getRequestedURI(req);
const toReturn = {
activated: false,
// This should set the cookie and then re-run the middleware (without query params).
response:
req.nextUrl.pathname.startsWith('/user/close') || req.nextUrl.pathname.startsWith('/user/mobile')
? NextResponse.next({
// @ts-expect-error NextJS' types are wrong.
headers: {
'Set-Cookie': [generateCookieString('jwt', queryParams.token ?? queryParams.jwt, (86400 * 7).toString())],
},
})
: NextResponse.redirect(req.cookies.get('href')?.value ?? process.env.APP_URI ?? '', {
// @ts-expect-error NextJS' types are wrong.
headers: {
'Set-Cookie': [
generateCookieString('jwt', queryParams.token ?? queryParams.jwt, (86400 * 7).toString()),
generateCookieString('href', '', (0).toString()),
],
},
}),
};
if (queryParams.token || queryParams.jwt) {
toReturn.activated = true;
}
return toReturn;
};
export const useNextAPIBypass: MiddlewareHook = async (req) => {
const toReturn = {
activated: false,
response: NextResponse.next(),
};
if (
req.nextUrl.pathname.startsWith('/_next/') ||
req.nextUrl.pathname.startsWith('/api/') ||
req.nextUrl.pathname === '/favicon.ico'
) {
toReturn.activated = true;
}
return toReturn;
};
export const useSocketIOBypass: MiddlewareHook = async (req) => {
const url = new URL(getRequestedURI(req));
return {
activated: url.host === 'socket.io',
response: NextResponse.next(),
};
};
export const useDocsPublicAccess: MiddlewareHook = async (req) => {
if (req.nextUrl.pathname === '/docs') {
return {
activated: true,
response: NextResponse.redirect(new URL('/docs/0-Introduction', req.url)),
};
}
return {
activated: req.nextUrl.pathname.startsWith('/docs'),
response: NextResponse.next(),
};
};
export default async function Middleware(req: NextRequest): Promise<NextResponse> {
const hooks = [useNextAPIBypass, useDocsPublicAccess, useOAuth2, useJWTQueryParam, useAuth];
for (const hook of hooks) {
const hookResult = await hook(req);
if (hookResult.activated) {
hookResult.response.headers.set('x-next-pathname', req.nextUrl.pathname);
return hookResult.response;
}
}
return NextResponse.next({
headers: {
'x-next-pathname': req.nextUrl.pathname,
},
});
}