将instagram用作API网关的``授权人''-AWS Cognito

时间:2018-06-26 15:56:44

标签: amazon-web-services amazon-cognito aws-cognito aws-sdk-js

我正在使用Congnito用户池执行API网关授权,效果很好。现在,我试图将Instagram添加为登录提供程序之一,为此我在联合身份中使用以下代码创建了自定义身份验证提供程序

var cognitoidentity = new AWS.CognitoIdentity({ apiVersion: '2014-06-30' });

var params = {
    IdentityPoolId: 'us-east-1:7d99e750-.....',
    Logins: {
        'login.instagram': 'Access-Token-Returned-By-Instagram',
    },
    TokenDuration: 60
};

cognitoidentity.getOpenIdTokenForDeveloperIdentity(params, function (err, data) {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data);
        var idParams = {
            IdentityId: data['IdentityId'],
            Logins: {
                'cognito-identity.amazonaws.com': data['Token']
            }
        };

        cognitoidentity.getCredentialsForIdentity(idParams, function (err2, data2) {
            if (err2) console.log(err2, err2.stack); // an error occurred
            else console.log(data2);           // successful response
        });
    }
});

我能够获得 accessToken sessionToken ,但是,我仍然找不到找到 idToken 的方法。 API网关用来授权传入请求的accessToken

我尝试研究SDK和AWS论坛,但仍然找不到使用自定义联合身份提供者来授权使用认知用户池的API Gateway的方法。

1 个答案:

答案 0 :(得分:0)

我要对此一想。...

  1. 您有一个认知用户吗?

import { CognitoUser, CognitoUserPool, AuthenticationDetails } from "amazon-cognito-identity-js";

let cognitoUser;

const userPool = new CognitoUserPool({
   UserPoolId: config.USER_POOL.pool_Id, //your userpool id
   ClientId: config.appClientId, //your appClient 
});

const userData = {
       Username: 'user name',
       Pool: userPool
 };

cognitoUser = new CognitoUser(userData);
/* Should now have a cognitoUser */
  1. 验证您的认知用户
const authenticationData = {
    Username : payload.userName,
    Password : payload.password,
};

const authenticationDetails = new AuthenticationDetails(authenticationData);

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        const accessToken = result.getAccessToken().getJwtToken();

        /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer*/
         const idToken = result.idToken.jwtToken;

          /*
            Do something with 
              idToken
              accessToken

           I would write them to a file or encrypt them and store them on the localStorage OR encrypt and save in REDUX store.
          */


    },

    onFailure: function(err) {
         //handle error
   },

});