Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 1.50 KB | None | 0 0
  1. 'use strict';
  2. exports.handler = (event, context, callback) => {
  3.  
  4.   // Get request and request headers
  5.   const request = event.Records[0].cf.request;
  6.   const headers = request.headers;
  7.  
  8.   // Configure authentication
  9.   const authUser = 'huchauser';
  10.   const authPass = 'czn6jk;9';
  11.  
  12.   // Construct the Basic Auth string
  13.   const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');
  14.   const isImageRequest = /\.(ico|png\svg)$/.test(request.uri);
  15.  
  16.   // Require Basic authentication
  17.   if (!isImageRequest && (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString)) {
  18.       const body = 'Unauthorized';
  19.       const response = {
  20.           status: '401',
  21.           statusDescription: 'Unauthorized',
  22.           body: body,
  23.           headers: {
  24.               'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}],
  25.               'strict-transport-security': [{key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubdomains; preload'}],
  26.               'x-content-type-options': [{key: 'X-Content-Type-Options', value: 'nosniff'}],
  27.               'x-frame-options': [{key: 'X-Frame-Options', value: 'DENY'}],
  28.               'x-xss-protection': [{key: 'X-XSS-Protection', value: '1; mode=block'}],
  29.               'referrer-policy': [{key: 'Referrer-Policy', value: 'same-origin'}],
  30.           },
  31.       };
  32.       callback(null, response);
  33.   }
  34.  
  35.   // Continue request processing if authentication passed
  36.   callback(null, request);
  37. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement