如何恢复过期的令牌[AWS Cognito]? [英] How to restore an expired token [AWS Cognito]?

查看:103
本文介绍了如何恢复过期的令牌[AWS Cognito]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上使用AWS。 1小时后,令牌过期,用户几乎无法做任何事情。

I'm using AWS for my website. After 1 hour the token expires and the user pretty much can't do anything.

现在我正在尝试刷新这样的凭据:

For now i'm trying to refresh the credentials like this:

 function getTokens(session) {
   return {
     accessToken: session.getAccessToken().getJwtToken(),
     idToken: session.getIdToken().getJwtToken(),
     refreshToken: session.getRefreshToken().getToken()
   };
 };


function getCognitoIdentityCredentials(tokens) {
  const loginInfo = {};
  loginInfo[`cognito-idp.eu-central-1.amazonaws.com/eu-central-1_XXX`] = tokens.idToken;
  const params = {
    IdentityPoolId: AWSConfiguration.IdPoolId
    Logins: loginInfo
  };
  return new AWS.CognitoIdentityCredentials(params);
 };


 if(AWS.config.credentials.needsRefresh()) {
    clearInterval(messwerte_updaten);
    cognitoUser.refreshSession(cognitoUser.signInUserSession.refreshToken, (err, session) => {
      if (err) {
        console.log(err);
      }
      else {
        var tokens = getTokens(session);

        AWS.config.credentials = getCognitoIdentityCredentials(tokens);

        AWS.config.credentials.get(function (err) {
          if (err) {
            console.log(err);
          }
          else {
            callLambda();
          }
       });
     }
   });
 }

事情是,1小时后,登录令牌会刷新而没有问题,但是2小时后我再也无法刷新登录令牌。

the thing is, after 1hour, the login token gets refreshed without a problem, but after 2hrs i can't refresh the login token anymore.

我也试过使用 AWS.config.credentials.get() AWS.config.credentials.getCredentials() AWS.config.credentials.refresh()
也不起作用。

i also tried using AWS.config.credentials.get(), AWS.config.credentials.getCredentials() and AWS.config.credentials.refresh() which doesn't work either.

我得到的错误消息是:


配置中缺少凭据

Missing credentials in config

无效的登录令牌。令牌已过期:1446742058> = 1446727732

Invalid login token. Token expired: 1446742058 >= 1446727732


推荐答案

差不多两周后我终于解决了。

After almost 2 weeks i finally solved it.

您需要刷新令牌才能接收新的Id令牌。获取刷新令牌后,使用新的Id令牌更新AWS.config.credentials对象。

You need the Refresh Token to receive a new Id Token. Once the Refreshed Token is acquired, update the AWS.config.credentials object with the new Id Token.

这里有一个如何设置它的示例,运行顺利!

here is an example on how to set this up, runs smoothly!

refresh_token = session.getRefreshToken();   // you'll get session from calling cognitoUser.getSession()

if (AWS.config.credentials.needsRefresh()) {

  cognitoUser.refreshSession(refresh_token, (err, session) => {
    if(err) {
      console.log(err);
    } 
    else {
      AWS.config.credentials.params.Logins['cognito-idp.<YOUR-REGION>.amazonaws.com/<YOUR_USER_POOL_ID>']  = session.getIdToken().getJwtToken();
      AWS.config.credentials.refresh((err)=> {
        if(err)  {
          console.log(err);
        }
        else{
          console.log("TOKEN SUCCESSFULLY UPDATED");
        }
      });
    }
  });
}

这篇关于如何恢复过期的令牌[AWS Cognito]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆