Skip to content

Commit

Permalink
Updates tests to adhere to the refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Sep 26, 2023
1 parent 18e377b commit 054dc73
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestRequest.Method;
Expand Down Expand Up @@ -141,7 +142,8 @@ public void basicTest() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -188,7 +190,8 @@ public void decryptAssertionsTest() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -236,7 +239,8 @@ public void shouldUnescapeSamlEntitiesTest() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -287,7 +291,8 @@ public void shouldUnescapeSamlEntitiesTest2() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -338,7 +343,8 @@ public void shouldNotEscapeSamlEntities() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -389,7 +395,8 @@ public void shouldNotTrimWhitespaceInJwtRoles() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -436,7 +443,8 @@ public void testMetadataBody() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -501,7 +509,8 @@ public void unsolicitedSsoTest() throws Exception {
);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -552,7 +561,8 @@ public void badUnsolicitedSsoTest() throws Exception {
);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

Assert.assertEquals(RestStatus.UNAUTHORIZED, tokenRestChannel.response.status());
}
Expand Down Expand Up @@ -584,7 +594,8 @@ public void wrongCertTest() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

Assert.assertEquals(401, tokenRestChannel.response.status().getStatus());
}
Expand Down Expand Up @@ -613,7 +624,8 @@ public void noSignatureTest() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

Assert.assertEquals(401, tokenRestChannel.response.status().getStatus());
}
Expand Down Expand Up @@ -646,7 +658,8 @@ public void rolesTest() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -693,7 +706,8 @@ public void idpEndpointWithQueryStringTest() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -747,7 +761,8 @@ private void commaSeparatedRoles(final String rolesAsString, final Settings.Buil
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand Down Expand Up @@ -850,7 +865,8 @@ public void initialConnectionFailureTest() throws Exception {

RestRequest restRequest = new FakeRestRequest(ImmutableMap.of(), new HashMap<String, String>());
TestRestChannel restChannel = new TestRestChannel(restRequest);
samlAuthenticator.reRequestAuthentication(restChannel, null);
BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(restRequest, null);
restChannel.sendResponse(authenticatorResponse);

Assert.assertNull(restChannel.response);

Expand All @@ -870,7 +886,8 @@ public void initialConnectionFailureTest() throws Exception {
RestRequest tokenRestRequest = buildTokenExchangeRestRequest(encodedSamlResponse, authenticateHeaders);
TestRestChannel tokenRestChannel = new TestRestChannel(tokenRestRequest);

samlAuthenticator.reRequestAuthentication(tokenRestChannel, null);
authenticatorResponse = samlAuthenticator.reRequestAuthentication(tokenRestRequest, null);
tokenRestChannel.sendResponse(authenticatorResponse);

String responseJson = new String(BytesReference.toBytes(tokenRestChannel.response.content()));
HashMap<String, Object> response = DefaultObjectMapper.objectMapper.readValue(
Expand All @@ -893,7 +910,8 @@ private AuthenticateHeaders getAutenticateHeaders(HTTPSamlAuthenticator samlAuth
RestRequest restRequest = new FakeRestRequest(ImmutableMap.of(), new HashMap<String, String>());
TestRestChannel restChannel = new TestRestChannel(restRequest);

samlAuthenticator.reRequestAuthentication(restChannel, null);
final BytesRestResponse authenticatorResponse = samlAuthenticator.reRequestAuthentication(restRequest, null);
restChannel.sendResponse(authenticatorResponse);

List<String> wwwAuthenticateHeaders = restChannel.response.getHeaders().get("WWW-Authenticate");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.opensearch.OpenSearchSecurityException;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.security.auth.HTTPAuthenticator;
import org.opensearch.security.user.AuthCredentials;
Expand All @@ -39,8 +39,8 @@ public AuthCredentials extractCredentials(RestRequest request, ThreadContext con
}

@Override
public boolean reRequestAuthentication(RestChannel channel, AuthCredentials credentials) {
return false;
public BytesRestResponse reRequestAuthentication(RestRequest request, AuthCredentials credentials) {
return null;
}

public static long getCount() {
Expand Down

0 comments on commit 054dc73

Please sign in to comment.