amazon - Cognito User Pool: How to refresh Access Token Android -
how refresh access token using cognito android? documentation suggest following (https://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-android-sdk.html):
// implement authentication handler authenticationhandler handler = new authenticationhandler { @override public void onsuccess(cognitousersession usersession) { // authentication successful, "usersession" have current valid tokens // time awesome stuff } @override public void getauthenticationdetails(final authenticationcontinuation continuation, final string userid) { // user authentication details, userid , password required continue. // use "continuation" object pass user authentication details // after user authentication details available, wrap them in authenticationdetails class // along userid , password, parameters user pools lambda can passed here // validation parameters "validationparameters" passed in map<string, string> authenticationdetails authdetails = new authenticationdetails(userid, password, validationparameters); // allow authentication continue continuation.setauthenticationdetails(authdetails); continuation.continuetask(); } @override public void getmfacode(final multifactorauthenticationcontinuation continuation) { // multi-factor authentication required authenticate // code sent user, use code continue authentication // find code sent string codesenthere = continuation.getparameter()[0]; // when verification code available, continue authenticate continuation.setmfacode(code); continuation.continuetask(); } @override public void authenticationchallenge(final challengecontinuation continuation) { // custom challenge has solved authenticate // set challenge responses // call continuetask() method respond challenge , continue authentication. } @override public void onfailure(final exception exception) { // authentication failed, probe exception cause } }; user.getsession(handler);
here why not work. user object getting session no longer authenticated when token expires. retrieving cached user via below, return null
cognitouser user = userpool.getcurrentuser();
because above returns null, try user object id
cognitouser user = userpool.getuser(userid);
which works perfectly, except user not authenticated , fail during following callback stage because userid null
@override public void getauthenticationdetails(final authenticationcontinuation continuation, final string userid)
only when attempt call before token expires work, , can receive new access token. how after token has expired? on appreciated. in advance
when call getsession(...) - tokens - , if cached tokens have expired, sdk automatically refresh tokens (as long refresh token has not expired). if refresh token has expired, getauthenticationdetails(...) invoked because user credentials (username, password, etc) required new set of tokens. should not matter how user object, i.e. through getcurrentuser() or getuser(...) methods, long there valid cached tokens or if tokens can refreshed, valid tokens getsession(...).
retry latest sdk (ver 2.3.1).
Comments
Post a Comment