MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / validateSdkRequest

Function validateSdkRequest

apps/api/src/utils/auth.ts:42–176  ·  view source on GitHub ↗
(
  req: FastifyRequest<{
    Body: ITrackHandlerPayload | DeprecatedPostEventPayload;
  }>
)

Source from the content-addressed store, hash-verified

40}
41
42export async function validateSdkRequest(
43 req: FastifyRequest<{
44 Body: ITrackHandlerPayload | DeprecatedPostEventPayload;
45 }>
46): Promise<IServiceClientWithProject> {
47 const { headers, clientIp } = req;
48 const clientIdNew = headers['openpanel-client-id'] as string;
49 const clientIdOld = headers['mixan-client-id'] as string;
50 const clientSecretNew = headers['openpanel-client-secret'] as string;
51 const clientSecretOld = headers['mixan-client-secret'] as string;
52 const clientIdFromBody = path<string | undefined>(['clientId'], req.body);
53 const clientSecretFromBody = path<string | undefined>(
54 ['clientSecret'],
55 req.body
56 );
57 const clientId = clientIdNew || clientIdOld || clientIdFromBody;
58 const clientSecret =
59 clientSecretNew || clientSecretOld || clientSecretFromBody;
60 const origin = headers.origin;
61
62 if (clientSecret) {
63 req.clientSecretAuth = true;
64 }
65
66 const createError = (message: string) =>
67 new SdkAuthError(message, {
68 clientId,
69 clientSecret:
70 typeof clientSecret === 'string'
71 ? `${clientSecret.slice(0, 5)}...${clientSecret.slice(-5)}`
72 : 'none',
73 origin,
74 });
75
76 if (!clientId) {
77 throw createError('Ingestion: Missing client id');
78 }
79
80 if (
81 !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(
82 clientId
83 )
84 ) {
85 throw createError('Ingestion: Client ID must be a valid UUIDv4');
86 }
87
88 const client = await getClientByIdCached(clientId);
89
90 if (!client) {
91 throw createError('Ingestion: Invalid client id');
92 }
93
94 if (!client.project) {
95 throw createError('Ingestion: Client has no project');
96 }
97
98 // Filter out blocked IPs
99 const ipFilter = client.project.filters.filter(

Callers 1

clientHookFunction · 0.90

Calls 7

getCacheFunction · 0.90
verifyPasswordFunction · 0.90
createErrorFunction · 0.85
cleanDomainFunction · 0.85
someMethod · 0.45
toStringMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected