2016-08-23 40 views
1

如何使用Cognito for Android刷新访问令牌?该文档建议如下(https://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-android-sdk.html):Cognito用户池:如何刷新访问令牌Android

// Implement authentication handler 
AuthenticationHandler handler = new AuthenticationHandler { 
    @Override 
    public void onSuccess(CognitoUserSession userSession) { 
     // Authentication was successful, the "userSession" will have the current valid tokens 
     // Time to do awesome stuff 
    } 

    @Override 
    public void getAuthenticationDetails(final AuthenticationContinuation continuation, final String userID) { 
     // User authentication details, userId and password are required to continue. 
     // Use the "continuation" object to pass the user authentication details 

     // After the user authentication details are available, wrap them in an AuthenticationDetails class 
     // Along with userId and password, parameters for user pools for Lambda can be passed here 
     // The validation parameters "validationParameters" are passed in as a Map<String, String> 
     AuthenticationDetails authDetails = new AuthenticationDetails(userId, password, validationParameters); 

     // Now allow the authentication to continue 
     continuation.setAuthenticationDetails(authDetails); 
     continuation.continueTask(); 
    } 

    @Override 
    public void getMFACode(final MultiFactorAuthenticationContinuation continuation) { 
     // Multi-factor authentication is required to authenticate 
     // A code was sent to the user, use the code to continue with the authentication 


     // Find where the code was sent to 
     String codeSentHere = continuation.getParameter()[0]; 

     // When the verification code is available, continue to authenticate 
     continuation.setMfaCode(code); 
     continuation.continueTask(); 
    } 

    @Override 
    public void authenticationChallenge(final ChallengeContinuation continuation) { 
     // A custom challenge has to be solved to authenticate 

     // Set the challenge responses 

     // Call continueTask() method to respond to the challenge and continue with authentication. 
    } 

    @Override 
    public void onFailure(final Exception exception) { 
     // Authentication failed, probe exception for the cause 

    } 
}; 
user.getSession(handler); 

这是为什么这是行不通的。我获取Session的用户对象在令牌过期时不再进行身份验证。因此,通过下面的检索缓存的用户,将返回null

CognitoUser user = userPool.getCurrentUser(); 

由于上述返回NULL,我试图通过ID来获得用户对象

​​

这完美的作品,但用户不验证并因为用户ID是空

@Override 
public void getAuthenticationDetails(final AuthenticationContinuation continuation, final String userID) 

只有当我尝试此调用令牌到期之前做这项工作在以下回调阶段会失败,并且可以接收一个新的访问令牌。但是在令牌过期后如何做到这一点?任何帮助,将不胜感激。提前致谢

回答

5

当您调用getSession(...) - 获取标记 - 并且缓存的标记已过期时,SDK将自动刷新标记(只要刷新标记未过期)。如果刷新令牌已过期,则会调用getAuthenticationDetails(...),因为现在需要用户凭证(用户名,密码等)才能获取新的令牌。不管你如何获得用户对象,即通过getCurrentUser()或getUser(...)方法,只要存在有效的缓存标记或者如果可以刷新标记,就可以通过getSession()获得有效标记。 ..)。

使用最新的SDK(版本2.3.1)重试。

+0

2.3.1这是工作 – portfoliobuilder

+0

“SDK将自动刷新标记” - 刷新标记更新以及?因此,如果应用程序完全未使用一段时间(默认30天,我认为刷新令牌过期),它将过期。或者自认证以来有限制(例如,首次登录后30天,比您再次需要凭证)? –

相关问题