Skip to content

Instantly share code, notes, and snippets.

@duhaime
Created June 8, 2018 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duhaime/ded6856e652a0a2bad11a9ef8b9a8b4f to your computer and use it in GitHub Desktop.
Save duhaime/ded6856e652a0a2bad11a9ef8b9a8b4f to your computer and use it in GitHub Desktop.
Lambda auth
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