【问题标题】:AWS Cognito: Can't get CredentialsAWS Cognito:无法获取凭证
【发布时间】:2018-03-28 19:42:49
【问题描述】:

我无法获得我的 CognitoIdentity 的凭据。当用户成功通过身份验证后,他需要获得一个身份才能访问其他 AWS 服务。就我而言,这就是 AWS IoT。但不知何故,我无法获得任何凭据。

这是错误信息:

检索凭据时出错:NotAuthorizedException:访问 身份 'eu-central-1:XXXXXXXXXX' 是 禁止。

我的代码几乎和 Github 上的教程一模一样:

 var cognitoUser = new AWSCognito.CognitoUser(userData);
  cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
      console.log("Logged in");
      console.log('access token + ' + result.getAccessToken().getJwtToken());
      //    window.location.href = "index.html";
      AWS.config.region = AWSConfiguration.region;

      AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: AWSConfiguration.IdPoolId,
        Logins : {
          'cognito-idp.eu-central-1.amazonaws.com/eu-central-1_XXXX' : result.getIdToken().getJwtToken()
        }
      });
      var cognitoIdentity = new AWS.CognitoIdentity();
      AWS.config.credentials.get(function(err, data) {
        if (!err) {
          console.log('retrieved identity: ' + AWS.config.credentials.identityId);
          var params = {
            IdentityId: AWS.config.credentials.identityId
          };
          cognitoIdentity.getCredentialsForIdentity(params, function(err, data) {
            if (!err) {
              thingShadows.updateWebSocketCredentials(data.credentials.AccessKeyId,
                                                 data.credentials.SecretKey,
                                                 data.credentials.SessionToken);
            }
            else {
              console.log('error retrieving credentials: ' + err);
            }
          });
        }
        else {
          console.log('error retrieving identity:' + err);
        }
      });
    }
  });  

请注意,我跳过了不相关的代码。 经过身份验证的用户可以完全访问我正在使用的所有 AWS 服务。

【问题讨论】:

  • 您需要将 IAM 策略附加到您需要访问的服务的 Cognito 的 IAM 角色。
  • 我不想使用任何第三方登录,例如 facebook 或 google。我只想使用我自己的登录名,基于我的 cognito 用户池......我已经在我自己的网站上为经过身份验证的用户设置了一个 IAM 角色,但不知何故我无法让它工作。
  • 然后将策略添加到 cognito 未经身份验证的角色
  • 已经这样做了,出现同样的错误。
  • 共享政策使用和更新问题

标签: javascript amazon-web-services aws-cognito aws-iot


【解决方案1】:

我认为您无需致电cognitoIdentity.getCredentialsForIdentity()。当您调用 AWS.config.credentials.get() 时,您的 IAM 密钥应放入 AWS.config.credentials 对象中。您可以直接在调用时提供的回调中访问它们。

换句话说,当您将retrieved identity: 注销到控制台时,凭证对象中应该已经包含您的密钥、访问密钥 ID 和会话令牌。

所有这些(给或带一个花括号):

 var params = {
    IdentityId: AWS.config.credentials.identityId
  };
  cognitoIdentity.getCredentialsForIdentity(params, function(err, data) {
    if (!err) {
      thingShadows.updateWebSocketCredentials(data.credentials.AccessKeyId,
                                         data.credentials.SecretKey,
                                         data.credentials.SessionToken);
    }
    else {
      console.log('error retrieving credentials: ' + err);
    }
  });

大概可以换成这样的:

thingShadows.updateWebSocketCredentials(AWS.config.credentials.accessKeyId,
                                        AWS.config.credentials.secretKey,
                                        AWS.config.credentials.sessionToken);

如果您传入带有用户池 ID 和访问令牌的 Logins 映射,则 getCredentialsForIdentity() 调用可能成功;我没有测试它。我还没有遇到需要使用这个特定 API 的用例,我怀疑你也不需要它。

资料来源:我正在开发一个 100% 的 javascript 应用程序,该应用程序同时使用经过身份验证和未经身份验证的 Cognito 身份。我们不会在任何地方调用getCredentialsForIdentity(),并且尝试插入它会产生与您遇到的相同的错误。

【讨论】:

    猜你喜欢
    • 2020-06-06
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 2020-12-17
    • 2021-01-20
    相关资源
    最近更新 更多