/
authFunctionNameExtractor.js
72 lines (55 loc) · 1.96 KB
/
authFunctionNameExtractor.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
import serverlessLog from '../../serverlessLog.js'
// FIXME "slessLog" param is only remaining for tests, should be removed
export default function extract(endpoint, slessLog) {
const buildFailureResult = (warningMessage) => {
const _serverlessLog = slessLog || serverlessLog // FIXME remove
_serverlessLog(warningMessage)
return { unsupportedAuth: true }
}
const buildSuccessResult = (authorizerName) => ({ authorizerName })
const handleStringAuthorizer = (authorizerString) => {
if (authorizerString.toUpperCase() === 'AWS_IAM') {
return buildFailureResult(
'WARNING: Serverless Offline does not support the AWS_IAM authorization type',
)
}
return buildSuccessResult(authorizerString)
}
const handleObjectAuthorizer = (authorizerObject) => {
const { arn, authorizerId, name, type } = authorizerObject
if (type && type.toUpperCase() === 'AWS_IAM') {
return buildFailureResult(
'WARNING: Serverless Offline does not support the AWS_IAM authorization type',
)
}
if (arn) {
return buildFailureResult(
`WARNING: Serverless Offline does not support non local authorizers (arn): ${arn}`,
)
}
if (authorizerId) {
return buildFailureResult(
`WARNING: Serverless Offline does not support non local authorizers (authorizerId): ${authorizerId}`,
)
}
if (!name) {
return buildFailureResult(
'WARNING: Serverless Offline supports local authorizers but authorizer name is missing',
)
}
return buildSuccessResult(name)
}
const { authorizer } = endpoint
if (!authorizer) {
return buildSuccessResult(null)
}
if (typeof authorizer === 'string') {
return handleStringAuthorizer(authorizer)
}
if (typeof authorizer === 'object') {
return handleObjectAuthorizer(authorizer)
}
return buildFailureResult(
'WARNING: Serverless Offline supports only local authorizers defined as string or object',
)
}