umami/lib/auth.ts

193 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-10-31 19:02:37 +01:00
import debug from 'debug';
2022-11-20 09:48:13 +01:00
import cache from 'lib/cache';
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
2022-10-12 06:48:33 +02:00
import { secret } from 'lib/crypto';
import { ensureArray, parseSecureToken, parseToken } from 'next-basics';
2022-12-07 03:36:41 +01:00
import { getTeamUser } from 'queries';
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');
export function getAuthToken(req) {
2022-11-11 18:42:54 +01:00
try {
return req.headers.authorization.split(' ')[1];
} catch {
return null;
}
}
2022-10-31 19:02:37 +01:00
export function parseAuthToken(req) {
try {
return parseSecureToken(getAuthToken(req), secret());
2022-10-31 19:02:37 +01:00
} catch (e) {
log(e);
return null;
}
2020-08-05 07:45:05 +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-10-12 06:48:33 +02:00
export function isValidToken(token, validation) {
try {
if (typeof validation === 'object') {
2022-10-25 04:48:10 +02:00
return !Object.keys(validation).find(key => token[key] !== validation[key]);
} else if (typeof validation === 'function') {
2022-10-25 04:48:10 +02:00
return validation(token);
}
} catch (e) {
2022-10-31 19:02:37 +01:00
log(e);
return false;
}
return false;
}
export async function canViewWebsite({ user }: Auth, websiteId: string) {
if (user.isAdmin) {
return true;
}
const website = await cache.fetchWebsite(websiteId);
if (website.userId) {
return user.id === website.userId;
2022-10-12 22:11:44 +02:00
}
if (website.teamId) {
return getTeamUser(website.teamId, user.id);
2022-11-19 03:49:58 +01:00
}
2022-12-07 03:36:41 +01:00
return false;
}
2022-11-20 09:48:13 +01:00
export async function canCreateWebsite({ user }: Auth, teamId?: string) {
if (user.isAdmin) {
return true;
}
if (teamId) {
const teamUser = await getTeamUser(teamId, user.id);
return hasPermission(teamUser.role, PERMISSIONS.websiteCreate);
}
return hasPermission(user.role, PERMISSIONS.websiteCreate);
}
export async function canUpdateWebsite({ user }: Auth, websiteId: string) {
if (user.isAdmin) {
return true;
}
const website = await cache.fetchWebsite(websiteId);
2022-11-20 09:48:13 +01:00
if (website.userId) {
return user.id === website.userId;
}
2022-11-20 09:48:13 +01:00
if (website.teamId) {
const teamUser = await getTeamUser(website.teamId, user.id);
2022-12-07 03:36:41 +01:00
return hasPermission(teamUser.role, PERMISSIONS.websiteUpdate);
}
2022-12-07 03:36:41 +01:00
return false;
2022-11-20 09:48:13 +01:00
}
export async function canDeleteWebsite({ user }: Auth, websiteId: string) {
if (user.isAdmin) {
return true;
}
const website = await cache.fetchWebsite(websiteId);
if (website.userId) {
return user.id === website.userId;
}
if (website.teamId) {
const teamUser = await getTeamUser(website.teamId, user.id);
2022-12-07 03:36:41 +01:00
return hasPermission(teamUser.role, PERMISSIONS.websiteDelete);
}
2022-12-07 03:36:41 +01:00
return false;
}
// To-do: Implement when payments are setup.
export async function canCreateTeam({ user }: Auth) {
if (user.isAdmin) {
return true;
}
return !!user;
}
// To-do: Implement when payments are setup.
export async function canViewTeam({ user }: Auth, teamId: string) {
if (user.isAdmin) {
return true;
}
return getTeamUser(teamId, user.id);
}
export async function canUpdateTeam({ user }: Auth, teamId: string) {
if (user.isAdmin) {
return true;
}
const teamUser = await getTeamUser(teamId, user.id);
2022-12-07 03:36:41 +01:00
return hasPermission(teamUser.role, PERMISSIONS.teamUpdate);
}
export async function canDeleteTeam({ user }: Auth, teamId: string) {
if (user.isAdmin) {
return true;
}
const teamUser = await getTeamUser(teamId, user.id);
2022-12-07 03:36:41 +01:00
return hasPermission(teamUser.role, PERMISSIONS.teamDelete);
}
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;
}
export async function canDeleteUser({ user }: Auth) {
return user.isAdmin;
}
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));
}