Tighten API access checks by website section and share permissions

This commit is contained in:
Mike Cao committed 2026-06-02 15:29:23 -07:00
1 parent 8bc49a29e3
commit ade0819be1
69 files changed
+303 -188

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
import { ENTITY_TYPE } from '@/lib/constants';
import type { Auth } from '@/lib/types';
import { canViewWebsite } from './website';
export type ShareSection =
| 'overview'
| 'events'
| 'sessions'
| 'realtime'
| 'performance'
| 'compare'
| 'breakdown'
| 'goals'
| 'funnels'
| 'journeys'
| 'retention'
| 'utm'
| 'revenue'
| 'attribution';
type ShareSectionInput = ShareSection | ShareSection[];
function shareTokenIncludesWebsite(auth: Auth | null | undefined, websiteId: string) {
const { shareToken } = auth || {};
return shareToken?.websiteId === websiteId || shareToken?.websiteIds?.includes(websiteId);
}
export async function canViewWebsiteSection(
auth: Auth | null | undefined,
websiteId: string,
section: ShareSectionInput,
) {
if (auth?.user) {
return canViewWebsite(auth, websiteId);
}
const { shareToken } = auth || {};
if (
!shareToken ||
shareToken.shareType !== ENTITY_TYPE.website ||
!shareTokenIncludesWebsite(auth, websiteId)
) {
return false;
}
const sections = Array.isArray(section) ? section : [section];
return sections.some(key => shareToken.parameters?.[key] === true);
}
export async function canViewSharedWebsite(auth: Auth | null | undefined, websiteId: string) {
if (auth?.user) {
return canViewWebsite(auth, websiteId);
}
const { shareToken } = auth || {};
return (
shareToken?.shareType === ENTITY_TYPE.website && shareTokenIncludesWebsite(auth, websiteId)
);
}
export async function canViewAuthenticatedWebsite(
auth: Auth | null | undefined,
websiteId: string,
) {
if (!auth?.user) {
return false;
}
return canViewWebsite(auth, websiteId);
}