联合身份的API网关自定义授权程序

时间:2017-02-18 19:52:31

标签: facebook facebook-graph-api facebook-javascript-sdk aws-api-gateway aws-cognito

我为API Gateway创建了一个自定义授权器,以便我可以传递Facebook令牌,并使用Cognito的联合身份对其进行身份验证。

我的问题是fb令牌似乎过期,所以我不断收到403错误。我想知道我的方法是否正确。我应该在每次REST API调用时将Facebook令牌作为请求标头的一部分传递给API网关,因此我会传递AWS身份标识。任何反馈都表示赞赏。谢谢。

var AWS = require('aws-sdk');
var cognitoidentity = new AWS.CognitoIdentity();

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

var params = {
  IdentityPoolId: 'us-west-2:xxxxxxxxxxxxxxxxx’, /* required */
  AccountId: ‘xxxxxxxxxxxxxxxxx,
  Logins: {
    'graph.facebook.com': event.authorizationToken //Token given by Facebook
  }
};
console.log(event.methodArn);
cognitoidentity.getId(params, function(err, data) {
  if (err) {
      console.log(err);
      callback(null, generatePolicy('user', 'Deny', event.methodArn));    
  }
  else{
      console.log("success");
      callback(null, generatePolicy('user', 'Allow', event.methodArn));
  }
});
};

var generatePolicy = function(principalId, effect, resource) {
var authResponse = {};

authResponse.principalId = principalId;
if (effect && resource) {
    var policyDocument = {};
    policyDocument.Version = '2012-10-17'; // default version
    policyDocument.Statement = [];
    var statementOne = {};
    statementOne.Action = 'execute-api:Invoke'; // default action
    statementOne.Effect = effect;
    statementOne.Resource = resource;
    policyDocument.Statement[0] = statementOne;
    authResponse.policyDocument = policyDocument;
}
return authResponse;
}

0 个答案:

没有答案