-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
70 lines (65 loc) · 1.98 KB
/
index.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
'use strict';
const _generatePolicy = (principalId, effect, resource,customErrorMessage=null) => {
const authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
const policyDocument = {};
policyDocument.Version = '2012-10-17';
policyDocument.Statement = [];
policyDocument.Statement.push({
Action : 'execute-api:Invoke',
Effect: effect,
Resource:resource,
})
authResponse.policyDocument = policyDocument;
}
if(effect.toLowerCase()=='deny'&& customErrorMessage!=null){
authResponse.context = {
"customErrorMessage": customErrorMessage ,
};
}
return authResponse;
};
module.exports.private = async (event, context, callback) => {
const response = {
statusCode: 200,
headers: {
/* Required for CORS support to work */
'Access-Control-Allow-Origin': '*',
/* Required for cookies, authorization headers with HTTPS */
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({
message: 'Private API',
})
};
return response
};
module.exports.public = async (event, context, callback) => {
const response = {
statusCode: 200,
headers: {
/* Required for CORS support to work */
'Access-Control-Allow-Origin': '*',
/* Required for cookies, authorization headers with HTTPS */
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({
message: 'Public API',
})
};
return response
};
module.exports.auth = async (event) => {
var token = event.authorizationToken;
switch (token.toLowerCase()) {
case 'allow':
return _generatePolicy('user123', 'allow', event.methodArn)
case 'deny':
return _generatePolicy('user123', 'deny', event.methodArn, "Custom Deny Message");
case 'unauthorized':
throw new Error("Unauthorized"); // Return a 401 Unauthorized response
default:
throw new Error("Unauthorized");
}
};