DEV Community

Discussion on: Basic Authentication with Lambda@Edge

Collapse
hshar7 profile image
Hayder Sharhan

With Cloudfront's new functions feature:

function handler (event) {
  var request = event.request;
  var headers = request.headers;
  var authString = "Basic BASE64_OF_USERNAME:PASSWORD";

  // If authorization header isn't present or doesn't match expected authString, deny the request
  if (
    typeof headers['authorization'] == 'undefined' ||
    headers['authorization'].value !== authString
  ) {
      return {
        statusCode: 401,
        statusDescription: 'Unauthorized',
        headers: {
            'www-authenticate': { value: 'Basic' }
        },
      };
  }

  return request;
};
Enter fullscreen mode Exit fullscreen mode

And then associate the function with the distribution

EEEEZZZ