3131import com .amazonaws .mobile .client .results .Tokens ;
3232import com .amazonaws .mobile .client .results .UserCodeDeliveryDetails ;
3333import com .amazonaws .mobile .config .AWSConfiguration ;
34+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .CognitoDevice ;
35+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .CognitoUser ;
36+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .CognitoUserDetails ;
37+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .CognitoUserPool ;
38+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .CognitoUserSession ;
39+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .AuthenticationContinuation ;
40+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .ChallengeContinuation ;
41+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .MultiFactorAuthenticationContinuation ;
42+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .handlers .AuthenticationHandler ;
43+ import com .amazonaws .mobileconnectors .cognitoidentityprovider .handlers .GetDetailsHandler ;
3444import com .amazonaws .regions .Region ;
3545import com .amazonaws .regions .Regions ;
3646import com .amazonaws .services .cognitoidentity .AmazonCognitoIdentity ;
5060import com .amazonaws .services .cognitoidentityprovider .model .ListUsersRequest ;
5161import com .amazonaws .services .cognitoidentityprovider .model .ListUsersResult ;
5262import com .amazonaws .services .cognitoidentityprovider .model .MessageActionType ;
63+ import com .amazonaws .services .cognitoidentityprovider .model .NotAuthorizedException ;
5364import com .amazonaws .services .cognitoidentityprovider .model .ResourceNotFoundException ;
5465import com .amazonaws .services .cognitoidentityprovider .model .UserNotConfirmedException ;
5566import com .amazonaws .services .cognitoidentityprovider .model .UserType ;
@@ -104,6 +115,7 @@ public class AWSMobileClientTest extends AWSMobileClientTestBase {
104115 private static final int THROTTLED_DELAY = 5000 ;
105116
106117 static AmazonCognitoIdentityProvider userpoolLL ;
118+ static CognitoUserPool userPool ;
107119
108120 static {
109121 try {
@@ -117,6 +129,8 @@ public class AWSMobileClientTest extends AWSMobileClientTestBase {
117129 static Regions clientRegion = Regions .US_WEST_2 ;
118130 static String userPoolId ;
119131 static String identityPoolId ;
132+ static String clientId ;
133+ static String clientSecret ;
120134
121135 Context appContext ;
122136 AWSMobileClient auth ;
@@ -226,13 +240,17 @@ public void onError(Exception e) {
226240 assertNotNull (userPoolConfig );
227241 clientRegion = Regions .fromName (userPoolConfig .getString ("Region" ));
228242 userPoolId = userPoolConfig .getString ("PoolId" );
243+ clientId = userPoolConfig .getString ("AppClientId" );
244+ clientSecret = userPoolConfig .optString ("AppClientSecret" );
229245
230246 JSONObject identityPoolConfig =
231247 awsConfiguration .optJsonObject ("CredentialsProvider" ).getJSONObject (
232248 "CognitoIdentity" ).getJSONObject ("Default" );
233249 assertNotNull (identityPoolConfig );
234250 identityPoolId = identityPoolConfig .getString ("PoolId" );
235251
252+ userPool = new CognitoUserPool (appContext , userPoolId , clientId , clientSecret , clientRegion );
253+
236254 deleteAllUsers (userPoolId );
237255 createUserViaAdminAPI (userPoolId , USERNAME_ADMIN_API_USER , EMAIL_ADMIN_API_USER );
238256 }
@@ -413,6 +431,124 @@ public void onUserStateChanged(UserStateDetails details) {
413431 assertNotEquals (getPackageConfigure ().getString ("identity_id" ), details .toString ());
414432 }
415433
434+ @ Test
435+ public void testRevokeTokenWithSignedInUser () throws Exception {
436+ auth .signIn (username , PASSWORD , null );
437+ assertTrue ("isSignedIn is true" , auth .isSignedIn ());
438+
439+ final AtomicReference <Boolean > tokenRevoked = new AtomicReference <Boolean >(false );
440+ final CountDownLatch revokeTokenLatch = new CountDownLatch (2 );
441+ final CognitoUser user = userPool .getCurrentUser ();
442+ user .getSession (new AuthenticationHandler () {
443+ @ Override
444+ public void onSuccess (CognitoUserSession userSession , CognitoDevice newDevice ) {
445+ revokeTokenLatch .countDown ();
446+ }
447+
448+ @ Override
449+ public void getAuthenticationDetails (AuthenticationContinuation authenticationContinuation , String userId ) {
450+
451+ }
452+
453+ @ Override
454+ public void getMFACode (MultiFactorAuthenticationContinuation continuation ) {
455+
456+ }
457+
458+ @ Override
459+ public void authenticationChallenge (ChallengeContinuation continuation ) {
460+
461+ }
462+
463+ @ Override
464+ public void onFailure (Exception exception ) {
465+ exception .printStackTrace ();
466+ fail ("Sign in failed." );
467+ }
468+ });
469+
470+ user .getDetails (new GetDetailsHandler () {
471+ @ Override
472+ public void onSuccess (CognitoUserDetails cognitoUserDetails ) {
473+ revokeTokenLatch .countDown ();
474+ }
475+
476+ @ Override
477+ public void onFailure (Exception exception ) {
478+ exception .printStackTrace ();
479+ fail ("Get user details failed." );
480+ }
481+ });
482+
483+ try {
484+ user .revokeTokens ();
485+ tokenRevoked .set (true );
486+ } catch (Exception e ) {
487+ e .printStackTrace ();
488+ }
489+
490+ revokeTokenLatch .await (5 , TimeUnit .SECONDS );
491+ assertTrue (tokenRevoked .get ());
492+
493+ user .getDetails (new GetDetailsHandler () {
494+ @ Override
495+ public void onSuccess (CognitoUserDetails cognitoUserDetails ) {
496+ fail ("Request to get user details should fail with NotAuthorizedException after token is revoked." );
497+ }
498+
499+ @ Override
500+ public void onFailure (Exception exception ) {
501+ assertTrue (exception instanceof NotAuthorizedException );
502+ }
503+ });
504+ }
505+
506+ @ Test
507+ public void testRevokeTokenWithSignedOutUser () throws Exception {
508+ auth .signIn (username , PASSWORD , null );
509+ assertTrue ("isSignedIn is true" , auth .isSignedIn ());
510+
511+ final CountDownLatch revokeTokenLatch = new CountDownLatch (1 );
512+ final CognitoUser user = userPool .getCurrentUser ();
513+ user .getSession (new AuthenticationHandler () {
514+ @ Override
515+ public void onSuccess (CognitoUserSession userSession , CognitoDevice newDevice ) {
516+ revokeTokenLatch .countDown ();
517+ }
518+
519+ @ Override
520+ public void getAuthenticationDetails (AuthenticationContinuation authenticationContinuation , String userId ) {
521+
522+ }
523+
524+ @ Override
525+ public void getMFACode (MultiFactorAuthenticationContinuation continuation ) {
526+
527+ }
528+
529+ @ Override
530+ public void authenticationChallenge (ChallengeContinuation continuation ) {
531+
532+ }
533+
534+ @ Override
535+ public void onFailure (Exception exception ) {
536+ exception .printStackTrace ();
537+ fail ("Sign in failed." );
538+ }
539+ });
540+ revokeTokenLatch .await (5 , TimeUnit .SECONDS );
541+
542+ auth .signOut ();
543+ assertFalse ("isSignedIn is false" , auth .isSignedIn ());
544+
545+ try {
546+ user .revokeTokens ();
547+ } catch (Exception e ) {
548+ assertTrue (e instanceof InvalidParameterException );
549+ }
550+ }
551+
416552 @ Test
417553 public void testIdentityId () throws Exception {
418554 try {
@@ -524,6 +660,28 @@ public void testSignOut() throws Exception {
524660 }
525661 }
526662
663+ @ Test
664+ public void testSignedOutWithRevokeToken () throws Exception {
665+ auth .signIn (username , PASSWORD , null );
666+ assertTrue ("isSignedIn is true" , auth .isSignedIn ());
667+
668+ String tokenWithOriginJTI = "eyJraWQiOiIwTmxhQUhzbmtwQW5zbHBzUFhHWkJKcVJoR3E5WTkwckwweXpaWUV1OTJZPSIsImFsZyI6IlJTMjU2In0.eyJvcmlnaW5fanRpIjoiMzM2MWFkZDMtMDIwNS00NTY1LTk0MjQtMDQ3YWQ2N2Y0MjhmZWwifQ.a" ;
669+ setAccessToken (appContext , clientId , username , tokenWithOriginJTI );
670+ auth .signOut ();
671+ assertFalse ("isSignedIn is false" , auth .isSignedIn ());
672+ }
673+
674+ @ Test
675+ public void testSignedOutWithoutRevokeToken () throws Exception {
676+ auth .signIn (username , PASSWORD , null );
677+ assertTrue ("isSignedIn is true" , auth .isSignedIn ());
678+
679+ String tokenWithSub = "eyJraWQiOiJzU01EYmZyQ21pb3FrbEVRZFprNXl6UmszekxSTlo4aGlGMnlxdVFZbVM0PSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiI3YTQyNTFmMS04MmEyLTQxNzgtOWZhOS1mNmE3MTc1RCJ9.a" ;
680+ setAccessToken (appContext , clientId , username , tokenWithSub );
681+ auth .signOut ();
682+ assertFalse ("isSignedIn is false" , auth .isSignedIn ());
683+ }
684+
527685 @ Test (expected = com .amazonaws .services .cognitoidentityprovider .model .NotAuthorizedException .class )
528686 public void testSignInWrongPassword () throws Exception {
529687 AWSMobileClient .getInstance ().signIn (getPackageConfigure ().getString ("username" ), "wrong" , null );
0 commit comments