-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
39 lines (32 loc) · 1.06 KB
/
auth.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
const username = "${username}"; //process.env.username;
const password = "${password}"; //process.env.password;
const unauthResp = {
status: "401",
statusDescription: "Unauthorized",
headers: {
"www-authenticate": [
{
key: "WWW-Authenticate",
value: 'Basic realm="Login"'
}
]
}
};
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const authorization = headers["authorization"];
// no Authorization header
if (!authorization || authorization.length === 0) {
return callback(null, unauthResp);
}
const credentials = authorization[0]["value"];
const split = credentials.split(" ");
if (split.length === 2 && split[0].toLowerCase() === "basic") {
let decoded = new Buffer(split[1], "base64").toString("ascii");
if (decoded === username + ":" + password) {
return callback(null, request);
}
}
return callback(null, unauthResp);
};