@valentine-efagene/qshelter-common 2.0.132 → 2.0.135
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,5 +2,7 @@ import { Request, Response, NextFunction } from 'express';
|
|
|
2
2
|
/**
|
|
3
3
|
* Request logging middleware that logs method, path, status code, and duration.
|
|
4
4
|
* Logs in JSON format for easy parsing by log aggregation tools.
|
|
5
|
+
*
|
|
6
|
+
* In debug mode, also logs the authorizer context from API Gateway.
|
|
5
7
|
*/
|
|
6
8
|
export declare function requestLogger(req: Request, res: Response, next: NextFunction): void;
|
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Request logging middleware that logs method, path, status code, and duration.
|
|
3
3
|
* Logs in JSON format for easy parsing by log aggregation tools.
|
|
4
|
+
*
|
|
5
|
+
* In debug mode, also logs the authorizer context from API Gateway.
|
|
4
6
|
*/
|
|
5
7
|
export function requestLogger(req, res, next) {
|
|
6
8
|
const start = Date.now();
|
|
9
|
+
// Debug: Log authorizer context structure
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
const lambdaReq = req;
|
|
12
|
+
const authorizer = lambdaReq.apiGateway?.event?.requestContext?.authorizer;
|
|
13
|
+
if (process.env.DEBUG_AUTH === 'true' || process.env.NODE_ENV !== 'production') {
|
|
14
|
+
console.log(JSON.stringify({
|
|
15
|
+
type: 'auth_debug',
|
|
16
|
+
path: req.path,
|
|
17
|
+
hasApiGateway: !!lambdaReq.apiGateway,
|
|
18
|
+
hasEvent: !!lambdaReq.apiGateway?.event,
|
|
19
|
+
hasRequestContext: !!lambdaReq.apiGateway?.event?.requestContext,
|
|
20
|
+
hasAuthorizer: !!authorizer,
|
|
21
|
+
authorizerKeys: authorizer ? Object.keys(authorizer) : [],
|
|
22
|
+
authorizer: authorizer,
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
7
25
|
res.on('finish', () => {
|
|
8
26
|
const duration = Date.now() - start;
|
|
9
27
|
console.log(JSON.stringify({
|
|
@@ -10,7 +10,13 @@ declare global {
|
|
|
10
10
|
tenantContext?: TenantContext;
|
|
11
11
|
tenantPrisma?: TenantPrismaClient | PrismaClient;
|
|
12
12
|
/**
|
|
13
|
-
* API Gateway context added by serverless-express
|
|
13
|
+
* API Gateway context added by serverless-express.
|
|
14
|
+
*
|
|
15
|
+
* With HTTP API v2 and enableSimpleResponses=true, context is under:
|
|
16
|
+
* authorizer.lambda.{field}
|
|
17
|
+
*
|
|
18
|
+
* With REST API or enableSimpleResponses=false, context is under:
|
|
19
|
+
* authorizer.{field}
|
|
14
20
|
*/
|
|
15
21
|
apiGateway?: {
|
|
16
22
|
event: {
|
|
@@ -20,6 +26,12 @@ declare global {
|
|
|
20
26
|
email?: string;
|
|
21
27
|
roles?: string;
|
|
22
28
|
tenantId?: string;
|
|
29
|
+
lambda?: {
|
|
30
|
+
userId?: string;
|
|
31
|
+
email?: string;
|
|
32
|
+
roles?: string;
|
|
33
|
+
tenantId?: string;
|
|
34
|
+
};
|
|
23
35
|
};
|
|
24
36
|
};
|
|
25
37
|
};
|
|
@@ -21,7 +21,10 @@ export function createTenantMiddleware(options) {
|
|
|
21
21
|
return function tenantMiddleware(req, res, next) {
|
|
22
22
|
try {
|
|
23
23
|
// 1. Try Lambda authorizer context first (production)
|
|
24
|
-
|
|
24
|
+
// HTTP API v2 with enableSimpleResponses=true nests context under authorizer.lambda
|
|
25
|
+
const authorizer = req.apiGateway?.event?.requestContext?.authorizer;
|
|
26
|
+
const lambdaContext = authorizer?.lambda;
|
|
27
|
+
const authorizerContext = lambdaContext || authorizer;
|
|
25
28
|
// 2. Fall back to x-authorizer-* headers (test/development)
|
|
26
29
|
const headers = req.headers;
|
|
27
30
|
const tenantId = authorizerContext?.tenantId || headers['x-authorizer-tenant-id'];
|
package/package.json
CHANGED