代码之家  ›  专栏  ›  技术社区  ›  David

AWS Cognito:无法获取凭据

  •  4
  • David  · 技术社区  · 7 年前

    我无法获得CognitoIdentity的凭据。当用户成功通过身份验证时,他需要获得身份才能访问其他AWS服务。在我的情况下,这是非常重要的。但不知为什么,我没有任何证书。

    这是错误消息:

    检索凭据时出错:NotAuthorizedException:访问 标识“eu-central-1:xxxxxxxxx”为 被禁止的

    我的代码与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 回复  |  直到 7 年前
        1
  •  2
  •   Mike Patrick    7 年前

    我觉得你不需要打电话 cognitoIdentity.getCredentialsForIdentity() . 您的IAM钥匙应该放在 AWS.config.credentials 对象时调用 AWS.config.credentials.get() . 您可以在调用时提供的回调中直接访问它们。

    换句话说,当你注销时 retrieved identity:

     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() 在任何地方,尝试插入它都会产生与您相同的错误。