Skip to content

Commit

Permalink
Adds test for authentication with an invalid token
Browse files Browse the repository at this point in the history
Ensure that we return 401 when an invalid token is presented as
the Bearer token

Relates: elastic#38866
  • Loading branch information
jkakavas committed Aug 2, 2019
1 parent e765a00 commit afc9c6f
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,23 @@ public void testRefreshingToken() throws IOException {
.admin().cluster().prepareHealth().get());
}

public void testAuthenticateWithWrongToken() throws IOException {
final RestHighLevelClient restClient = new TestRestHighLevelClient();
CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant(
SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
assertNotNull(response.getRefreshToken());
// First check that the correct access token works by getting cluster health with token
assertNoTimeout(client()
.filterWithHeader(Collections.singletonMap("Authorization", "Bearer " + response.getAccessToken()))
.admin().cluster().prepareHealth().get());
// Now attempt to authenticate with an invalid access token string
RequestOptions wrongAuthOptions =
RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + randomAlphaOfLengthBetween(0, 128)).build();
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class,
() -> restClient.security().authenticate(wrongAuthOptions));
assertEquals(RestStatus.UNAUTHORIZED, e.status());
}

public void testRefreshingInvalidatedToken() throws IOException {
final RestHighLevelClient restClient = new TestRestHighLevelClient();
CreateTokenResponse createTokenResponse = restClient.security().createToken(CreateTokenRequest.passwordGrant(
Expand Down

0 comments on commit afc9c6f

Please sign in to comment.