我的ID令牌有效期为一小时。一小时后,当我在服务调用中的CognitoUser对象上调用getSessionInBackground时,我的调用失败。如果前一个ID令牌已经过期,那么它是否应该返回一个新的ID令牌?没有帮助这种行为不能始终如一地重现 - 它只发生在某些屏幕上。此外,它似乎只发生在Android,而不是iOS。
答案 0 :(得分:1)
这种情况发生在sdk中并不是完全线程安全的,这个错误发生在aws-sdk-version< 2.3中,因为一个线程读取正确的值,如果令牌过期,它会清除cachedToken,它不会留下任何内容要读取的第二个线程,第二个线程假定没有令牌存在(当第一个线程刷新令牌时),因此抛出用户未授权的异常。
如果您使用的是aws-sdk2.2,这是一个突出的错误,您可以使用以下代码段来修复它:
new Thread(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
CognitoUser user = userPool.getCurrentUser();
user.getSessionInBackground(new AuthenticationHandler() {
@Override
public void onSuccess(final CognitoUserSession userSession) {
Log.d("Authenticator.java", userSession.getIdToken().getJWTToken().toString());
semaphore.release();
callback.onSuccess(userSession);
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String UserId) {
semaphore.release();
callback.onNeedsPassword();
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
semaphore.release();
callback.onNeedsPassword();
}
@Override
public void onFailure(final Exception exception) {
semaphore.release();
callback.onFailure(exception);
}
});
}
}).start();
当前的aws-sdk-2.4版本中还存在一个与此相关的错误,如果您多次调用aws以获取新的ID令牌,则会增加您的网络流量。(这是一个边缘情况,但仍然存在) https://github.com/aws/aws-sdk-android/pull/272