umami/lib/middleware.ts

93 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-12-27 06:51:16 +01:00
import redis from '@umami/redis-client';
2023-01-02 08:20:14 +01:00
import cors from 'cors';
import debug from 'debug';
2022-11-22 07:32:55 +01:00
import { getAuthToken, parseShareToken } from 'lib/auth';
2022-12-07 03:36:41 +01:00
import { ROLES } from 'lib/constants';
2023-01-02 08:20:14 +01:00
import { secret } from 'lib/crypto';
import { isOverApiLimit, findSession, findWebsite, useSessionCache } from 'lib/session';
import {
badRequest,
createMiddleware,
parseSecureToken,
tooManyRequest,
unauthorized,
} from 'next-basics';
import { NextApiRequestCollect } from 'pages/api/collect';
import { validate } from 'uuid';
2022-11-09 15:40:36 +01:00
import { getUser } from '../queries';
2023-01-02 08:20:14 +01:00
import { getJsonBody } from './detect';
const log = debug('umami:middleware');
2022-02-27 00:53:45 +01:00
export const useCors = createMiddleware(cors());
2020-07-28 08:52:14 +02:00
2023-01-02 08:20:14 +01:00
export const useSession = createMiddleware(async (req: NextApiRequestCollect, res, next) => {
// Verify payload
const { payload } = getJsonBody(req);
const { website: websiteId } = payload;
if (!payload) {
log('useSession: No payload');
return badRequest(res);
}
// Get session from cache
let session = await useSessionCache(req);
// Get or create session
if (!session) {
const website = await findWebsite(websiteId);
if (!website) {
log('useSession: Website not found');
return badRequest(res);
}
if (process.env.ENABLE_COLLECT_LIMIT) {
if (isOverApiLimit(website)) {
return tooManyRequest(res, 'Collect currently exceeds monthly limit of 10000.');
}
}
session = await findSession(req, payload);
}
if (!session) {
2022-12-27 06:51:16 +01:00
log('useSession: Session not found');
2020-08-12 05:05:40 +02:00
return badRequest(res);
2020-07-28 08:52:14 +02:00
}
2022-12-31 22:42:03 +01:00
req.session = session;
2020-07-28 08:52:14 +02:00
next();
});
2022-02-27 00:53:45 +01:00
export const useAuth = createMiddleware(async (req, res, next) => {
const token = getAuthToken(req);
2022-11-12 20:33:14 +01:00
const payload = parseSecureToken(token, secret());
2022-10-31 19:02:37 +01:00
const shareToken = await parseShareToken(req);
2022-11-16 20:44:36 +01:00
let user = null;
2022-11-12 20:33:14 +01:00
const { userId, key } = payload || {};
2022-11-09 19:16:50 +01:00
2022-11-11 18:42:54 +01:00
if (validate(userId)) {
user = await getUser({ id: userId });
2022-11-16 20:44:36 +01:00
} else if (redis.enabled && key) {
2022-11-09 19:16:50 +01:00
user = await redis.get(key);
2022-11-09 15:40:36 +01:00
}
2022-11-11 18:42:54 +01:00
log({ token, payload, user, shareToken });
2022-11-09 15:40:36 +01:00
if (!user && !shareToken) {
2022-12-27 06:51:16 +01:00
log('useAuth: User not authorized');
2020-08-12 05:05:40 +02:00
return unauthorized(res);
2020-07-28 08:52:14 +02:00
}
2022-12-07 03:36:41 +01:00
if (user) {
user.isAdmin = user.role === ROLES.admin;
}
2022-12-28 05:20:44 +01:00
(req as any).auth = { user, token, shareToken, key };
2020-07-28 08:52:14 +02:00
next();
});