【问题标题】:AWS Cognito on Android - How to get a new session from a refresh tokenAndroid 上的 AWS Cognito - 如何从刷新令牌中获取新会话
【发布时间】:2018-12-09 18:13:48
【问题描述】:

我正在使用 AWS Cognito 作为 android 应用程序的 mu 身份验证提供程序,并且我在我的用户池中设置了 30 天的刷新令牌过期时间。

我收到的访问令牌有效期最长为 1 小时,因此如果用户离开应用并在 1 小时内返回,我可以通过在 CognitoUserPool 上调用 getCurrentUser() 自动更新用户会话。

但是,如果用户在几个小时后回来,我想使用缓存的刷新令牌,这样他们就不需要经常重新登录。CognitoUserPool 文档表明它只会在使用刷新令牌时使用而访问令牌仍处于活动状态。

这不会破坏刷新令牌的目的,还是说刷新令牌只能在访问令牌处于活动状态时使用?

如果无法使用刷新令牌来获取新会话,应用如何让用户一次保持一天/数周/数月的登录状态?他们是否将密码存储在设备上?

【问题讨论】:

  • 您在哪里看到“只有在访问令牌仍然处于活动状态时才使用刷新令牌”的部分?这似乎与docs.aws.amazon.com/cognito/latest/developerguide/… 冲突
  • @Michael-sqlbot 身份验证失败的原因是因为我使用的是getSession 而不是getSessionInBackground,并且由于它在主线程上运行而引发了异常。
  • 但是,我似乎误解的部分文档是 The Mobile SDK for iOS and the Mobile SDK for Android automatically refresh your ID and access tokens if there is a valid (non-expired) refresh token present, and the ID and access tokens have a minimum remaining validity of 5 minutes. If the refresh token is expired, your app user must reauthenticate by signing in again to your user pool. 说访问令牌必须至少再有效 5 分钟才能刷新。
  • 我可能不正确,但我将其解释为,当用户处于活动状态时,访问和令牌持续保持最新状态,并且每次接近到期时,SDK在令牌达到其剩余不到 5 分钟的有效期之前开始处理此后台自动刷新。

标签: android amazon-web-services security authentication aws-cognito


【解决方案1】:

Cognito SDK 内部使用刷新令牌来刷新当前用户会话:

//get CognitoUser instance by passing userName or emailID

CognitoUser cognitoUser = cognitoUserPool.getUser(userName);

//simply get User Session again and in your authenticationHandler, handle userSession obtained
cognitoUser.getSessionInBackground(authenticationHandler);

getSessionInBackground 将在内部使用刷新令牌生成新会话,无需传递任何密码。

【讨论】:

  • 这不是真的,AuthenticationHandler 方法 getAuthenticationDetails 需要有效密码才能刷新令牌。您提到的方法只能与带有有效密码的有效用户ID一起使用。
猜你喜欢
  • 2020-12-13
  • 2023-03-16
  • 2018-12-01
  • 2018-04-03
  • 1970-01-01
  • 2021-11-05
  • 2020-10-16
  • 2021-08-25
  • 2018-07-25
相关资源
最近更新 更多