-
-
Notifications
You must be signed in to change notification settings - Fork 794
/
VelocityContext.js
144 lines (123 loc) · 3.93 KB
/
VelocityContext.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
import { Buffer } from 'node:buffer'
import { env } from 'node:process'
import jsEscapeString from 'js-string-escape'
import { decodeJwt } from 'jose'
import {
createUniqueId,
isPlainObject,
jsonPath,
parseHeaders,
} from '../../../utils/index.js'
const { parse, stringify } = JSON
const { assign, entries, fromEntries } = Object
function escapeJavaScript(x) {
if (typeof x === 'string') {
return jsEscapeString(x).replaceAll('\\n', '\n') // See #26,
}
if (isPlainObject(x)) {
const result = fromEntries(
entries(x).map(([key, value]) => [key, jsEscapeString(value)]),
)
return stringify(result) // Is this really how APIG does it?
}
if (typeof x.toString === 'function') {
return escapeJavaScript(x.toString())
}
return x
}
/*
Returns a context object that mocks APIG mapping template reference
http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
*/
export default class VelocityContext {
#path = null
#payload = null
#request = null
#stage = null
constructor(request, stage, payload, path) {
this.#path = path
this.#payload = payload
this.#request = request
this.#stage = stage
}
getContext() {
const path = (x) => jsonPath(this.#payload, x)
const authPrincipalId =
this.#request.auth &&
this.#request.auth.credentials &&
this.#request.auth.credentials.principalId
let authorizer =
this.#request.auth &&
this.#request.auth.credentials &&
this.#request.auth.credentials.authorizer
// NOTE FIXME request.raw.req.rawHeaders can only be null for testing (hapi shot inject())
const headers = parseHeaders(this.#request.raw.req.rawHeaders || [])
let token = headers && (headers.Authorization || headers.authorization)
if (token && token.split(' ')[0] === 'Bearer') {
;[, token] = token.split(' ')
}
if (!authorizer) authorizer = {}
authorizer.principalId =
authPrincipalId ||
env.PRINCIPAL_ID ||
'offlineContext_authorizer_principalId' // See #24
if (token) {
try {
assign(authorizer, { claims: decodeJwt(token) })
} catch {
// Nothing
}
}
return {
context: {
apiId: 'offlineContext_apiId',
authorizer,
httpMethod: this.#request.method.toUpperCase(),
identity: {
accountId: 'offlineContext_accountId',
apiKey: 'offlineContext_apiKey',
apiKeyId: 'offlineContext_apiKeyId',
caller: 'offlineContext_caller',
cognitoAuthenticationProvider:
'offlineContext_cognitoAuthenticationProvider',
cognitoAuthenticationType: 'offlineContext_cognitoAuthenticationType',
sourceIp: this.#request.info.remoteAddress,
user: 'offlineContext_user',
userAgent: this.#request.headers['user-agent'] || '',
userArn: 'offlineContext_userArn',
},
requestId: createUniqueId(),
resourceId: 'offlineContext_resourceId',
resourcePath: this.#path,
stage: this.#stage,
},
input: {
body: this.#payload, // Not a string yet, todo
json: (x) => stringify(path(x)),
params: (x) =>
typeof x === 'string'
? this.#request.params[x] || this.#request.query[x] || headers[x]
: {
header: headers,
path: {
...this.#request.params,
},
querystring: {
...this.#request.query,
},
},
path,
},
util: {
base64Decode: (x) =>
Buffer.from(x.toString(), 'base64').toString('binary'),
base64Encode: (x) =>
Buffer.from(x.toString(), 'binary').toString('base64'),
escapeJavaScript,
parseJson: parse,
urlDecode: (x) => decodeURIComponent(x.replaceAll('+', ' ')),
urlEncode: encodeURI,
},
}
}
}