【问题标题】:How to get session callback info? AWS - Cognito - JavaScript如何获取会话回调信息? AWS - 认知 - JavaScript
【发布时间】:2019-10-14 10:00:20
【问题描述】:

我正在尝试获取 AccessKeyID 和 SecretKey(最终以编程方式输入到具有细粒度访问控制的 dynamoDB 上的 CRUD 操作)。

任何人,都有许多 API 调用来获取凭据,例如 getCredentialsForIdentity()。但是,这些都需要更多的参数,这需要更多的api请求和更多的参数等等。

我找到了一种登录 Cognito 用户的方法,然后检查他们是否登录了个人资料页面,其中可以显示电子邮件和用户名:

var data = { 
    UserPoolId : _config.cognito.userPoolId,
            ClientId : _config.cognito.clientId
    };
    var userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
    var cognitoUser = userPool.getCurrentUser();

window.onload = function(){
        if (cognitoUser != null) {
            cognitoUser.getSession(function(err, session) {
                if (err) {
                    alert(err);
                    return;
                }

            console.log('session validity: ' + session.isValid());
            console.log(session.Credentials.AccessKeyId); //<--THIS DOESNT WORK

    cognitoUser.getUserAttributes(function(err, result) {
        if (err) {
            console.log(err);
            return;
            }
        console.log(result);

            document.getElementById("email_value").innerHTML = result[2].getValue();    
            document.getElementById("username").innerHTML = cognitoUser.getUsername();  
    });                 
      });
    }
  }

console.log(session.Credentials.AccessKeyId); 似乎是一个无效请求。我假设session 中有一些很好的回调信息,比如我正在寻找的 AccessKeyID 和 SecretKey。

【问题讨论】:

    标签: amazon-web-services authentication token amazon-cognito


    【解决方案1】:

    session.Credentials.AccessKeyId 将不起作用,因为会话的输出是:

    e {idToken: t, refreshToken: e, accessToken: t, clockDrift: 0}

    而 AWS.config.credentials 的输出是:

    因此您的代码将是:

    var data = { 
        UserPoolId : _config.cognito.userPoolId,
        ClientId : _config.cognito.clientId
    };
    var userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
    var cognitoUser = userPool.getCurrentUser();
    
    window.onload = function(){
            if (cognitoUser != null) {
                cognitoUser.getSession(function(err, session) {
                    if (err) {
                        alert(err);
                        return;
                    }
                console.log('session validity: ' + session.isValid());
                AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                     IdentityPoolId: _config.cognito.IdentityPoolId
                });
                AWS.config.credentials.get(function(err) {
                     console.log(AWS.config.credentials); // * see above what your response will be
                     console.log(AWS.config.credentials.accessKeyId);
                     console.log(AWS.config.credentials.secretAccessKey);
          });
        }
      }
    

    【讨论】:

    • 非常感谢。有没有办法让登录的用户以编程方式获取用户池?还是必须硬编码?
    • @FlightHoldings 据我所知,因为您必须确定要使用的用户池。但是,您可以在对用户进行身份验证后立即获取 AWS.config.credentials - 但是为此您需要用户名/电子邮件和登录密码。
    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2012-09-26
    • 2011-11-12
    相关资源
    最近更新 更多