forked from builtinnya/aws-lambda-edge-basic-auth-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic-auth.js
37 lines (31 loc) · 865 Bytes
/
basic-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
'use strict'
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request
const headers = request.headers
const authUser = '${user}'
const authPass = '${password}'
const encodedCredentials = new Buffer(`${authUser}:${authPass}`).toString('base64')
const authString = `Basic ${encodedCredentials}`
if (
typeof headers.authorization == 'undefined' ||
headers.authorization[0].value != authString
) {
const response = {
status: '401',
statusDescription: 'Unauthorized',
body: 'Unauthorized',
headers: {
'www-authenticate': [
{
key: 'WWW-Authenticate',
value: 'Basic',
}
]
},
}
callback(null, response)
return
}
// Continue request processing if authentication passed
callback(null, request)
}