在Edge使用Lambda将用户名信息传递给Webapp

时间:2018-06-05 17:01:03

标签: node.js aws-lambda amazon-cloudfront

我已经按照这些教程进行了操作:

https://hackernoon.com/serverless-password-protecting-a-static-website-in-an-aws-s3-bucket-bfaaa01b8666

https://github.com/dumrauf/serverless_static_website_with_basic_auth

哪个工作正常,并试图修改它以某种方式将用户名发送到webapp,所以我尝试将它们添加到请求查询字符串。 lambda在Lambda控制台中工作正常,但当我真正尝试访问cloudfront网址时,我得到了503页。

enter image description here

有没有更好的方法来完成我想要做的事情?除非我从Lambda控制台执行它,为什么lambda似乎没有出现在CloudWatch中?这个代码怎么样导致它给我一个503,当教程中的代码几乎没有什么不同时,可以正常工作?

这是我的代码:

'use strict';

const querystring = require('querystring');


exports.handler = (event, context, callback) => {


// Get request and request headers
let request = event.Records[0].cf.request;
let uri = request.uri
const headers = request.headers;
let credentials

credentials = {
    'username': 'password'
};

let auth_to_user_map = {}

// Placeholder list which eventually holds all credential strings
let credential_strings = [];

// Construct all credential strings and add to corresponding list
for (let user in credentials) {
    const password = credentials[user];
    const auth_string = 'Basic ' + new Buffer(user + ':' + password).toString('base64');
    credential_strings.push(auth_string);
    auth_to_user_map[auth_string] = user
}


// Validate Basic Authentication
if (typeof headers.authorization == 'undefined' || credential_strings.indexOf(headers.authorization[0].value) === -1) {
    const response = {
        status: '401',
        statusDescription: 'Unauthorized',
        body: 'Unauthorized!',
        headers: {
            'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}]
        },
    };
    callback(null, response);
}

let username = auth_to_user_map[headers.authorization[0].value]
request.querystring = querystring.stringify({username});

callback(null, request);
};

0 个答案:

没有答案