/
authValidateContext.js
50 lines (38 loc) · 1.31 KB
/
authValidateContext.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
import Boom from '@hapi/boom'
import serverlessLog from '../serverlessLog.js'
const { keys, values } = Object
function internalServerError(message) {
const errorType = 'AuthorizerConfigurationException'
const error = Boom.internal()
error.output.payload.message = message
error.output.payload.error = errorType
error.output.headers['x-amzn-ErrorType'] = errorType
return error
}
function isValidContext(context) {
return values(context).every(
(i) =>
typeof i === 'string' || typeof i === 'boolean' || typeof i === 'number',
)
}
function transform(context) {
keys(context).forEach((i) => {
context[i] = context[i].toString()
})
return context
}
export default function authValidateContext(context, authFunName) {
if (typeof context !== 'object') {
return internalServerError('Authorizer response context must be an object')
}
if (!isValidContext(context)) {
const error =
'Authorizer response context values must be of type string, number, or boolean'
serverlessLog(
`Detected invalid value types returned in authorizer context: (λ: ${authFunName}). ${error}. ` +
'More info: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html',
)
return internalServerError(error)
}
return transform(context)
}