1919import com .carrotsearch .randomizedtesting .annotations .ThreadLeakScope ;
2020import org .apache .hc .core5 .http .Header ;
2121import org .apache .hc .core5 .http .message .BasicHeader ;
22+ import org .junit .Assert ;
2223import org .junit .ClassRule ;
2324import org .junit .Test ;
2425import org .junit .runner .RunWith ;
@@ -55,8 +56,15 @@ public class OnBehalfOfJwtAuthenticationTest {
5556 private static final String encryptionKey = Base64 .getEncoder ().encodeToString ("encryptionKey" .getBytes (StandardCharsets .UTF_8 ));
5657 public static final String ADMIN_USER_NAME = "admin" ;
5758 public static final String DEFAULT_PASSWORD = "secret" ;
59+ public static final String NEW_PASSWORD = "testPassword123!!" ;
5860 public static final String OBO_TOKEN_REASON = "{\" reason\" :\" Test generation\" }" ;
5961 public static final String OBO_ENDPOINT_PREFIX = "_plugins/_security/api/user/onbehalfof" ;
62+ public static final String OBO_REASON = "{\" reason\" :\" Testing\" , \" service\" :\" self-issued\" }" ;
63+ public static final String CURRENT_AND_NEW_PASSWORDS = "{ \" current_password\" : \" "
64+ + DEFAULT_PASSWORD
65+ + "\" , \" password\" : \" "
66+ + NEW_PASSWORD
67+ + "\" }" ;
6068
6169 @ ClassRule
6270 public static final LocalCluster cluster = new LocalCluster .Builder ().clusterManager (ClusterManager .SINGLENODE )
@@ -76,60 +84,62 @@ public class OnBehalfOfJwtAuthenticationTest {
7684
7785 @ Test
7886 public void shouldAuthenticateWithOBOTokenEndPoint () {
79- Header adminOboAuthHeader ;
80-
81- try (TestRestClient client = cluster .getRestClient (ADMIN_USER_NAME , DEFAULT_PASSWORD )) {
82-
83- client .assertCorrectCredentials (ADMIN_USER_NAME );
84-
85- TestRestClient .HttpResponse response = client .postJson (OBO_ENDPOINT_PREFIX , OBO_TOKEN_REASON );
86- response .assertStatusCode (200 );
87-
88- Map <String , Object > oboEndPointResponse = response .getBodyAs (Map .class );
89- assertThat (oboEndPointResponse , allOf (aMapWithSize (3 ), hasKey ("user" ), hasKey ("onBehalfOfToken" ), hasKey ("duration" )));
87+ String oboToken = generateOboToken (ADMIN_USER_NAME , DEFAULT_PASSWORD );
88+ Header adminOboAuthHeader = new BasicHeader ("Authorization" , "Bearer " + oboToken );
89+ authenticateWithOboToken (adminOboAuthHeader , ADMIN_USER_NAME , 200 );
90+ }
9091
91- String encodedOboTokenStr = oboEndPointResponse .get ("onBehalfOfToken" ).toString ();
92+ @ Test
93+ public void shouldNotAuthenticateWithATemperedOBOToken () {
94+ String oboToken = generateOboToken (ADMIN_USER_NAME , DEFAULT_PASSWORD );
95+ oboToken = oboToken .substring (0 , oboToken .length () - 1 ); // tampering the token
96+ Header adminOboAuthHeader = new BasicHeader ("Authorization" , "Bearer " + oboToken );
97+ authenticateWithOboToken (adminOboAuthHeader , ADMIN_USER_NAME , 401 );
98+ }
9299
93- adminOboAuthHeader = new BasicHeader ("Authorization" , "Bearer " + encodedOboTokenStr );
94- }
100+ @ Test
101+ public void shouldNotAuthenticateForUsingOBOTokenToAccessOBOEndpoint () {
102+ String oboToken = generateOboToken (ADMIN_USER_NAME , DEFAULT_PASSWORD );
103+ Header adminOboAuthHeader = new BasicHeader ("Authorization" , "Bearer " + oboToken );
95104
96105 try (TestRestClient client = cluster .getRestClient (adminOboAuthHeader )) {
97-
98- TestRestClient .HttpResponse response = client .getAuthInfo ();
99- response .assertStatusCode (200 );
100-
101- String username = response .getTextFromJsonBody (POINTER_USERNAME );
102- assertThat (username , equalTo (ADMIN_USER_NAME ));
106+ TestRestClient .HttpResponse response = client .getOBOTokenFromOboEndpoint (OBO_REASON , adminOboAuthHeader );
107+ response .assertStatusCode (401 );
103108 }
104109 }
105110
106111 @ Test
107- public void shouldNotAuthenticateWithATemperedOBOToken () {
108- Header adminOboAuthHeader ;
112+ public void shouldNotAuthenticateForUsingOBOTokenToAccessAccountEndpoint () {
113+ String oboToken = generateOboToken (ADMIN_USER_NAME , DEFAULT_PASSWORD );
114+ Header adminOboAuthHeader = new BasicHeader ("Authorization" , "Bearer " + oboToken );
109115
110- try (TestRestClient client = cluster .getRestClient (ADMIN_USER_NAME , DEFAULT_PASSWORD )) {
111-
112- client .assertCorrectCredentials (ADMIN_USER_NAME );
116+ try (TestRestClient client = cluster .getRestClient (adminOboAuthHeader )) {
117+ TestRestClient .HttpResponse response = client .changeInternalUserPassword (CURRENT_AND_NEW_PASSWORDS , adminOboAuthHeader );
118+ response .assertStatusCode (401 );
119+ }
120+ }
113121
122+ private String generateOboToken (String username , String password ) {
123+ try (TestRestClient client = cluster .getRestClient (username , password )) {
124+ client .assertCorrectCredentials (username );
114125 TestRestClient .HttpResponse response = client .postJson (OBO_ENDPOINT_PREFIX , OBO_TOKEN_REASON );
115126 response .assertStatusCode (200 );
116-
117127 Map <String , Object > oboEndPointResponse = response .getBodyAs (Map .class );
118128 assertThat (oboEndPointResponse , allOf (aMapWithSize (3 ), hasKey ("user" ), hasKey ("onBehalfOfToken" ), hasKey ("duration" )));
119-
120- String encodedOboTokenStr = oboEndPointResponse .get ("onBehalfOfToken" ).toString ();
121- StringBuilder stringBuilder = new StringBuilder (encodedOboTokenStr );
122- stringBuilder .deleteCharAt (encodedOboTokenStr .length () - 1 );
123- String temperedOboTokenStr = stringBuilder .toString ();
124-
125- adminOboAuthHeader = new BasicHeader ("Authorization" , "Bearer " + temperedOboTokenStr );
129+ return oboEndPointResponse .get ("onBehalfOfToken" ).toString ();
126130 }
131+ }
127132
128- try ( TestRestClient client = cluster . getRestClient ( adminOboAuthHeader ) ) {
129-
133+ private void authenticateWithOboToken ( Header authHeader , String expectedUsername , int expectedStatusCode ) {
134+ try ( TestRestClient client = cluster . getRestClient ( authHeader )) {
130135 TestRestClient .HttpResponse response = client .getAuthInfo ();
131- response .assertStatusCode (401 );
132- response .getBody ().contains ("Unauthorized" );
136+ response .assertStatusCode (expectedStatusCode );
137+ if (expectedStatusCode == 200 ) {
138+ String username = response .getTextFromJsonBody (POINTER_USERNAME );
139+ assertThat (username , equalTo (expectedUsername ));
140+ } else {
141+ Assert .assertTrue (response .getBody ().contains ("Unauthorized" ));
142+ }
133143 }
134144 }
135145}
0 commit comments