api: add first version of export api
This commit is contained in:
committed by
Carl-Gerhard Lindesvärd
parent
7ea95afe16
commit
7f8d857508
@@ -1,7 +1,8 @@
|
||||
import type { RawRequestDefaultExpression } from 'fastify';
|
||||
|
||||
import { verifyPassword } from '@openpanel/common';
|
||||
import { db } from '@openpanel/db';
|
||||
import type { IServiceClient } from '@openpanel/db';
|
||||
import { ClientType, db } from '@openpanel/db';
|
||||
|
||||
import { logger } from './logger';
|
||||
|
||||
@@ -89,5 +90,39 @@ export async function validateSdkRequest(
|
||||
}
|
||||
}
|
||||
|
||||
if (!client.projectId) {
|
||||
throw new Error('No project id found for client');
|
||||
}
|
||||
|
||||
return client.projectId;
|
||||
}
|
||||
|
||||
export async function validateExportRequest(
|
||||
headers: RawRequestDefaultExpression['headers']
|
||||
): Promise<IServiceClient> {
|
||||
const clientId = headers['openpanel-client-id'] as string;
|
||||
const clientSecret = (headers['openpanel-client-secret'] as string) || '';
|
||||
const client = await db.client.findUnique({
|
||||
where: {
|
||||
id: clientId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!client) {
|
||||
throw new Error('Export: Invalid client id');
|
||||
}
|
||||
|
||||
if (!client.secret) {
|
||||
throw new Error('Export: Client has no secret');
|
||||
}
|
||||
|
||||
if (client.type === ClientType.write) {
|
||||
throw new Error('Export: Client is not allowed to export');
|
||||
}
|
||||
|
||||
if (!(await verifyPassword(clientSecret, client.secret))) {
|
||||
throw new Error('Export: Invalid client secret');
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user