2022-10-31 18:02:37 +00:00
|
|
|
import debug from 'debug';
|
2022-11-19 02:49:58 +00:00
|
|
|
import { NextApiRequestAuth } from 'interface/api/nextApi';
|
2022-11-20 08:48:13 +00:00
|
|
|
import cache from 'lib/cache';
|
2022-11-19 02:49:58 +00:00
|
|
|
import { SHARE_TOKEN_HEADER, UmamiApi } from 'lib/constants';
|
2022-10-12 04:48:33 +00:00
|
|
|
import { secret } from 'lib/crypto';
|
2022-11-19 02:49:58 +00:00
|
|
|
import { parseSecureToken, parseToken } from 'next-basics';
|
2022-11-20 08:48:13 +00:00
|
|
|
import { getPermissionsByUserId, getTeamUser, getUser } from 'queries';
|
2020-07-28 06:52:14 +00:00
|
|
|
|
2022-10-31 18:02:37 +00:00
|
|
|
const log = debug('umami:auth');
|
|
|
|
|
|
2022-11-08 00:22:49 +00:00
|
|
|
export function getAuthToken(req) {
|
2022-11-11 17:42:54 +00:00
|
|
|
try {
|
|
|
|
|
return req.headers.authorization.split(' ')[1];
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-11-08 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-31 18:02:37 +00:00
|
|
|
export function parseAuthToken(req) {
|
2022-01-23 08:32:17 +00:00
|
|
|
try {
|
2022-11-08 00:22:49 +00:00
|
|
|
return parseSecureToken(getAuthToken(req), secret());
|
2022-10-31 18:02:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
log(e);
|
2022-01-23 08:32:17 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2020-08-05 05:45:05 +00:00
|
|
|
}
|
2020-09-18 05:52:20 +00:00
|
|
|
|
2022-10-31 18:02:37 +00:00
|
|
|
export function parseShareToken(req) {
|
2022-10-12 04:48:33 +00:00
|
|
|
try {
|
2022-10-25 02:48:10 +00:00
|
|
|
return parseToken(req.headers[SHARE_TOKEN_HEADER], secret());
|
2022-10-31 18:02:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
log(e);
|
2022-10-12 04:48:33 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-08-30 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-12 04:48:33 +00:00
|
|
|
export function isValidToken(token, validation) {
|
2020-09-18 05:52:20 +00:00
|
|
|
try {
|
|
|
|
|
if (typeof validation === 'object') {
|
2022-10-25 02:48:10 +00:00
|
|
|
return !Object.keys(validation).find(key => token[key] !== validation[key]);
|
2020-09-18 05:52:20 +00:00
|
|
|
} else if (typeof validation === 'function') {
|
2022-10-25 02:48:10 +00:00
|
|
|
return validation(token);
|
2020-09-18 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2022-10-31 18:02:37 +00:00
|
|
|
log(e);
|
2020-09-18 05:52:20 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 02:49:58 +00:00
|
|
|
export async function allowQuery(
|
|
|
|
|
req: NextApiRequestAuth,
|
|
|
|
|
type: UmamiApi.AuthType,
|
|
|
|
|
typeId?: string,
|
|
|
|
|
) {
|
|
|
|
|
const { id } = req.query as { id: string };
|
2020-09-18 05:52:20 +00:00
|
|
|
|
2022-11-16 19:44:36 +00:00
|
|
|
const { user, shareToken } = req.auth;
|
2020-09-18 05:52:20 +00:00
|
|
|
|
2022-10-12 20:11:44 +00:00
|
|
|
if (shareToken) {
|
2022-10-25 02:48:10 +00:00
|
|
|
return isValidToken(shareToken, { id });
|
2022-10-12 20:11:44 +00:00
|
|
|
}
|
2020-09-18 05:52:20 +00:00
|
|
|
|
2022-11-16 19:44:36 +00:00
|
|
|
if (user?.id) {
|
2022-11-19 02:49:58 +00:00
|
|
|
if (type === UmamiApi.AuthType.Website) {
|
2022-11-20 08:48:13 +00:00
|
|
|
const website = await cache.fetchWebsite(typeId ?? id);
|
|
|
|
|
|
|
|
|
|
if (website && website.userId === user.id) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (website.teamId) {
|
|
|
|
|
const teamUser = getTeamUser({ userId: user.id, teamId: website.teamId, isDeleted: false });
|
2020-09-18 05:52:20 +00:00
|
|
|
|
2022-11-20 08:48:13 +00:00
|
|
|
return teamUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2022-11-19 02:49:58 +00:00
|
|
|
} else if (type === UmamiApi.AuthType.User) {
|
2022-11-01 06:42:37 +00:00
|
|
|
const user = await getUser({ id });
|
2022-10-25 17:45:56 +00:00
|
|
|
|
2022-11-01 06:42:37 +00:00
|
|
|
return user && user.id === id;
|
2022-11-20 08:48:13 +00:00
|
|
|
} else if (type === UmamiApi.AuthType.Team) {
|
|
|
|
|
const teamUser = await getTeamUser({
|
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: typeId ?? id,
|
|
|
|
|
isDeleted: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return teamUser;
|
|
|
|
|
} else if (type === UmamiApi.AuthType.TeamOwner) {
|
|
|
|
|
const teamUser = await getTeamUser({
|
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: typeId ?? id,
|
|
|
|
|
isDeleted: false,
|
|
|
|
|
});
|
2020-09-18 05:52:20 +00:00
|
|
|
|
2022-11-20 08:48:13 +00:00
|
|
|
return teamUser && teamUser.isOwner;
|
|
|
|
|
}
|
2022-11-19 02:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-18 05:52:20 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-11-20 08:48:13 +00:00
|
|
|
|
|
|
|
|
export async function checkPermission(req: NextApiRequestAuth, type: UmamiApi.Permission) {
|
|
|
|
|
const {
|
|
|
|
|
user: { id: userId },
|
|
|
|
|
} = req.auth;
|
|
|
|
|
|
|
|
|
|
const userRole = await getPermissionsByUserId(userId, type);
|
|
|
|
|
|
|
|
|
|
return userRole.length > 0;
|
|
|
|
|
}
|