-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.js
48 lines (41 loc) · 1.27 KB
/
handler.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
'use strict';
exports.main = (event, context, callback) => {
const cf = event.Records[0].cf;
const request = cf.request;
const hostname = request.headers.host[0].value;
const redirectEnabled = '[REDIRECT_ENABLED]' === 'true';
const basicAuthEnabled = '[BASIC_AUTH_ENABLED]' === 'true';
if (redirectEnabled && hostname === '[REDIRECT_FROM]') {
const response = {
status: '302',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: '[REDIRECT_TO]',
}],
},
};
callback(null, response);
return;
}
if (basicAuthEnabled) {
const authUser = '[BASIC_AUTH_USERNAME]';
const authPass = '[BASIC_AUTH_PASSWORD]';
const request = event.Records[0].cf.request;
const headers = request.headers;
const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');
if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) {
callback(null, {
status: '401',
statusDescription: 'Unauthorized',
body: 'Unauthorized',
headers: {
'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}],
}
});
return;
}
}
callback(null, request);
};