【问题标题】:amazon-cognito-identity-js refresh token expiration handlingamazon-cognito-identity-js 刷新令牌过期处理
【发布时间】:2019-01-24 08:02:03
【问题描述】:

当通过 get session 检索 id 令牌时,如果访问令牌已过期,cognito identity js 会使用其刷新令牌自动检索新的访问令牌。但是,如果刷新令牌也已过期,我想实施正确的处理,但很难测试,因为刷新令牌的最短过期时间是 1 天。

很高兴知道是否:

  • 还有其他方法可以正确测试访问和刷新令牌过期时会发生什么(因此我可以测试重定向到登录页面)

  • 调用了哪个代码路径或如何捕获刷新令牌过期的情况

代码:

getIdToken(callback: Callback): void {
if (callback == null) {
  throw("callback is null");
}
if (this.getCurrentUser() != null) {
  this.getCurrentUser().getSession(function (err, session) {
    if (err) {
      console.log("error: " + err);
      callback.callbackWithParam(null);
    } else {
      if (session.isValid()) {
        console.log("returning id token");
        callback.callbackWithParam(session.getIdToken().getJwtToken());
      } else {
        console.log("got the id token, but the session isn't valid");
      }
    }
  });
  } 
  else
    callback.callbackWithParam(null);
}

我的猜测是got the id token, but the session isn't valid 会被调用,因为当刷新令牌有效时它会自动刷新访问令牌并且会话再次有效。

【问题讨论】:

    标签: javascript amazon-web-services typescript amazon-cognito refresh-token


    【解决方案1】:

    登录 Kibana 时,我收到以下消息:

    com.amazonaws.services.cognitoidp.model.NotAuthorizedException: Refresh Token has expired (Service: AWSCognitoIdentityProvider; Status Code: 400; Error Code: NotAuthorizedException; Request ID: ...)
    

    在这种情况下,将调用 err 分支

    if (err) {
      console.log("error: " + err);
      callback.callbackWithParam(null);
    }
    

    因此需要在此处完成刷新令牌到期的处理。但是,我决定在每种情况下都将用户重定向到登录页面,session.isValid()

    希望这对那里的人有帮助:)

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2020-02-05
      • 2014-09-18
      • 2022-08-14
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      相关资源
      最近更新 更多