Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- // Node.js Lambda functions receive the event, context and callback parameters
- exports.handler = (event, context, callback) => {
- // Extract the headers from the incoming request
- const request = event.Records[0].cf.request;
- const headers = request.headers;
- // Define the auth credentials
- const authUser = 'user';
- const authPass = 'pass';
- // Construct the expected Basic Auth string for the credentials
- const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');
- // If there is no auth header or its value doesn't match our expected string
- if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) {
- // Construct a 401 (unauthorized) response requiring Basic Auth
- const response = {
- status: '401',
- statusDescription: 'Unauthorized',
- body: 'Unauthorized',
- headers: { 'www-authenticate': [{ key: 'WWW-Authenticate', value: 'Basic' }] },
- };
- // Return the response to the client - callback(Error error, Object result);
- callback(null, response);
- }
- // Otherwise, continue processing the request
- callback(null, request);
- };
Add Comment
Please, Sign In to add comment