2022-11-09 07:58:52 +01:00
|
|
|
import { createMiddleware, unauthorized, badRequest, parseSecureToken } from 'next-basics';
|
2022-11-08 07:35:51 +01:00
|
|
|
import debug from 'debug';
|
2020-07-18 19:36:46 +02:00
|
|
|
import cors from 'cors';
|
2022-11-09 19:15:21 +01:00
|
|
|
import { validate } from 'uuid';
|
2022-12-27 06:51:16 +01:00
|
|
|
import redis from '@umami/redis-client';
|
2022-11-08 07:35:51 +01:00
|
|
|
import { findSession } from 'lib/session';
|
2022-11-22 07:32:55 +01:00
|
|
|
import { getAuthToken, parseShareToken } from 'lib/auth';
|
2022-11-09 19:15:21 +01:00
|
|
|
import { secret } from 'lib/crypto';
|
2022-12-07 03:36:41 +01:00
|
|
|
import { ROLES } from 'lib/constants';
|
2022-11-09 15:40:36 +01:00
|
|
|
import { getUser } from '../queries';
|
2022-11-08 07:35:51 +01:00
|
|
|
|
|
|
|
const log = debug('umami:middleware');
|
2020-07-18 19:36:46 +02:00
|
|
|
|
2022-02-27 00:53:45 +01:00
|
|
|
export const useCors = createMiddleware(cors());
|
2020-07-28 08:52:14 +02:00
|
|
|
|
2022-02-27 00:53:45 +01:00
|
|
|
export const useSession = createMiddleware(async (req, res, next) => {
|
2022-11-08 07:35:51 +01:00
|
|
|
const session = await findSession(req);
|
2020-08-08 02:19:42 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-08-08 02:19:42 +02:00
|
|
|
|
2022-12-28 05:20:44 +01:00
|
|
|
(req as any).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) => {
|
2022-11-09 07:58:52 +01:00
|
|
|
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);
|
2020-08-08 02:19:42 +02:00
|
|
|
|
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
|
|
|
}
|
2020-08-08 02:19:42 +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();
|
|
|
|
});
|