Guest User

Untitled

a guest
Sep 4th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // Node.js Lambda functions receive the event, context and callback parameters
  4. exports.handler = (event, context, callback) => {
  5.  
  6. // Extract the headers from the incoming request
  7. const request = event.Records[0].cf.request;
  8. const headers = request.headers;
  9.  
  10. // Define the auth credentials
  11. const authUser = 'user';
  12. const authPass = 'pass';
  13.  
  14. // Construct the expected Basic Auth string for the credentials
  15. const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');
  16.  
  17. // If there is no auth header or its value doesn't match our expected string
  18. if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) {
  19. // Construct a 401 (unauthorized) response requiring Basic Auth
  20. const response = {
  21. status: '401',
  22. statusDescription: 'Unauthorized',
  23. body: 'Unauthorized',
  24. headers: { 'www-authenticate': [{ key: 'WWW-Authenticate', value: 'Basic' }] },
  25. };
  26.  
  27. // Return the response to the client - callback(Error error, Object result);
  28. callback(null, response);
  29. }
  30.  
  31. // Otherwise, continue processing the request
  32. callback(null, request);
  33. };
Add Comment
Please, Sign In to add comment