1919
2020package org .elasticsearch .client .documentation ;
2121
22+ import org .elasticsearch .ElasticsearchException ;
2223import org .elasticsearch .ElasticsearchStatusException ;
2324import org .elasticsearch .action .ActionListener ;
2425import org .elasticsearch .action .LatchedActionListener ;
@@ -1323,19 +1324,52 @@ public void testInvalidateToken() throws Exception {
13231324 String accessToken ;
13241325 String refreshToken ;
13251326 {
1326- // Setup user
1327+ // Setup users
13271328 final char [] password = "password" .toCharArray ();
1328- User invalidate_token_user = new User ("invalidate_token " , Collections .singletonList ("kibana_user" ));
1329- PutUserRequest putUserRequest = new PutUserRequest (invalidate_token_user , password , true , RefreshPolicy .IMMEDIATE );
1329+ User user = new User ("user " , Collections .singletonList ("kibana_user" ));
1330+ PutUserRequest putUserRequest = new PutUserRequest (user , password , true , RefreshPolicy .IMMEDIATE );
13301331 PutUserResponse putUserResponse = client .security ().putUser (putUserRequest , RequestOptions .DEFAULT );
13311332 assertTrue (putUserResponse .isCreated ());
13321333
1334+ User this_user = new User ("this_user" , Collections .singletonList ("kibana_user" ));
1335+ PutUserRequest putThisUserRequest = new PutUserRequest (this_user , password , true , RefreshPolicy .IMMEDIATE );
1336+ PutUserResponse putThisUserResponse = client .security ().putUser (putThisUserRequest , RequestOptions .DEFAULT );
1337+ assertTrue (putThisUserResponse .isCreated ());
1338+
1339+ User that_user = new User ("that_user" , Collections .singletonList ("kibana_user" ));
1340+ PutUserRequest putThatUserRequest = new PutUserRequest (that_user , password , true , RefreshPolicy .IMMEDIATE );
1341+ PutUserResponse putThatUserResponse = client .security ().putUser (putThatUserRequest , RequestOptions .DEFAULT );
1342+ assertTrue (putThatUserResponse .isCreated ());
1343+
1344+ User other_user = new User ("other_user" , Collections .singletonList ("kibana_user" ));
1345+ PutUserRequest putOtherUserRequest = new PutUserRequest (other_user , password , true , RefreshPolicy .IMMEDIATE );
1346+ PutUserResponse putOtherUserResponse = client .security ().putUser (putOtherUserRequest , RequestOptions .DEFAULT );
1347+ assertTrue (putOtherUserResponse .isCreated ());
1348+
1349+ User extra_user = new User ("extra_user" , Collections .singletonList ("kibana_user" ));
1350+ PutUserRequest putExtraUserRequest = new PutUserRequest (extra_user , password , true , RefreshPolicy .IMMEDIATE );
1351+ PutUserResponse putExtraUserResponse = client .security ().putUser (putExtraUserRequest , RequestOptions .DEFAULT );
1352+ assertTrue (putExtraUserResponse .isCreated ());
1353+
13331354 // Create tokens
1334- final CreateTokenRequest createTokenRequest = CreateTokenRequest .passwordGrant ("invalidate_token " , password );
1355+ final CreateTokenRequest createTokenRequest = CreateTokenRequest .passwordGrant ("user " , password );
13351356 final CreateTokenResponse tokenResponse = client .security ().createToken (createTokenRequest , RequestOptions .DEFAULT );
13361357 accessToken = tokenResponse .getAccessToken ();
13371358 refreshToken = tokenResponse .getRefreshToken ();
1359+ final CreateTokenRequest createThisTokenRequest = CreateTokenRequest .passwordGrant ("this_user" , password );
1360+ final CreateTokenResponse thisTokenResponse = client .security ().createToken (createThisTokenRequest , RequestOptions .DEFAULT );
1361+ assertNotNull (thisTokenResponse );
1362+ final CreateTokenRequest createThatTokenRequest = CreateTokenRequest .passwordGrant ("that_user" , password );
1363+ final CreateTokenResponse thatTokenResponse = client .security ().createToken (createThatTokenRequest , RequestOptions .DEFAULT );
1364+ assertNotNull (thatTokenResponse );
1365+ final CreateTokenRequest createOtherTokenRequest = CreateTokenRequest .passwordGrant ("other_user" , password );
1366+ final CreateTokenResponse otherTokenResponse = client .security ().createToken (createOtherTokenRequest , RequestOptions .DEFAULT );
1367+ assertNotNull (otherTokenResponse );
1368+ final CreateTokenRequest createExtraTokenRequest = CreateTokenRequest .passwordGrant ("extra_user" , password );
1369+ final CreateTokenResponse extraTokenResponse = client .security ().createToken (createExtraTokenRequest , RequestOptions .DEFAULT );
1370+ assertNotNull (extraTokenResponse );
13381371 }
1372+
13391373 {
13401374 // tag::invalidate-access-token-request
13411375 InvalidateTokenRequest invalidateTokenRequest = InvalidateTokenRequest .accessToken (accessToken );
@@ -1347,15 +1381,54 @@ public void testInvalidateToken() throws Exception {
13471381 // end::invalidate-token-execute
13481382
13491383 // tag::invalidate-token-response
1350- boolean isCreated = invalidateTokenResponse .isCreated ();
1384+ final List <ElasticsearchException > errors = invalidateTokenResponse .getErrors ();
1385+ final int invalidatedTokens = invalidateTokenResponse .getInvalidatedTokens ();
1386+ final int previouslyInvalidatedTokens = invalidateTokenResponse .getPreviouslyInvalidatedTokens ();
13511387 // end::invalidate-token-response
1352- assertTrue (isCreated );
1388+ assertTrue (errors .isEmpty ());
1389+ assertThat (invalidatedTokens , equalTo (1 ));
1390+ assertThat (previouslyInvalidatedTokens , equalTo (0 ));
13531391 }
13541392
13551393 {
13561394 // tag::invalidate-refresh-token-request
13571395 InvalidateTokenRequest invalidateTokenRequest = InvalidateTokenRequest .refreshToken (refreshToken );
13581396 // end::invalidate-refresh-token-request
1397+ InvalidateTokenResponse invalidateTokenResponse =
1398+ client .security ().invalidateToken (invalidateTokenRequest , RequestOptions .DEFAULT );
1399+ assertTrue (invalidateTokenResponse .getErrors ().isEmpty ());
1400+ assertThat (invalidateTokenResponse .getInvalidatedTokens (), equalTo (1 ));
1401+ assertThat (invalidateTokenResponse .getPreviouslyInvalidatedTokens (), equalTo (0 ));
1402+ }
1403+
1404+ {
1405+ // tag::invalidate-user-tokens-request
1406+ InvalidateTokenRequest invalidateTokenRequest = InvalidateTokenRequest .userTokens ("other_user" );
1407+ // end::invalidate-user-tokens-request
1408+ InvalidateTokenResponse invalidateTokenResponse =
1409+ client .security ().invalidateToken (invalidateTokenRequest , RequestOptions .DEFAULT );
1410+ assertTrue (invalidateTokenResponse .getErrors ().isEmpty ());
1411+ // We have one refresh and one access token for that user
1412+ assertThat (invalidateTokenResponse .getInvalidatedTokens (), equalTo (2 ));
1413+ assertThat (invalidateTokenResponse .getPreviouslyInvalidatedTokens (), equalTo (0 ));
1414+ }
1415+
1416+ {
1417+ // tag::invalidate-user-realm-tokens-request
1418+ InvalidateTokenRequest invalidateTokenRequest = new InvalidateTokenRequest (null , null , "default_native" , "extra_user" );
1419+ // end::invalidate-user-realm-tokens-request
1420+ InvalidateTokenResponse invalidateTokenResponse =
1421+ client .security ().invalidateToken (invalidateTokenRequest , RequestOptions .DEFAULT );
1422+ assertTrue (invalidateTokenResponse .getErrors ().isEmpty ());
1423+ // We have one refresh and one access token for that user in this realm
1424+ assertThat (invalidateTokenResponse .getInvalidatedTokens (), equalTo (2 ));
1425+ assertThat (invalidateTokenResponse .getPreviouslyInvalidatedTokens (), equalTo (0 ));
1426+ }
1427+
1428+ {
1429+ // tag::invalidate-realm-tokens-request
1430+ InvalidateTokenRequest invalidateTokenRequest = InvalidateTokenRequest .realmTokens ("default_native" );
1431+ // end::invalidate-realm-tokens-request
13591432
13601433 ActionListener <InvalidateTokenResponse > listener ;
13611434 //tag::invalidate-token-execute-listener
@@ -1385,8 +1458,10 @@ public void onFailure(Exception e) {
13851458
13861459 final InvalidateTokenResponse response = future .get (30 , TimeUnit .SECONDS );
13871460 assertNotNull (response );
1388- assertTrue (response .isCreated ());// technically, this should be false, but the API is broken
1389- // See https://github.com/elastic/elasticsearch/issues/35115
1461+ assertTrue (response .getErrors ().isEmpty ());
1462+ //We still have 4 tokens ( 2 access_tokens and 2 refresh_tokens ) for the default_native realm
1463+ assertThat (response .getInvalidatedTokens (), equalTo (4 ));
1464+ assertThat (response .getPreviouslyInvalidatedTokens (), equalTo (0 ));
13901465 }
13911466 }
13921467
0 commit comments