mirror of
https://github.com/fosrl/pangolin.git
synced 2025-06-21 04:45:41 +02:00
68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
|
import { Request, Response, NextFunction } from "express";
|
||
|
import { db } from "@server/db";
|
||
|
import { userOrgs } from "@server/db/schemas";
|
||
|
import { and, eq } from "drizzle-orm";
|
||
|
import createHttpError from "http-errors";
|
||
|
import HttpCode from "@server/types/HttpCode";
|
||
|
|
||
|
export async function verifyApiKeyUserAccess(
|
||
|
req: Request,
|
||
|
res: Response,
|
||
|
next: NextFunction
|
||
|
) {
|
||
|
try {
|
||
|
const apiKey = req.apiKey;
|
||
|
const reqUserId =
|
||
|
req.params.userId || req.body.userId || req.query.userId;
|
||
|
|
||
|
if (!apiKey) {
|
||
|
return next(
|
||
|
createHttpError(HttpCode.UNAUTHORIZED, "Key not authenticated")
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (!reqUserId) {
|
||
|
return next(
|
||
|
createHttpError(HttpCode.BAD_REQUEST, "Invalid user ID")
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (!req.apiKeyOrg || !req.apiKeyOrg.orgId) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.FORBIDDEN,
|
||
|
"Key does not have organization access"
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
const orgId = req.apiKeyOrg.orgId;
|
||
|
|
||
|
const [userOrgRecord] = await db
|
||
|
.select()
|
||
|
.from(userOrgs)
|
||
|
.where(
|
||
|
and(eq(userOrgs.userId, reqUserId), eq(userOrgs.orgId, orgId))
|
||
|
)
|
||
|
.limit(1);
|
||
|
|
||
|
if (!userOrgRecord) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.FORBIDDEN,
|
||
|
"Key does not have access to this user"
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return next();
|
||
|
} catch (error) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||
|
"Error checking if key has access to this user"
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
}
|