2023-08-20 05:23:15 +00:00
|
|
|
import cors from 'cors';
|
|
|
|
|
import debug from 'debug';
|
2023-11-28 04:47:35 +00:00
|
|
|
import redis from '@umami/redis-client';
|
2023-08-20 05:23:15 +00:00
|
|
|
import { getAuthToken, parseShareToken } from 'lib/auth';
|
|
|
|
|
import { ROLES } from 'lib/constants';
|
2023-11-28 18:22:24 +00:00
|
|
|
import { secret } from 'lib/crypto';
|
2023-08-20 05:23:15 +00:00
|
|
|
import { findSession } from 'lib/session';
|
2023-05-16 03:41:12 +00:00
|
|
|
import {
|
|
|
|
|
badRequest,
|
2023-08-20 05:23:15 +00:00
|
|
|
createMiddleware,
|
2023-11-29 06:44:11 +00:00
|
|
|
forbidden,
|
2023-05-16 03:41:12 +00:00
|
|
|
parseSecureToken,
|
|
|
|
|
tooManyRequest,
|
2023-08-20 05:23:15 +00:00
|
|
|
unauthorized,
|
2023-05-16 03:41:12 +00:00
|
|
|
} from 'next-basics';
|
2023-03-30 18:18:57 +00:00
|
|
|
import { NextApiRequestCollect } from 'pages/api/send';
|
2023-08-20 05:23:15 +00:00
|
|
|
import { getUserById } from '../queries';
|
2022-11-08 06:35:51 +00:00
|
|
|
|
|
|
|
|
const log = debug('umami:middleware');
|
2020-07-18 17:36:46 +00:00
|
|
|
|
2023-01-31 05:44:07 +00:00
|
|
|
export const useCors = createMiddleware(
|
|
|
|
|
cors({
|
|
|
|
|
// Cache CORS preflight request 24 hours by default
|
2023-09-20 22:26:49 +00:00
|
|
|
maxAge: Number(process.env.CORS_MAX_AGE) || 86400,
|
2023-01-31 05:44:07 +00:00
|
|
|
}),
|
|
|
|
|
);
|
2020-07-28 06:52:14 +00:00
|
|
|
|
2022-02-26 23:53:45 +00:00
|
|
|
export const useSession = createMiddleware(async (req, res, next) => {
|
2023-04-22 21:17:57 +00:00
|
|
|
try {
|
|
|
|
|
const session = await findSession(req as NextApiRequestCollect);
|
2020-08-08 00:19:42 +00:00
|
|
|
|
2023-04-22 21:17:57 +00:00
|
|
|
if (!session) {
|
|
|
|
|
log('useSession: Session not found');
|
|
|
|
|
return badRequest(res, 'Session not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(req as any).session = session;
|
|
|
|
|
} catch (e: any) {
|
2023-05-16 03:41:12 +00:00
|
|
|
if (e.message === 'Usage Limit.') {
|
|
|
|
|
return tooManyRequest(res, e.message);
|
|
|
|
|
}
|
2023-11-29 06:44:11 +00:00
|
|
|
if (e.message.startsWith('Website not found:')) {
|
|
|
|
|
return forbidden(res, e.message);
|
|
|
|
|
}
|
2023-04-22 21:17:57 +00:00
|
|
|
return badRequest(res, e.message);
|
2020-07-28 06:52:14 +00:00
|
|
|
}
|
2020-08-08 00:19:42 +00:00
|
|
|
|
2020-07-28 06:52:14 +00:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-26 23:53:45 +00:00
|
|
|
export const useAuth = createMiddleware(async (req, res, next) => {
|
2022-11-09 06:58:52 +00:00
|
|
|
const token = getAuthToken(req);
|
2022-11-12 19:33:14 +00:00
|
|
|
const payload = parseSecureToken(token, secret());
|
2023-11-28 04:47:35 +00:00
|
|
|
const shareToken = await parseShareToken(req as any);
|
2020-08-08 00:19:42 +00:00
|
|
|
|
2022-11-16 19:44:36 +00:00
|
|
|
let user = null;
|
2023-08-22 22:37:22 +00:00
|
|
|
const { userId, authKey, grant } = payload || {};
|
2022-11-09 18:16:50 +00:00
|
|
|
|
2023-11-28 18:22:24 +00:00
|
|
|
if (userId) {
|
2023-07-30 05:03:34 +00:00
|
|
|
user = await getUserById(userId);
|
2023-11-28 04:47:35 +00:00
|
|
|
} else if (redis.enabled && authKey) {
|
|
|
|
|
const key = await redis.client.get(authKey);
|
|
|
|
|
|
2023-12-01 06:24:43 +00:00
|
|
|
if (key?.userId) {
|
|
|
|
|
user = await getUserById(key.userId);
|
|
|
|
|
}
|
2022-11-09 14:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-09 00:37:43 +00:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
2023-11-28 18:22:24 +00:00
|
|
|
log('useAuth:', { token, shareToken, payload, user, grant });
|
2023-03-09 00:37:43 +00:00
|
|
|
}
|
2022-11-11 17:42:54 +00:00
|
|
|
|
2023-01-31 05:44:07 +00:00
|
|
|
if (!user?.id && !shareToken) {
|
2022-12-27 05:51:16 +00:00
|
|
|
log('useAuth: User not authorized');
|
2020-08-12 03:05:40 +00:00
|
|
|
return unauthorized(res);
|
2020-07-28 06:52:14 +00:00
|
|
|
}
|
2020-08-08 00:19:42 +00:00
|
|
|
|
2022-12-07 02:36:41 +00:00
|
|
|
if (user) {
|
|
|
|
|
user.isAdmin = user.role === ROLES.admin;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 22:37:22 +00:00
|
|
|
(req as any).auth = {
|
|
|
|
|
user,
|
|
|
|
|
grant,
|
|
|
|
|
token,
|
|
|
|
|
shareToken,
|
|
|
|
|
authKey,
|
|
|
|
|
};
|
2023-07-26 06:59:08 +00:00
|
|
|
|
2020-07-28 06:52:14 +00:00
|
|
|
next();
|
|
|
|
|
});
|
2023-08-20 05:23:15 +00:00
|
|
|
|
2023-09-29 12:29:22 +00:00
|
|
|
export const useValidate = async (schema, req, res) => {
|
|
|
|
|
return createMiddleware(async (req: any, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const rules = schema[req.method];
|
2023-08-20 05:23:15 +00:00
|
|
|
|
2023-09-29 12:29:22 +00:00
|
|
|
if (rules) {
|
2023-10-04 08:46:00 +00:00
|
|
|
rules.validateSync({ ...req.query, ...req.body });
|
2023-09-29 12:29:22 +00:00
|
|
|
}
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
return badRequest(res, e.message);
|
|
|
|
|
}
|
2023-08-20 05:23:15 +00:00
|
|
|
|
2023-09-29 12:29:22 +00:00
|
|
|
next();
|
|
|
|
|
})(req, res);
|
|
|
|
|
};
|