0

我正在使用 Congnito 用户池来执行 API 网关授权,它工作正常。现在,我正在尝试将 Instagram 添加为登录提供程序之一,为此我在 Federated Identities 中创建了自定义身份验证提供程序并使用以下代码:

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
        });
    }
});

我能够获取accessTokensessionToken,但是,我仍然无法找到获取API 网关授权传入请求所需的idTokenaccessToken的方法。

我尝试查看 SDK 以及 AWS 论坛,但我仍然无法找到一种方法来使用自定义联合身份提供商来授权使用认知用户池的 API 网关。

4

1 回答 1

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. 验证您的 cognito 用户
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
   },

});
于 2019-02-08T20:24:51.597 回答