(
req: FastifyRequest<{
Body: ITrackHandlerPayload | DeprecatedPostEventPayload;
}>
)
| 40 | } |
| 41 | |
| 42 | export 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( |
no test coverage detected