2023-05-29 06:37:34 +02:00
|
|
|
import { Report } from '@prisma/client';
|
2023-04-05 08:29:54 +02:00
|
|
|
import redis from '@umami/redis-client';
|
2023-05-18 22:21:35 +02:00
|
|
|
import debug from 'debug';
|
2022-12-28 00:18:58 +01:00
|
|
|
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
|
2022-10-12 06:48:33 +02:00
|
|
|
import { secret } from 'lib/crypto';
|
2023-04-05 08:29:54 +02:00
|
|
|
import {
|
|
|
|
createSecureToken,
|
|
|
|
ensureArray,
|
|
|
|
getRandomChars,
|
|
|
|
parseSecureToken,
|
|
|
|
parseToken,
|
|
|
|
} from 'next-basics';
|
2023-05-18 22:21:35 +02:00
|
|
|
import { getTeamUser } from 'queries';
|
2023-03-09 21:42:12 +01:00
|
|
|
import { getTeamWebsite, getTeamWebsiteByTeamMemberId } from 'queries/admin/teamWebsite';
|
|
|
|
import { validate } from 'uuid';
|
2023-04-02 02:38:35 +02:00
|
|
|
import { loadWebsite } from './query';
|
2023-05-18 22:21:35 +02:00
|
|
|
import { Auth } from './types';
|
2020-07-28 08:52:14 +02:00
|
|
|
|
2022-10-31 19:02:37 +01:00
|
|
|
const log = debug('umami:auth');
|
|
|
|
|
2023-04-05 08:29:54 +02:00
|
|
|
export async function setAuthKey(user, expire = 0) {
|
|
|
|
const authKey = `auth:${getRandomChars(32)}`;
|
|
|
|
|
|
|
|
await redis.set(authKey, user);
|
|
|
|
|
|
|
|
if (expire) {
|
|
|
|
await redis.expire(authKey, expire);
|
|
|
|
}
|
|
|
|
|
|
|
|
return createSecureToken({ authKey }, secret());
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:22:49 +01:00
|
|
|
export function getAuthToken(req) {
|
2022-11-11 18:42:54 +01:00
|
|
|
try {
|
|
|
|
return req.headers.authorization.split(' ')[1];
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-08 01:22:49 +01:00
|
|
|
}
|
|
|
|
|
2022-10-31 19:02:37 +01:00
|
|
|
export function parseAuthToken(req) {
|
2022-01-23 09:32:17 +01:00
|
|
|
try {
|
2022-11-08 01:22:49 +01:00
|
|
|
return parseSecureToken(getAuthToken(req), secret());
|
2022-10-31 19:02:37 +01:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2022-01-23 09:32:17 +01:00
|
|
|
return null;
|
|
|
|
}
|
2020-08-05 07:45:05 +02:00
|
|
|
}
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-10-31 19:02:37 +01:00
|
|
|
export function parseShareToken(req) {
|
2022-10-12 06:48:33 +02:00
|
|
|
try {
|
2022-10-25 04:48:10 +02:00
|
|
|
return parseToken(req.headers[SHARE_TOKEN_HEADER], secret());
|
2022-10-31 19:02:37 +01:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2022-10-12 06:48:33 +02:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-30 05:57:34 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 06:48:33 +02:00
|
|
|
export function isValidToken(token, validation) {
|
2020-09-18 07:52:20 +02:00
|
|
|
try {
|
|
|
|
if (typeof validation === 'object') {
|
2022-10-25 04:48:10 +02:00
|
|
|
return !Object.keys(validation).find(key => token[key] !== validation[key]);
|
2020-09-18 07:52:20 +02:00
|
|
|
} else if (typeof validation === 'function') {
|
2022-10-25 04:48:10 +02:00
|
|
|
return validation(token);
|
2020-09-18 07:52:20 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-10-31 19:02:37 +01:00
|
|
|
log(e);
|
2020-09-18 07:52:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-03 21:37:26 +01:00
|
|
|
export async function canViewWebsite({ user, shareToken }: Auth, websiteId: string) {
|
|
|
|
if (user?.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shareToken?.websiteId === websiteId) {
|
2022-12-28 00:18:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-09 21:42:12 +01:00
|
|
|
const teamWebsite = await getTeamWebsiteByTeamMemberId(websiteId, user.id);
|
|
|
|
|
|
|
|
if (teamWebsite) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-12-02 05:53:37 +01:00
|
|
|
if (website.userId) {
|
2022-12-28 00:18:58 +01:00
|
|
|
return user.id === website.userId;
|
2022-10-12 22:11:44 +02:00
|
|
|
}
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
return false;
|
2020-09-18 07:52:20 +02:00
|
|
|
}
|
2022-11-20 09:48:13 +01:00
|
|
|
|
2023-04-14 05:57:22 +02:00
|
|
|
export async function canCreateWebsite({ user }: Auth) {
|
2022-12-28 00:18:58 +01:00
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasPermission(user.role, PERMISSIONS.websiteCreate);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function canUpdateWebsite({ user }: Auth, websiteId: string) {
|
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:59:38 +01:00
|
|
|
if (!validate(websiteId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2022-11-20 09:48:13 +01:00
|
|
|
|
2022-12-02 05:53:37 +01:00
|
|
|
if (website.userId) {
|
2022-12-28 00:18:58 +01:00
|
|
|
return user.id === website.userId;
|
2022-12-02 05:53:37 +01:00
|
|
|
}
|
2022-11-20 09:48:13 +01:00
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
return false;
|
2022-11-20 09:48:13 +01:00
|
|
|
}
|
2022-12-01 19:58:50 +01:00
|
|
|
|
2022-12-28 00:18:58 +01:00
|
|
|
export async function canDeleteWebsite({ user }: Auth, websiteId: string) {
|
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2022-12-01 19:58:50 +01:00
|
|
|
|
|
|
|
if (website.userId) {
|
2022-12-28 00:18:58 +01:00
|
|
|
return user.id === website.userId;
|
2022-12-01 19:58:50 +01:00
|
|
|
}
|
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
return false;
|
2022-12-01 19:58:50 +01:00
|
|
|
}
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
export async function canViewReport(auth: Auth, report: Report) {
|
2023-05-18 22:13:18 +02:00
|
|
|
if (auth.user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
if ((auth.user.id = report.userId)) {
|
2023-05-18 22:13:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
if (await canViewWebsite(auth, report.websiteId)) {
|
2023-05-18 22:13:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
export async function canUpdateReport(auth: Auth, report: Report) {
|
2023-05-18 22:13:18 +02:00
|
|
|
if (auth.user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
if ((auth.user.id = report.userId)) {
|
2023-05-18 22:13:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-28 00:18:58 +01:00
|
|
|
export async function canCreateTeam({ user }: Auth) {
|
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!user;
|
2022-12-02 05:53:37 +01:00
|
|
|
}
|
2022-12-01 19:58:50 +01:00
|
|
|
|
2022-12-28 00:18:58 +01:00
|
|
|
export async function canViewTeam({ user }: Auth, teamId: string) {
|
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTeamUser(teamId, user.id);
|
2022-12-02 05:53:37 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 00:18:58 +01:00
|
|
|
export async function canUpdateTeam({ user }: Auth, teamId: string) {
|
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:59:38 +01:00
|
|
|
if (validate(teamId)) {
|
|
|
|
const teamUser = await getTeamUser(teamId, user.id);
|
2022-12-02 05:53:37 +01:00
|
|
|
|
2023-02-02 20:59:38 +01:00
|
|
|
return hasPermission(teamUser.role, PERMISSIONS.teamUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2022-12-02 05:53:37 +01:00
|
|
|
}
|
2022-12-01 19:58:50 +01:00
|
|
|
|
2022-12-28 00:18:58 +01:00
|
|
|
export async function canDeleteTeam({ user }: Auth, teamId: string) {
|
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:59:38 +01:00
|
|
|
if (validate(teamId)) {
|
|
|
|
const teamUser = await getTeamUser(teamId, user.id);
|
|
|
|
|
|
|
|
return hasPermission(teamUser.role, PERMISSIONS.teamDelete);
|
|
|
|
}
|
2022-12-02 05:53:37 +01:00
|
|
|
|
2023-02-02 20:59:38 +01:00
|
|
|
return false;
|
2022-12-02 05:53:37 +01:00
|
|
|
}
|
|
|
|
|
2023-04-08 07:45:46 +02:00
|
|
|
export async function canDeleteTeamUser({ user }: Auth, teamId: string, removeUserId: string) {
|
2023-03-10 08:21:19 +01:00
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-08 07:45:46 +02:00
|
|
|
if (validate(teamId) && validate(removeUserId)) {
|
|
|
|
if (removeUserId === user.id) {
|
2023-03-30 01:02:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-08 07:45:46 +02:00
|
|
|
const teamUser = await getTeamUser(teamId, user.id);
|
2023-03-10 08:21:19 +01:00
|
|
|
|
|
|
|
return hasPermission(teamUser.role, PERMISSIONS.teamUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-10 01:04:28 +02:00
|
|
|
export async function canDeleteTeamWebsite({ user }: Auth, teamId: string, websiteId: string) {
|
2023-03-09 21:42:12 +01:00
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-10 01:04:28 +02:00
|
|
|
if (validate(teamId) && validate(websiteId)) {
|
|
|
|
const teamWebsite = await getTeamWebsite(teamId, websiteId);
|
2023-03-09 21:42:12 +01:00
|
|
|
|
|
|
|
if (teamWebsite.website.userId === user.id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const teamUser = await getTeamUser(teamWebsite.teamId, user.id);
|
|
|
|
|
2023-04-10 01:04:28 +02:00
|
|
|
return hasPermission(teamUser.role, PERMISSIONS.teamUpdate);
|
2023-03-09 21:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-28 00:18:58 +01:00
|
|
|
export async function canCreateUser({ user }: Auth) {
|
|
|
|
return user.isAdmin;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function canViewUser({ user }: Auth, viewedUserId: string) {
|
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.id === viewedUserId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function canViewUsers({ user }: Auth) {
|
|
|
|
return user.isAdmin;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function canUpdateUser({ user }: Auth, viewedUserId: string) {
|
|
|
|
if (user.isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.id === viewedUserId;
|
2022-12-02 05:53:37 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 00:18:58 +01:00
|
|
|
export async function canDeleteUser({ user }: Auth) {
|
|
|
|
return user.isAdmin;
|
2022-12-02 05:53:37 +01:00
|
|
|
}
|
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
export async function hasPermission(role: string, permission: string | string[]) {
|
|
|
|
return ensureArray(permission).some(e => ROLE_PERMISSIONS[role]?.includes(e));
|
2022-12-01 19:58:50 +01:00
|
|
|
}
|