【问题标题】:How to get the temporary credentials for aws using identity token?如何使用身份令牌获取 aws 的临时凭证?
【发布时间】:2018-08-18 13:17:11
【问题描述】:

用例:我想使用联合身份限制对我的 API 网关路径的访问。

为此,我需要使用我的 ID 令牌获取 AWS 临时凭证。我得到了我的 Id 令牌,但我不知道如何使用 Id 令牌获取临时凭证。

这是将 ID 令牌传递给 cognito 凭据映射

  var data = {
      UserPoolId: config.cognito.USER_POOL_ID,
      ClientId: config.cognito.APP_CLIENT_ID,
    };
    var userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
    var cognitoUser = userPool.getCurrentUser();
    if (cognitoUser != null) {
      cognitoUser.getSession(function(err, result) {
          if (result) {
              console.log('You are now logged in.');

              // Add the User's Id Token to the Cognito credentials login map.
              AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                  IdentityPoolId: config.cognito.IDENTITY_POOL_ID,
                  Logins: {
                    'cognito-idp.ap-south-1.amazonaws.com/ap-south-1_G7YbVxxxx': result.getIdToken().getJwtToken()
                  }
              });
          }
      });
  }

如何获得临时凭证?

谢谢

【问题讨论】:

    标签: amazon-cognito aws-cognito


    【解决方案1】:

    使用下面的 sn-p 获得临时凭证

     AWS.config.credentials.get(function(err,data) {
          if (!err) {
            var id = AWS.config.credentials.identityId;
            var key = AWS.config.credentials.accessKeyId;
            var secretkey = AWS.config.credentials.secretAccessKey;
            var sessionToken = AWS.config.credentials.sessionToken;
            console.log('Cognito Identity ID '+ id);
            console.log('Cognito Key '+ key);
            console.log('Cognito Secret Key '+ secretkey);
            console.log('Cognito SessionToken '+ sessionToken);
          }
        });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-05
    • 2020-12-17
    • 2019-11-18
    • 2023-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    相关资源
    最近更新 更多