Skip to content

Instantly share code, notes, and snippets.

@DevoKun
Created October 29, 2018 01:55
Show Gist options
  • Select an option

  • Save DevoKun/2f128023f5355c22a6b9eb9739049d98 to your computer and use it in GitHub Desktop.

Select an option

Save DevoKun/2f128023f5355c22a6b9eb9739049d98 to your computer and use it in GitHub Desktop.
Amazon Lambda HTTP Basic Auth using ClaudiaJS
var ApiBuilder = require('claudia-api-builder'),
api = module.exports = new ApiBuilder(),
renderPage = function (body) {
'use strict';
return body;
};
const authUser = 'user',
authPass = 'pass',
authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');
api.get('/', function(req) {
'use strict';
var headers = req.headers;
if ( ('Authorization' in headers) && (headers['Authorization'] === authString) ) {
return {"status":200, "statusDescription":"Authorized"};
} else {
throw new ApiBuilder.ApiResponse({"status":401, "statusDescription":"Unauthorized"}, {'Content-Type':'application/json'}, 401);
} // if
}, { success: { code:200, headers:{'X-Version':'0.0.1','WWW-Authenticate':'Basic'}, contentType: 'application/json'},
error: { code:500, headers:{'X-Version':'0.0.1','WWW-Authenticate':'Basic'}, contentType: 'application/json'},
apiKeyRequired: false
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment