forked from dherault/serverless-offline
/
createLambdaProxyContext.js
110 lines (97 loc) · 4.04 KB
/
createLambdaProxyContext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const { decode } = require('jsonwebtoken');
const {
normalizeMultiValueQuery,
normalizeQuery,
nullIfEmpty,
randomId,
} = require('./utils');
/*
Mimicks the request context object
http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html
*/
module.exports = function createLambdaProxyContext(request, options, stageVariables) {
const authPrincipalId = request.auth && request.auth.credentials && request.auth.credentials.user;
const authContext = (request.auth && request.auth.credentials && request.auth.credentials.context) || {};
let authAuthorizer;
if (process.env.AUTHORIZER) {
try {
authAuthorizer = JSON.parse(process.env.AUTHORIZER);
}
catch (error) {
console.error('Serverless-offline: Could not parse process.env.AUTHORIZER, make sure it is correct JSON.');
}
}
let body = request.payload;
const headers = request.unprocessedHeaders;
if (body) {
if (typeof body !== 'string') {
// JSON.stringify(JSON.parse(request.payload)) is NOT the same as the rawPayload
body = request.rawPayload;
}
if (!headers['Content-Length'] && !headers['content-length'] && !headers['Content-length'] && (typeof body === 'string' || body instanceof Buffer || body instanceof ArrayBuffer)) {
headers['Content-Length'] = Buffer.byteLength(body);
}
// Set a default Content-Type if not provided.
if (!headers['Content-Type'] && !headers['content-type'] && !headers['Content-type']) {
headers['Content-Type'] = 'application/json';
}
}
else if (typeof body === 'undefined' || body === '') {
body = null;
}
// clone own props
const pathParams = { ...request.params };
let token = headers.Authorization || headers.authorization;
if (token && token.split(' ')[0] === 'Bearer') {
token = token.split(' ')[1];
}
let claims;
if (token) {
try {
claims = decode(token) || undefined;
}
catch (err) {
// Do nothing
}
}
return {
headers,
multiValueHeaders: request.multiValueHeaders,
path: request.path,
pathParameters: nullIfEmpty(pathParams),
requestContext: {
accountId: 'offlineContext_accountId',
resourceId: 'offlineContext_resourceId',
apiId: 'offlineContext_apiId',
stage: options.stage,
requestId: `offlineContext_requestId_${randomId()}`,
identity: {
cognitoIdentityPoolId: process.env.SLS_COGNITO_IDENTITY_POOL_ID || 'offlineContext_cognitoIdentityPoolId',
accountId: process.env.SLS_ACCOUNT_ID || 'offlineContext_accountId',
cognitoIdentityId: request.headers['cognito-identity-id'] || process.env.SLS_COGNITO_IDENTITY_ID || 'offlineContext_cognitoIdentityId',
caller: process.env.SLS_CALLER || 'offlineContext_caller',
apiKey: process.env.SLS_API_KEY || 'offlineContext_apiKey',
sourceIp: request.info.remoteAddress,
cognitoAuthenticationType: process.env.SLS_COGNITO_AUTHENTICATION_TYPE || 'offlineContext_cognitoAuthenticationType',
cognitoAuthenticationProvider: request.headers['cognito-authentication-provider'] || process.env.SLS_COGNITO_AUTHENTICATION_PROVIDER || 'offlineContext_cognitoAuthenticationProvider',
userArn: 'offlineContext_userArn',
userAgent: request.headers['user-agent'] || '',
user: 'offlineContext_user',
},
authorizer: authAuthorizer || Object.assign(authContext, { // 'principalId' should have higher priority
principalId: authPrincipalId || process.env.PRINCIPAL_ID || 'offlineContext_authorizer_principalId', // See #24
claims,
}),
protocol: 'HTTP/1.1',
resourcePath: request.route.path,
httpMethod: request.method.toUpperCase(),
requestTimeEpoch: request.info.received,
},
resource: request.route.path,
httpMethod: request.method.toUpperCase(),
queryStringParameters: nullIfEmpty(normalizeQuery(request.query)),
multiValueQueryStringParameters: nullIfEmpty(normalizeMultiValueQuery(request.query)),
stageVariables: nullIfEmpty(stageVariables),
body,
};
};