AWS Cognito:无法获取凭证

4
我无法获取我的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服务。

1
您需要将IAM策略附加到Cognito的IAM角色上,以便访问所需的服务。 - Kush Vyas
我不想使用任何第三方登录,比如Facebook或Google。我只想使用基于我的Cognito用户池的自己的登录方式...我已经为我的网站上的已认证用户设置了IAM角色,但不知何故无法使其正常工作。 - David
将策略添加到Cognito未经身份验证的角色中。 - Kush Vyas
已经尝试过了,同样的错误出现了。 - David
共享策略使用和更新问题 - Kush Vyas
经过身份验证的用户可以完全访问我正在使用的所有AWS服务。 - David
1个回答

2
我认为你不需要调用 cognitoIdentity.getCredentialsForIdentity()。当你调用 AWS.config.credentials.get() 时,你的 IAM 密钥应该被放入 AWS.config.credentials 对象中。在你提供的回调函数中直接访问它们即可。
换句话说,在你将“检索到的身份”记录到控制台时,凭证对象应该已经包含你的秘密密钥、访问密钥 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);

如果您在Logins映射中传递了用户池ID和访问令牌,则getCredentialsForIdentity()调用可能成功;我没有测试过。 我还没有遇到需要使用此特定API的用例,并且我怀疑您也不需要它。
来源:我在一个100%的JavaScript应用程序上工作,该应用程序同时使用经过身份验证和未经身份验证的Cognito身份。 我们没有在任何地方调用getCredentialsForIdentity(),尝试插入它会产生与您收到的相同错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接