Skip to content

Instantly share code, notes, and snippets.

@noelrocha
Last active January 31, 2018 13:22
Show Gist options
  • Save noelrocha/0cdd7a05989fb161725660a6f6ae1a22 to your computer and use it in GitHub Desktop.
Save noelrocha/0cdd7a05989fb161725660a6f6ae1a22 to your computer and use it in GitHub Desktop.
lambda-edge-basic-authentication
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const authUser = 'USERNAME';
const authPass = 'PASSWORD';
const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');
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);
}
callback(null, request);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment