mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-17 02:16:38 +00:00
Merge dev into fix/log-analytics-adjustments
This commit is contained in:
@@ -20,6 +20,6 @@ export const errorHandlerMiddleware: ErrorRequestHandler = (
|
||||
error: true,
|
||||
message: error.message || "Internal Server Error",
|
||||
status: statusCode,
|
||||
stack: process.env.ENVIRONMENT === "prod" ? null : error.stack,
|
||||
stack: process.env.ENVIRONMENT === "prod" ? null : error.stack
|
||||
});
|
||||
};
|
||||
|
||||
@@ -8,13 +8,13 @@ import HttpCode from "@server/types/HttpCode";
|
||||
export async function getUserOrgs(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
next: NextFunction
|
||||
) {
|
||||
const userId = req.user?.userId; // Assuming you have user information in the request
|
||||
|
||||
if (!userId) {
|
||||
return next(
|
||||
createHttpError(HttpCode.UNAUTHORIZED, "User not authenticated"),
|
||||
createHttpError(HttpCode.UNAUTHORIZED, "User not authenticated")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ export async function getUserOrgs(
|
||||
const userOrganizations = await db
|
||||
.select({
|
||||
orgId: userOrgs.orgId,
|
||||
roleId: userOrgs.roleId,
|
||||
roleId: userOrgs.roleId
|
||||
})
|
||||
.from(userOrgs)
|
||||
.where(eq(userOrgs.userId, userId));
|
||||
@@ -38,8 +38,8 @@ export async function getUserOrgs(
|
||||
next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
"Error retrieving user organizations",
|
||||
),
|
||||
"Error retrieving user organizations"
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,4 +12,4 @@ export * from "./verifyAccessTokenAccess";
|
||||
export * from "./verifyApiKeyIsRoot";
|
||||
export * from "./verifyApiKeyApiKeyAccess";
|
||||
export * from "./verifyApiKeyClientAccess";
|
||||
export * from "./verifyApiKeySiteResourceAccess";
|
||||
export * from "./verifyApiKeySiteResourceAccess";
|
||||
|
||||
@@ -97,7 +97,6 @@ export async function verifyApiKeyAccessTokenAccess(
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return next();
|
||||
} catch (e) {
|
||||
return next(
|
||||
|
||||
@@ -11,7 +11,7 @@ export async function verifyApiKeyApiKeyAccess(
|
||||
next: NextFunction
|
||||
) {
|
||||
try {
|
||||
const {apiKey: callerApiKey } = req;
|
||||
const { apiKey: callerApiKey } = req;
|
||||
|
||||
const apiKeyId =
|
||||
req.params.apiKeyId || req.body.apiKeyId || req.query.apiKeyId;
|
||||
@@ -44,7 +44,10 @@ export async function verifyApiKeyApiKeyAccess(
|
||||
.select()
|
||||
.from(apiKeyOrg)
|
||||
.where(
|
||||
and(eq(apiKeys.apiKeyId, callerApiKey.apiKeyId), eq(apiKeyOrg.orgId, orgId))
|
||||
and(
|
||||
eq(apiKeys.apiKeyId, callerApiKey.apiKeyId),
|
||||
eq(apiKeyOrg.orgId, orgId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
|
||||
@@ -11,9 +11,12 @@ export async function verifyApiKeySetResourceClients(
|
||||
next: NextFunction
|
||||
) {
|
||||
const apiKey = req.apiKey;
|
||||
const singleClientId = req.params.clientId || req.body.clientId || req.query.clientId;
|
||||
const singleClientId =
|
||||
req.params.clientId || req.body.clientId || req.query.clientId;
|
||||
const { clientIds } = req.body;
|
||||
const allClientIds = clientIds || (singleClientId ? [parseInt(singleClientId as string)] : []);
|
||||
const allClientIds =
|
||||
clientIds ||
|
||||
(singleClientId ? [parseInt(singleClientId as string)] : []);
|
||||
|
||||
if (!apiKey) {
|
||||
return next(
|
||||
@@ -70,4 +73,3 @@ export async function verifyApiKeySetResourceClients(
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,8 @@ export async function verifyApiKeySetResourceUsers(
|
||||
next: NextFunction
|
||||
) {
|
||||
const apiKey = req.apiKey;
|
||||
const singleUserId = req.params.userId || req.body.userId || req.query.userId;
|
||||
const singleUserId =
|
||||
req.params.userId || req.body.userId || req.query.userId;
|
||||
const { userIds } = req.body;
|
||||
const allUserIds = userIds || (singleUserId ? [singleUserId] : []);
|
||||
|
||||
|
||||
@@ -38,17 +38,12 @@ export async function verifyApiKeySiteResourceAccess(
|
||||
const [siteResource] = await db
|
||||
.select()
|
||||
.from(siteResources)
|
||||
.where(and(
|
||||
eq(siteResources.siteResourceId, siteResourceId)
|
||||
))
|
||||
.where(and(eq(siteResources.siteResourceId, siteResourceId)))
|
||||
.limit(1);
|
||||
|
||||
if (!siteResource) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.NOT_FOUND,
|
||||
"Site resource not found"
|
||||
)
|
||||
createHttpError(HttpCode.NOT_FOUND, "Site resource not found")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import HttpCode from "@server/types/HttpCode";
|
||||
export function notFoundMiddleware(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
next: NextFunction
|
||||
) {
|
||||
if (req.path.startsWith("/api")) {
|
||||
const message = `The requests url is not found - ${req.originalUrl}`;
|
||||
|
||||
@@ -1,30 +1,32 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import logger from '@server/logger';
|
||||
import createHttpError from 'http-errors';
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import logger from "@server/logger";
|
||||
import createHttpError from "http-errors";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
|
||||
export function requestTimeoutMiddleware(timeoutMs: number = 30000) {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
// Set a timeout for the request
|
||||
const timeout = setTimeout(() => {
|
||||
if (!res.headersSent) {
|
||||
logger.error(`Request timeout: ${req.method} ${req.url} from ${req.ip}`);
|
||||
logger.error(
|
||||
`Request timeout: ${req.method} ${req.url} from ${req.ip}`
|
||||
);
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.REQUEST_TIMEOUT,
|
||||
'Request timeout - operation took too long to complete'
|
||||
"Request timeout - operation took too long to complete"
|
||||
)
|
||||
);
|
||||
}
|
||||
}, timeoutMs);
|
||||
|
||||
// Clear timeout when response finishes
|
||||
res.on('finish', () => {
|
||||
res.on("finish", () => {
|
||||
clearTimeout(timeout);
|
||||
});
|
||||
|
||||
// Clear timeout when response closes
|
||||
res.on('close', () => {
|
||||
res.on("close", () => {
|
||||
clearTimeout(timeout);
|
||||
});
|
||||
|
||||
|
||||
@@ -76,7 +76,10 @@ export async function verifySiteAccess(
|
||||
.select()
|
||||
.from(userOrgs)
|
||||
.where(
|
||||
and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, site.orgId))
|
||||
and(
|
||||
eq(userOrgs.userId, userId),
|
||||
eq(userOrgs.orgId, site.orgId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
req.userOrg = userOrgRole[0];
|
||||
|
||||
Reference in New Issue
Block a user