-
-
Notifications
You must be signed in to change notification settings - Fork 797
/
Copy pathLambdaProxyIntegrationEvent.js
233 lines (206 loc) · 6.83 KB
/
LambdaProxyIntegrationEvent.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { Buffer } from 'node:buffer'
import { env } from 'node:process'
import { log } from '@serverless/utils/log.js'
import { decode } from 'jsonwebtoken'
import {
createUniqueId,
formatToClfTime,
nullIfEmpty,
parseHeaders,
parseMultiValueHeaders,
parseMultiValueQueryStringParameters,
parseQueryStringParameters,
} from '../../../utils/index.js'
const { byteLength } = Buffer
const { parse } = JSON
const { assign } = Object
// https://serverless.com/framework/docs/providers/aws/events/apigateway/
// https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
// http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html
export default class LambdaProxyIntegrationEvent {
#additionalRequestContext = null
#path = null
#routeKey = null
#request = null
#stage = null
#stageVariables = null
constructor(
request,
stage,
path,
stageVariables,
routeKey,
additionalRequestContext,
) {
this.#additionalRequestContext = additionalRequestContext || {}
this.#path = path
this.#routeKey = routeKey
this.#request = request
this.#stage = stage
this.#stageVariables = stageVariables
}
create() {
const authPrincipalId =
this.#request.auth &&
this.#request.auth.credentials &&
this.#request.auth.credentials.principalId
const authContext =
(this.#request.auth &&
this.#request.auth.credentials &&
this.#request.auth.credentials.context) ||
{}
let authAuthorizer
if (env.AUTHORIZER) {
try {
authAuthorizer = parse(env.AUTHORIZER)
} catch {
log.error(
'Could not parse env.AUTHORIZER, make sure it is correct JSON',
)
}
}
let body = this.#request.payload
const { rawHeaders, url } = this.#request.raw.req
// NOTE FIXME request.raw.req.rawHeaders can only be null for testing (hapi shot inject())
const headers = parseHeaders(rawHeaders || []) || {}
if (headers['sls-offline-authorizer-override']) {
try {
authAuthorizer = parse(headers['sls-offline-authorizer-override'])
} catch {
log.error(
'Could not parse header sls-offline-authorizer-override, make sure it is correct JSON',
)
}
}
if (body) {
if (typeof body !== 'string') {
// this.#request.payload is NOT the same as the rawPayload
body = this.#request.rawPayload
}
if (
!headers['Content-Length'] &&
!headers['content-length'] &&
!headers['Content-length'] &&
(typeof body === 'string' ||
body instanceof Buffer ||
body instanceof ArrayBuffer)
) {
headers['Content-Length'] = String(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 = { ...this.#request.params }
let token = headers.Authorization || headers.authorization
if (token && token.split(' ')[0] === 'Bearer') {
;[, token] = token.split(' ')
}
let claims
let scopes
if (token) {
try {
claims = decode(token) || undefined
if (claims && claims.scope) {
scopes = claims.scope.split(' ')
// In AWS HTTP Api the scope property is removed from the decoded JWT
// I'm leaving this property because I'm not sure how all of the authorizers
// for AWS REST Api handle JWT.
// claims = { ...claims }
// delete claims.scope
}
} catch {
// Do nothing
}
}
const {
headers: _headers,
info: { received, remoteAddress },
method,
route,
} = this.#request
const httpMethod = method.toUpperCase()
const requestTime = formatToClfTime(received)
const requestTimeEpoch = received
const resource = this.#routeKey || route.path.replace(`/${this.#stage}`, '')
return {
body,
headers,
httpMethod,
isBase64Encoded: false, // TODO hook up
multiValueHeaders: parseMultiValueHeaders(
// NOTE FIXME request.raw.req.rawHeaders can only be null for testing (hapi shot inject())
rawHeaders || [],
),
multiValueQueryStringParameters:
parseMultiValueQueryStringParameters(url),
path: this.#path,
pathParameters: nullIfEmpty(pathParams),
queryStringParameters: parseQueryStringParameters(url),
requestContext: {
accountId: 'offlineContext_accountId',
apiId: 'offlineContext_apiId',
authorizer:
authAuthorizer ||
assign(authContext, {
claims,
// 'principalId' should have higher priority
principalId:
authPrincipalId ||
env.PRINCIPAL_ID ||
'offlineContext_authorizer_principalId', // See #24
scopes,
}),
domainName: 'offlineContext_domainName',
domainPrefix: 'offlineContext_domainPrefix',
extendedRequestId: createUniqueId(),
httpMethod,
identity: {
accessKey: null,
accountId: env.SLS_ACCOUNT_ID || 'offlineContext_accountId',
apiKey: env.SLS_API_KEY || 'offlineContext_apiKey',
apiKeyId: env.SLS_API_KEY_ID || 'offlineContext_apiKeyId',
caller: env.SLS_CALLER || 'offlineContext_caller',
cognitoAuthenticationProvider:
_headers['cognito-authentication-provider'] ||
env.SLS_COGNITO_AUTHENTICATION_PROVIDER ||
'offlineContext_cognitoAuthenticationProvider',
cognitoAuthenticationType:
env.SLS_COGNITO_AUTHENTICATION_TYPE ||
'offlineContext_cognitoAuthenticationType',
cognitoIdentityId:
_headers['cognito-identity-id'] ||
env.SLS_COGNITO_IDENTITY_ID ||
'offlineContext_cognitoIdentityId',
cognitoIdentityPoolId:
env.SLS_COGNITO_IDENTITY_POOL_ID ||
'offlineContext_cognitoIdentityPoolId',
principalOrgId: null,
sourceIp: remoteAddress,
user: 'offlineContext_user',
userAgent: _headers['user-agent'] || '',
userArn: 'offlineContext_userArn',
},
operationName: this.#additionalRequestContext.operationName,
path: this.#path,
protocol: 'HTTP/1.1',
requestId: createUniqueId(),
requestTime,
requestTimeEpoch,
resourceId: 'offlineContext_resourceId',
resourcePath: route.path,
stage: this.#stage,
},
resource,
stageVariables: this.#stageVariables,
}
}
}