mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Refactor API authentication.
This commit is contained in:
parent
c33729e185
commit
5a4fc96ebc
@ -15,13 +15,13 @@ const initialValues = {
|
|||||||
password: '',
|
password: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
const validate = ({ userId, username, password }) => {
|
const validate = ({ id, username, password }) => {
|
||||||
const errors = {};
|
const errors = {};
|
||||||
|
|
||||||
if (!username) {
|
if (!username) {
|
||||||
errors.username = <FormattedMessage id="label.required" defaultMessage="Required" />;
|
errors.username = <FormattedMessage id="label.required" defaultMessage="Required" />;
|
||||||
}
|
}
|
||||||
if (!userId && !password) {
|
if (!id && !password) {
|
||||||
errors.password = <FormattedMessage id="label.required" defaultMessage="Required" />;
|
errors.password = <FormattedMessage id="label.required" defaultMessage="Required" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,8 +33,8 @@ export default function AccountEditForm({ values, onSave, onClose }) {
|
|||||||
const [message, setMessage] = useState();
|
const [message, setMessage] = useState();
|
||||||
|
|
||||||
const handleSubmit = async values => {
|
const handleSubmit = async values => {
|
||||||
const { userId } = values;
|
const { id } = values;
|
||||||
const { ok, data } = await post(userId ? `/accounts/${userId}` : '/accounts', values);
|
const { ok, data } = await post(id ? `/accounts/${id}` : '/accounts', values);
|
||||||
|
|
||||||
if (ok) {
|
if (ok) {
|
||||||
onSave();
|
onSave();
|
||||||
|
@ -94,7 +94,7 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {
|
|||||||
return (
|
return (
|
||||||
<FormLayout>
|
<FormLayout>
|
||||||
<Formik
|
<Formik
|
||||||
initialValues={{ ...initialValues, ...values, enable_share_url: !!values?.shareId }}
|
initialValues={{ ...initialValues, ...values, enableShareUrl: !!values?.shareId }}
|
||||||
validate={validate}
|
validate={validate}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
>
|
>
|
||||||
@ -128,7 +128,7 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {
|
|||||||
<OwnerDropDown accounts={accounts} user={user} />
|
<OwnerDropDown accounts={accounts} user={user} />
|
||||||
<FormRow>
|
<FormRow>
|
||||||
<label />
|
<label />
|
||||||
<Field name="enable_share_url">
|
<Field name="enableShareUrl">
|
||||||
{({ field }) => (
|
{({ field }) => (
|
||||||
<Checkbox
|
<Checkbox
|
||||||
{...field}
|
{...field}
|
||||||
|
26
lib/auth.js
26
lib/auth.js
@ -1,4 +1,3 @@
|
|||||||
import { validate } from 'uuid';
|
|
||||||
import { parseSecureToken, parseToken } from 'next-basics';
|
import { parseSecureToken, parseToken } from 'next-basics';
|
||||||
import { getWebsite } from 'queries';
|
import { getWebsite } from 'queries';
|
||||||
import { SHARE_TOKEN_HEADER } from 'lib/constants';
|
import { SHARE_TOKEN_HEADER } from 'lib/constants';
|
||||||
@ -38,24 +37,23 @@ export function isValidToken(token, validation) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function allowQuery(req, skipToken) {
|
export async function allowQuery(req) {
|
||||||
const { id } = req.query;
|
const { id: websiteId } = req.query;
|
||||||
const token = req.headers[SHARE_TOKEN_HEADER];
|
|
||||||
|
|
||||||
const website = await getWebsite(validate(id) ? { websiteUuid: id } : { id: +id });
|
const { userId, isAdmin, shareToken } = req.auth ?? {};
|
||||||
|
|
||||||
if (website) {
|
if (isAdmin) {
|
||||||
if (token && token !== 'undefined' && !skipToken) {
|
return true;
|
||||||
return isValidToken(token, { websiteId: website.id });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const authToken = await getAuthToken(req);
|
if (shareToken) {
|
||||||
|
return isValidToken(shareToken, { websiteUuid: websiteId });
|
||||||
if (authToken) {
|
|
||||||
const { userId, isAdmin } = authToken;
|
|
||||||
|
|
||||||
return isAdmin || website.userId === userId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (userId) {
|
||||||
|
const website = await getWebsite({ websiteUuid: websiteId });
|
||||||
|
|
||||||
|
return website && website.userId === userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -1,32 +1,31 @@
|
|||||||
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||||
import { getAccountById, deleteAccount, getAccountByUsername, updateAccount } from 'queries';
|
import { getAccount, deleteAccount, updateAccount } from 'queries';
|
||||||
import { useAuth } from 'lib/middleware';
|
import { useAuth } from 'lib/middleware';
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
await useAuth(req, res);
|
await useAuth(req, res);
|
||||||
|
|
||||||
const { is_admin: currentUserIsAdmin, user_id: currentUserId } = req.auth;
|
const { isAdmin, userId } = req.auth;
|
||||||
const { id } = req.query;
|
const { id } = req.query;
|
||||||
const userId = +id;
|
|
||||||
|
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
if (userId !== currentUserId && !currentUserIsAdmin) {
|
if (id !== userId && !isAdmin) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const account = await getAccountById(userId);
|
const account = await getAccount({ id: +id });
|
||||||
|
|
||||||
return ok(res, account);
|
return ok(res, account);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
const { username, password, is_admin } = req.body;
|
const { username, password } = req.body;
|
||||||
|
|
||||||
if (userId !== currentUserId && !currentUserIsAdmin) {
|
if (id !== userId && !isAdmin) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const account = await getAccountById(userId);
|
const account = await getAccount({ id: +id });
|
||||||
|
|
||||||
const data = {};
|
const data = {};
|
||||||
|
|
||||||
@ -35,27 +34,26 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Only admin can change these fields
|
// Only admin can change these fields
|
||||||
if (currentUserIsAdmin) {
|
if (isAdmin) {
|
||||||
data.username = username;
|
data.username = username;
|
||||||
data.isAdmin = is_admin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check when username changes
|
// Check when username changes
|
||||||
if (data.username && account.username !== data.username) {
|
if (data.username && account.username !== data.username) {
|
||||||
const accountByUsername = await getAccountByUsername(username);
|
const accountByUsername = await getAccount({ username });
|
||||||
|
|
||||||
if (accountByUsername) {
|
if (accountByUsername) {
|
||||||
return badRequest(res, 'Account already exists');
|
return badRequest(res, 'Account already exists');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const updated = await updateAccount(userId, data);
|
const updated = await updateAccount(data, { id: +id });
|
||||||
|
|
||||||
return ok(res, updated);
|
return ok(res, updated);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'DELETE') {
|
if (req.method === 'DELETE') {
|
||||||
if (!currentUserIsAdmin) {
|
if (!isAdmin) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||||
import { allowQuery } from 'lib/auth';
|
import { allowQuery } from 'lib/auth';
|
||||||
import { useCors } from 'lib/middleware';
|
import { useAuth, useCors } from 'lib/middleware';
|
||||||
import { getActiveVisitors } from 'queries';
|
import { getActiveVisitors } from 'queries';
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
if (req.method === 'GET') {
|
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
|
if (req.method === 'GET') {
|
||||||
if (!(await allowQuery(req))) {
|
if (!(await allowQuery(req))) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,15 @@ import moment from 'moment-timezone';
|
|||||||
import { getEventMetrics } from 'queries';
|
import { getEventMetrics } from 'queries';
|
||||||
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
|
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
|
||||||
import { allowQuery } from 'lib/auth';
|
import { allowQuery } from 'lib/auth';
|
||||||
import { useCors } from 'lib/middleware';
|
import { useAuth, useCors } from 'lib/middleware';
|
||||||
|
|
||||||
const unitTypes = ['year', 'month', 'hour', 'day'];
|
const unitTypes = ['year', 'month', 'hour', 'day'];
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
if (req.method === 'GET') {
|
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
|
if (req.method === 'GET') {
|
||||||
if (!(await allowQuery(req))) {
|
if (!(await allowQuery(req))) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
@ -1,47 +1,42 @@
|
|||||||
import { allowQuery } from 'lib/auth';
|
import { allowQuery } from 'lib/auth';
|
||||||
import { useAuth, useCors } from 'lib/middleware';
|
import { useAuth, useCors } from 'lib/middleware';
|
||||||
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||||
import { deleteWebsite, getAccount, getWebsite, getWebsiteByUuid, updateWebsite } from 'queries';
|
import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { id: websiteId } = req.query;
|
|
||||||
|
|
||||||
if (req.method === 'GET') {
|
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
|
const { id: websiteId } = req.query;
|
||||||
|
|
||||||
if (!(await allowQuery(req))) {
|
if (!(await allowQuery(req))) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const website = await getWebsiteByUuid(websiteId);
|
if (req.method === 'GET') {
|
||||||
|
const website = await getWebsite({ websiteUuid: websiteId });
|
||||||
|
|
||||||
return ok(res, website);
|
return ok(res, website);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
await useAuth(req, res);
|
const { name, domain, owner, enableShareUrl, shareId } = req.body;
|
||||||
|
const { accountUuid } = req.auth;
|
||||||
const { isAdmin: currentUserIsAdmin, userId: currentUserId, accountUuid } = req.auth;
|
|
||||||
const { name, domain, owner, enable_share_url } = req.body;
|
|
||||||
let account;
|
let account;
|
||||||
|
|
||||||
if (accountUuid) {
|
if (accountUuid) {
|
||||||
account = await getAccount({ accountUuid });
|
account = await getAccount({ accountUuid });
|
||||||
}
|
}
|
||||||
|
|
||||||
const website = await getWebsite(websiteId);
|
const website = await getWebsite({ websiteUuid: websiteId });
|
||||||
|
|
||||||
const shareId = enable_share_url ? website.shareId || getRandomChars(8) : null;
|
const newShareId = enableShareUrl ? website.shareId || getRandomChars(8) : null;
|
||||||
|
|
||||||
if (website.userId !== currentUserId && !currentUserIsAdmin) {
|
|
||||||
return unauthorized(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
await updateWebsite(
|
await updateWebsite(
|
||||||
{
|
{
|
||||||
name,
|
name,
|
||||||
domain,
|
domain,
|
||||||
shareId: shareId,
|
shareId: shareId ? shareId : newShareId,
|
||||||
userId: account ? account.id : +owner,
|
userId: account ? account.id : +owner,
|
||||||
},
|
},
|
||||||
{ websiteUuid: websiteId },
|
{ websiteUuid: websiteId },
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { allowQuery } from 'lib/auth';
|
import { allowQuery } from 'lib/auth';
|
||||||
import { FILTER_IGNORED } from 'lib/constants';
|
import { FILTER_IGNORED } from 'lib/constants';
|
||||||
import { useCors } from 'lib/middleware';
|
import { useAuth, useCors } from 'lib/middleware';
|
||||||
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||||
import { getPageviewMetrics, getSessionMetrics, getWebsiteByUuid } from 'queries';
|
import { getPageviewMetrics, getSessionMetrics, getWebsiteByUuid } from 'queries';
|
||||||
|
|
||||||
@ -34,9 +34,10 @@ function getColumn(type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
if (req.method === 'GET') {
|
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
|
if (req.method === 'GET') {
|
||||||
if (!(await allowQuery(req))) {
|
if (!(await allowQuery(req))) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,15 @@ import moment from 'moment-timezone';
|
|||||||
import { getPageviewStats } from 'queries';
|
import { getPageviewStats } from 'queries';
|
||||||
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
|
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
|
||||||
import { allowQuery } from 'lib/auth';
|
import { allowQuery } from 'lib/auth';
|
||||||
import { useCors } from 'lib/middleware';
|
import { useAuth, useCors } from 'lib/middleware';
|
||||||
|
|
||||||
const unitTypes = ['year', 'month', 'hour', 'day'];
|
const unitTypes = ['year', 'month', 'hour', 'day'];
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
if (req.method === 'GET') {
|
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
|
if (req.method === 'GET') {
|
||||||
if (!(await allowQuery(req))) {
|
if (!(await allowQuery(req))) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import { resetWebsite } from 'queries';
|
import { resetWebsite } from 'queries';
|
||||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||||
import { allowQuery } from 'lib/auth';
|
import { allowQuery } from 'lib/auth';
|
||||||
|
import { useAuth, useCors } from 'lib/middleware';
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
|
await useCors(req, res);
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
const { id: websiteId } = req.query;
|
const { id: websiteId } = req.query;
|
||||||
|
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
import { getWebsiteStats } from 'queries';
|
import { getWebsiteStats } from 'queries';
|
||||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||||
import { allowQuery } from 'lib/auth';
|
import { allowQuery } from 'lib/auth';
|
||||||
import { useCors } from 'lib/middleware';
|
import { useAuth, useCors } from 'lib/middleware';
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
if (req.method === 'GET') {
|
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
|
if (req.method === 'GET') {
|
||||||
if (!(await allowQuery(req))) {
|
if (!(await allowQuery(req))) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
const { name, domain, owner, enable_share_url } = req.body;
|
const { name, domain, owner, enableShareUrl } = req.body;
|
||||||
|
|
||||||
const website_owner = account ? account.id : +owner;
|
const website_owner = account ? account.id : +owner;
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const websiteUuid = uuid();
|
const websiteUuid = uuid();
|
||||||
const shareId = enable_share_url ? getRandomChars(8) : null;
|
const shareId = enableShareUrl ? getRandomChars(8) : null;
|
||||||
const website = await createWebsite(website_owner, { websiteUuid, name, domain, shareId });
|
const website = await createWebsite(website_owner, { websiteUuid, name, domain, shareId });
|
||||||
|
|
||||||
return ok(res, website);
|
return ok(res, website);
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
|
|
||||||
export async function updateAccount(userId, data) {
|
export async function updateAccount(data, where) {
|
||||||
return prisma.client.account.update({
|
return prisma.client.account.update({
|
||||||
where: {
|
where,
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user