2022-11-08 01:22:49 +01:00
|
|
|
import { getRandomChars, parseSecureToken, parseToken } from 'next-basics';
|
2022-11-01 07:42:37 +01:00
|
|
|
import { getUser, getWebsite } from 'queries';
|
2022-10-31 19:02:37 +01:00
|
|
|
import debug from 'debug';
|
2022-11-01 17:56:43 +01:00
|
|
|
import { SHARE_TOKEN_HEADER, TYPE_USER, TYPE_WEBSITE } from 'lib/constants';
|
2022-10-12 06:48:33 +02:00
|
|
|
import { secret } from 'lib/crypto';
|
2020-07-28 08:52:14 +02:00
|
|
|
|
2022-10-31 19:02:37 +01:00
|
|
|
const log = debug('umami:auth');
|
|
|
|
|
2022-11-08 01:22:49 +01:00
|
|
|
export function generateAuthToken() {
|
2022-11-08 21:28:45 +01:00
|
|
|
return `auth:${getRandomChars(32)}`;
|
2022-11-08 01:22:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getAuthToken(req) {
|
|
|
|
const token = req.headers.authorization;
|
|
|
|
|
|
|
|
return token.split(' ')[1];
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-25 19:45:56 +02:00
|
|
|
export async function allowQuery(req, type) {
|
2022-10-25 04:48:10 +02:00
|
|
|
const { id } = req.query;
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-11-08 20:55:02 +01:00
|
|
|
const { userId, isAdmin, shareToken } = req.auth ?? {};
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (shareToken) {
|
2022-10-25 04:48:10 +02:00
|
|
|
return isValidToken(shareToken, { id });
|
2022-10-12 22:11:44 +02:00
|
|
|
}
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (userId) {
|
2022-10-25 19:45:56 +02:00
|
|
|
if (type === TYPE_WEBSITE) {
|
2022-11-01 07:42:37 +01:00
|
|
|
const website = await getWebsite({ id });
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-10-25 19:45:56 +02:00
|
|
|
return website && website.userId === userId;
|
2022-11-01 17:56:43 +01:00
|
|
|
} else if (type === TYPE_USER) {
|
2022-11-01 07:42:37 +01:00
|
|
|
const user = await getUser({ id });
|
2022-10-25 19:45:56 +02:00
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
return user && user.id === id;
|
2022-10-25 19:45:56 +02:00
|
|
|
}
|
2020-09-18 07:52:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|