-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (26 loc) · 881 Bytes
/
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
// Function Type: Lambda@Edge Function
// Function Name: cloudfront-basic-auth
// CloudFront Event: Viewer Request
exports.handler = async (event) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const hostname = headers.host[0].value;
const credentials = require('./credentials.json');
const username = credentials[hostname].username;
const password = credentials[hostname].password;
const auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != auth) {
return {
status: '401',
statusDescription: 'Unauthorized',
body: 'Unauthorized',
headers: {
'www-authenticate': [{
key: 'WWW-Authenticate',
value: 'Basic'
}]
}
};
}
return request;
};