Created
June 24, 2021 02:28
-
-
Save jrobinsonc/50342ec3b919914c46e6b87a7110ec70 to your computer and use it in GitHub Desktop.
BASIC Authentication - Simple authentication script intended to be run by Amazon Lambda to * provide Basic HTTP Authentication for a static website hosted in an * Amazon S3 bucket through Couldfront.
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
/** | |
* BASIC Authentication | |
* | |
* Simple authentication script intended to be run by Amazon Lambda to | |
* provide Basic HTTP Authentication for a static website hosted in an | |
* Amazon S3 bucket through Couldfront. | |
* | |
* https://hackernoon.com/serverless-password-protecting-a-static-website-in-an-aws-s3-bucket-bfaaa01b8666 | |
*/ | |
'use strict'; | |
exports.handler = (event, context, callback) => { | |
// Get request and request headers | |
const request = event.Records[0].cf.request; | |
const headers = request.headers; | |
// Configure authentication | |
const authUser = 'YOUR USERNAME'; | |
const authPass = 'YOUR PASWORD'; | |
// Construct the Basic Auth string | |
const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64'); | |
// Require Basic authentication | |
if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) { | |
const body = 'Unauthorized'; | |
const response = { | |
status: '401', | |
statusDescription: 'Unauthorized', | |
body: body, | |
headers: { | |
'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}] | |
}, | |
}; | |
callback(null, response); | |
} | |
// Continue request processing if authentication passed | |
callback(null, request); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment