Created
June 8, 2018 18:33
-
-
Save duhaime/ded6856e652a0a2bad11a9ef8b9a8b4f to your computer and use it in GitHub Desktop.
Lambda auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.handler = (event, context, callback) => { | |
// Get the request and its headers | |
const request = event.Records[0].cf.request; | |
const headers = request.headers; | |
// Specify the username and password to be used | |
const user = 'top-secret-user'; | |
const pw = 'top-secret-password'; | |
// Build a Basic Authentication string | |
const authString = 'Basic ' + new Buffer(user + ':' + pw).toString('base64'); | |
// Challenge for auth if auth credentials are absent or incorrect | |
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); | |
} | |
// User has authenticated | |
callback(null, request); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment