Created
October 29, 2018 01:55
-
-
Save DevoKun/2f128023f5355c22a6b9eb9739049d98 to your computer and use it in GitHub Desktop.
Amazon Lambda HTTP Basic Auth using ClaudiaJS
This file contains hidden or 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
| 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