保证未在https电话

时间:2017-04-24 16:35:41

标签: node.js lambda https resolve

我正在编写API网关后面的AWS Lambda函数,该函数使用Facebook和我们的应用程序ID验证令牌,以便我们确认用户的身份,以便他可以删除他的资源。用户从Facebook登录中发送他的用户ID和令牌,我检查令牌是否有效以及是否是同一用户。在实现中我使用promises但问题是第一个然后根本没有执行。我首先调用getFacebookAccessToken,以便我可以调用API并在响应中解析或拒绝,但代码没有达到这一点。这是功能:

'use strict';

const https = require('https');
const querystring = require('querystring');

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

	if (typeof event.body.token === 'undefined' || event.body.token === '') {

		context.succeed({
			"result":"false",
			"message": `InputError: token is not defined or it is empty`
		});
		return;
	}
	if (typeof event.body.userId === 'undefined' || event.body.userId === '') {

		context.succeed({
			"result":"false",
			"message": `InputError: userId is not defined or it is empty`
		});
		return;
	}

	getFacebookAccessToken()
		.then((appAccessToken) => {

			return checkFacebookId(appAccessToken, event.body.token, event.body.userId);
		})
		.then((result) => {

			console.log('Done');
			context.succeed(result);
		})
		.catch((error) => {

			context.succeed(error);
		});
};

let checkFacebookId = (appAccessToken, token, facebookId) => {

	return new Promise((resolve, reject) => {

		let params = {
			input_token: token,
			access_token: appAccessToken
		};

		let requestParams = querystring.stringify(params);

		let configOptions = {
			method: "get",
			hostname: "graph.facebook.com",
			path: "/debug_token?" + requestParams
		};

		let request = https.request(configOptions, function (result) {

			let response = "";
			result.setEncoding('utf8');
			result.on('data', function (chunk) {

				response += chunk;
			});

			result.on('end', function () {

				const fbResponse = JSON.parse(response);

				if (result.statusCode.toString() === '200') {

					console.log(`Validating facebook token: OK`);

					if (typeof fbResponse.data === 'undefined') {
						reject({
							"result": "false",
							"message": "Token not valid"
						});
					}
					if (fbResponse.data.user_id === facebookId) {
						resolve({
							"result": "true"
						});
					} else {
						reject({
							"result": "false",
							"message": "Token not valid"
						});
					}
				}
				else {

					reject({
						"result": "false",
						"error": {
							"Message": `Failed validating facebook user access token`,
							"statusCode": result.statusCode,
							"token_valid": fbResponse
						}
					});
				}
			});
		});

		request.on('error', function (error) {
			reject({
				"result": "false",
				"message": "Error in Facebook",
				"error": error
			});
		});

		console.log(`Validating facebook token: PENDING`);
		request.end();
	});
};

let getFacebookAccessToken = () => {

	return new Promise((resolve, reject) => {

		let params = {
			client_id: event.stage.fb_app_id,
			client_secret: event.stage.fb_app_secret,
			grant_type: "client_credentials"
		};

		let requestParams = querystring.stringify(params);

		let configOptions = {
			method: "get",
			hostname: "graph.facebook.com",
			path: "/oauth/access_token?" + requestParams
		};
		let request = https.request(configOptions, function (result) {

			let responseData = "";
			result.setEncoding('utf8');
			result.on('data', function (chunk) {

				responseData += chunk;
			});

			result.on('end', function () {
				console.log(result);
				if (result.statusCode.toString() === '200') {

					console.log(`Getting Facebook Access Token: OK`);
					resolve(JSON.parse(responseData).access_token);
				}
				else {

					reject({
						"result": "false",
						"error": {
							"Message": `Failed getting facebook access token`,
							"statusCode": result.statusCode
						}
					});
				}
			});
		});

		request.on('error', function (error) {
			reject({
				"result": "false",
				"message": "Error in Facebook",
				"error": error
			});
		});

		console.log(`Getting Facebook Access Token: PENDING`);
		request.end();
	});
};

1 个答案:

答案 0 :(得分:1)

承诺中有一个JavaScript错误。 event.stage.fb_app_id和secret不在函数范围内,这很明显,但由于捕获错误是沉默的,我得到一个空对象。