umami/lib/auth.ts

93 lines
2.3 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';
2022-11-19 03:49:58 +01:00
import { SHARE_TOKEN_HEADER, UmamiApi } from 'lib/constants';
2022-10-12 06:48:33 +02:00
import { secret } from 'lib/crypto';
2022-11-19 03:49:58 +01:00
import { parseSecureToken, parseToken } from 'next-basics';
2022-12-01 03:40:44 +01:00
import { getPermissionsByUserId, getTeamPermissionsByUserId } from 'queries';
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;
}
2022-11-19 03:49:58 +01:00
export async function allowQuery(
2022-12-01 03:40:44 +01:00
requestUserId: string,
2022-11-19 03:49:58 +01:00
type: UmamiApi.AuthType,
typeId?: string,
2022-12-01 03:40:44 +01:00
permission?: UmamiApi.Permission,
2022-11-19 03:49:58 +01:00
) {
2022-12-01 03:40:44 +01:00
if (type === UmamiApi.AuthType.Website) {
const website = await cache.fetchWebsite(typeId);
2022-12-01 03:40:44 +01:00
if (website && website.userId === requestUserId) {
return true;
}
2022-12-01 03:40:44 +01:00
if (website.teamId) {
return checkTeamPermission(requestUserId, typeId, permission);
}
2022-12-01 03:40:44 +01:00
return false;
} else if (type === UmamiApi.AuthType.User) {
if (requestUserId !== typeId) {
return checkUserPermission(requestUserId, permission || UmamiApi.Permission.Admin);
2022-11-20 09:48:13 +01:00
}
2022-12-01 03:40:44 +01:00
return requestUserId === typeId;
} else if (type === UmamiApi.AuthType.Team) {
return checkTeamPermission(requestUserId, typeId, permission);
2022-11-19 03:49:58 +01:00
}
return false;
}
2022-11-20 09:48:13 +01:00
2022-12-01 03:40:44 +01:00
export async function checkUserPermission(userId: string, type: UmamiApi.Permission) {
2022-11-20 09:48:13 +01:00
const userRole = await getPermissionsByUserId(userId, type);
return userRole.length > 0;
}
2022-12-01 03:40:44 +01:00
export async function checkTeamPermission(userId, teamId: string, type: UmamiApi.Permission) {
const userRole = await getTeamPermissionsByUserId(userId, teamId, type);
return userRole.length > 0;
}