2016-08-23 21 views
1

Android用Cognitoを使用してアクセストークンをリフレッシュするにはどうすればよいですか?文書は(https://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-android-sdk.html)次のことをお勧め:これが動作しない理由はここにCognito User Pool:アクセストークンをリフレッシュする方法

// 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); 

です。セッションを取得しているユーザオブジェクトは、トークンが期限切れになったときに認証されなくなりました。だから、以下を経由してキャッシュされたユーザーを検索し、上記のリターンをnullので、そのユーザを除いて、完璧に動作

が ​​

ないが、私はidでユーザーオブジェクトを取得しようとヌル

CognitoUser user = userPool.getCurrentUser(); 

を返します。認証されたと私はこの呼び出しを試みる

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

場合のみ、トークンは、この作業を行い満了する前に、ユーザーIDがnullであるため、次のコールバックステージで失敗し、私が受け取ることができます新しいアクセストークンしかし、トークンが期限切れになった後にこれを行う方法は?これについての助けに感謝します。

答えて

5

getSession(...)を呼び出してトークンを取得すると、キャッシュされたトークンが期限切れになった場合、SDKは(リフレッシュトークンが期限切れでない限り)トークンを自動的に更新します。リフレッシュトークンも期限切れになっている場合、新しいトークンセットを取得するためにユーザー資格証明(ユーザー名、パスワードなど)が必要になるため、getAuthenticationDetails(...)が呼び出されます。有効なキャッシュされたトークンがあるか、またはトークンがリフレッシュできる場合、getSession()メソッドを使用して有効なトークンを取得します。ユーザーオブジェクトを取得する方法、つまりgetCurrentUser()またはgetUser ..)。

最新のSDK(ver 2.3.1)で再試行してください。

+0

2.3.1これは動作しています – portfoliobuilder

+0

"SDKは自動的にトークンを更新します" - トークンの更新も更新されますか?だから、アプリがまったく使用されていない場合には期限切れになります(リフレッシュトークンの期限は30日です)。または、認証後に制限があります(最初にログインしてから30日後に、もう一度資格情報が必要な場合よりも)。 –

関連する問題