mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-01 18:20:35 +02:00
114 lines
3.2 KiB
TypeScript
114 lines
3.2 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import { aiModels, aiProviders, apiKeyOrg, db } from "@server/db";
|
|
import { and, eq } from "drizzle-orm";
|
|
import createHttpError from "http-errors";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import { getFirstString } from "@server/lib/requestParams";
|
|
|
|
export async function verifyApiKeyAiModelAccess(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
) {
|
|
try {
|
|
const apiKey = req.apiKey;
|
|
const modelIdRaw = getFirstString(req.params.modelId);
|
|
const modelId = Number.parseInt(modelIdRaw ?? "", 10);
|
|
const providerIdRaw = getFirstString(req.params.providerId);
|
|
const providerId = Number.parseInt(providerIdRaw ?? "", 10);
|
|
const orgId = getFirstString(req.params.orgId);
|
|
|
|
if (!apiKey) {
|
|
return next(
|
|
createHttpError(HttpCode.UNAUTHORIZED, "Key not authenticated")
|
|
);
|
|
}
|
|
|
|
if (!orgId) {
|
|
return next(
|
|
createHttpError(HttpCode.BAD_REQUEST, "Invalid organization ID")
|
|
);
|
|
}
|
|
|
|
if (Number.isNaN(providerId)) {
|
|
return next(
|
|
createHttpError(HttpCode.BAD_REQUEST, "Invalid provider ID")
|
|
);
|
|
}
|
|
|
|
if (Number.isNaN(modelId)) {
|
|
return next(
|
|
createHttpError(HttpCode.BAD_REQUEST, "Invalid model ID")
|
|
);
|
|
}
|
|
|
|
const [row] = await db
|
|
.select({
|
|
model: aiModels,
|
|
provider: aiProviders
|
|
})
|
|
.from(aiModels)
|
|
.innerJoin(
|
|
aiProviders,
|
|
eq(aiModels.providerId, aiProviders.providerId)
|
|
)
|
|
.where(
|
|
and(
|
|
eq(aiModels.modelId, modelId),
|
|
eq(aiModels.providerId, providerId),
|
|
eq(aiProviders.orgId, orgId)
|
|
)
|
|
)
|
|
.limit(1);
|
|
|
|
if (!row) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.NOT_FOUND,
|
|
`AI model with ID ${modelId} not found`
|
|
)
|
|
);
|
|
}
|
|
|
|
if (apiKey.isRoot) {
|
|
req.aiProvider = row.provider;
|
|
req.aiModel = row.model;
|
|
return next();
|
|
}
|
|
|
|
if (!req.apiKeyOrg) {
|
|
const apiKeyOrgRes = await db
|
|
.select()
|
|
.from(apiKeyOrg)
|
|
.where(
|
|
and(
|
|
eq(apiKeyOrg.apiKeyId, apiKey.apiKeyId),
|
|
eq(apiKeyOrg.orgId, orgId)
|
|
)
|
|
)
|
|
.limit(1);
|
|
req.apiKeyOrg = apiKeyOrgRes[0];
|
|
}
|
|
|
|
if (!req.apiKeyOrg) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.FORBIDDEN,
|
|
"Key does not have access to this organization"
|
|
)
|
|
);
|
|
}
|
|
|
|
req.aiProvider = row.provider;
|
|
req.aiModel = row.model;
|
|
return next();
|
|
} catch (error) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
"Error verifying AI model access"
|
|
)
|
|
);
|
|
}
|
|
}
|