SHARE
TWEET

Untitled

a guest Jun 11th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * BASIC Authentication
  3.  *
  4.  * Lambda@Edge snippet for basic auth with CloudFront & Amazon S3 (static website)
  5.  * This code use viewer request integration btw CloudFront 7 Lambda@Edge
  6.  *
  7.  */
  8.  
  9. 'use strict';
  10.  
  11. exports.handler = (event, context, callback) => {
  12.  
  13.     // Get request and request headers
  14.     const request = event.Records[0].cf.request;
  15.     const headers = request.headers;
  16.  
  17.     // Configure authentication
  18.     const authUser = 'YOUR_USERNAME';
  19.     const authPass = 'YOUR_PASSWORD';
  20.  
  21.     // Construct the Basic Auth string
  22.     const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');
  23.  
  24.     // Require Basic authentication
  25.     if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) {
  26.         const body = 'Unauthorized';
  27.         const response = {
  28.             status: '401',
  29.             statusDescription: 'Unauthorized',
  30.             body: body,
  31.             headers: {
  32.                 'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}]
  33.             },
  34.         };
  35.         callback(null, response);
  36.     }
  37.  
  38.     // Continue request processing if authentication passed
  39.     callback(null, request);
  40. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top