【问题标题】:How to get AWS credentials with a UserPools idToken?如何使用 UserPools idToken 获取 AWS 凭证?
【发布时间】:2017-08-10 03:33:21
【问题描述】:

我目前正在使用 USER-POOLS 授权器为我的 API 获取前 3 个令牌:

  • idToken
  • 刷新令牌
  • 访问令牌

我想从这里请求凭据,以便能够向我已经设置的 API 网关发出 SigV4 请求,但首先我需要获取请求的凭据才能执行 SigV4。

在文档中我发现了这个:

// Set the region where your identity pool exists (us-east-1, eu-west-1)
AWSCognito.config.region = 'us-east-1';

// Configure the credentials provider to use your identity pool
AWSCognito.config.credentials = new AWSCognito.CognitoIdentityCredentials({
  IdentityPoolId: 'us-east-1:009xxxx ...',
});

// Make the call to obtain credentials
AWSCognito.config.credentials.get(function(){

  // Credentials will be available when this function is called.
  var accessKeyId = AWSCognito.config.credentials.accessKeyId;
  var secretAccessKey = AWSCognito.config.credentials.secretAccessKey;
  var sessionToken = AWSCognito.config.credentials.sessionToken;

});

令我惊讶的是,回调被调用了,但 - accessKeyId - 秘密访问密钥 - 会话令牌 都是空的。

我期待某种方法,我发送我的第一个 idToken,并基于此获得凭据,但看起来这一切都是在幕后想出来的?无论如何它对我不起作用。

【问题讨论】:

    标签: amazon-web-services aws-sdk amazon-cognito aws-cognito


    【解决方案1】:

    经过一番研究,我意识到有一种未记录的方法可以做到这一点。

    你需要先构造这个对象:

    let url = 'cognito-idp.' + 'identity pool region' + '.amazonaws.com/' + 'your user pool id';
    let logins = {};
    
    logins[url] = idTokenJwt; // <- the one obtained before
    
    let params = {
       IdentityPoolId: 'the federated identity pool id', 
       Logins: logins
    };
    
    let creds = new AWS.CognitoIdentityCredentials(params);
    
    
    AWS.config.region = 'us-east-1';
    AWS.config.credentials = creds;
    
    creds.get(function (err: any) {
      if (!err) {
        console.log("returned without error"); // <-- this gets called!!!
    
        // and the values are correctly set!
        var accessKeyId = AWS.config.credentials.accessKeyId;
        var secretAccessKey = AWS.config.credentials.secretAccessKey;
        var sessionToken = AWS.config.credentials.sessionToken;
    
      }
      else{
        console.log("returned with error"); // <-- might get called if something is missing, anyways self-descriptive. 
        console.log(err);
      }
    });
    

    在我的情况下,我仍然需要配置角色和身份池之间的信任关系,这里是示例:

    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Federated": "cognito-identity.amazonaws.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "cognito-identity.amazonaws.com:aud": "your federated identity pool id"
        },
        "ForAnyValue:StringLike": {
          "cognito-identity.amazonaws.com:amr": "authenticated"
        }
      }
    }
    

    *您也可以根据需要将“authenticated”替换为“unauthenticated”、“graph.facebook.com”、“google ...”。

    【讨论】:

      猜你喜欢
      • 2017-01-14
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多