/
VelocityContext.js
137 lines (120 loc) · 3.88 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
import { Buffer } from 'buffer'
import jsEscapeString from 'js-string-escape'
import { decode } from 'jsonwebtoken'
import {
createUniqueId,
isPlainObject,
jsonPath,
parseHeaders,
} from '../../../utils/index.js'
const { parse, stringify } = JSON
const { entries, fromEntries } = Object
function escapeJavaScript(x) {
if (typeof x === 'string') {
return jsEscapeString(x).replace(/\\n/g, '\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 {
constructor(request, stage, payload) {
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 ||
process.env.PRINCIPAL_ID ||
'offlineContext_authorizer_principalId' // See #24
if (token) {
try {
const claims = decode(token) || undefined
if (claims) {
Object.assign(authorizer, { claims })
}
} catch (err) {
// 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._request.route.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.replace(/\+/g, ' ')),
urlEncode: encodeURI,
},
}
}
}