Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save justindav1s/b8d142f5df82f617c12280d8e2aa256f to your computer and use it in GitHub Desktop.
Save justindav1s/b8d142f5df82f617c12280d8e2aa256f to your computer and use it in GitHub Desktop.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jnd.keycloak.web.client.controllers.TokenController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import java.util.Base64;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AuthCodeTests {
String realm = "psd2";
String baseUrl = "https://sso.datr.eu:8443";
String clientId = "Amazon";
String redirecturl= "http://127.0.0.1:8082/getcode";
String user = "test_user";
String password = "123456";
private Log log = LogFactory.getLog(TokenController.class);
@Test
public void getAuthCode() {
CloseableHttpClient httpClient
= HttpClients.custom()
.disableRedirectHandling()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpComponentsClientHttpRequestFactory requestFactory
= new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
log.info("Get Auth code");
String query_string = "scope=openid&response_type=code&client_id="+clientId+"&redirect_uri="+redirecturl;
log.info("query_string : "+query_string);
String uri = baseUrl+"/auth/realms/"+realm+"/protocol/openid-connect/auth";
log.info("Auth Code URL : "+uri);
uri = uri+"?"+query_string;
log.info("Auth Code URL : "+uri);
HttpHeaders headers = new HttpHeaders();
String creds = user+":"+password;
String base64Creds = new String(Base64.getEncoder().encode(creds.getBytes()));
log.info("base64Creds : "+base64Creds);
headers.add("Authorization", "Basic "+base64Creds);
HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<String> exchange =
restTemplate.exchange(
uri,
HttpMethod.GET,
request,
String.class);
HttpHeaders responseHeaders = exchange.getHeaders();
log.info("Location : "+responseHeaders.get("Location"));
}
}
@justindav1s
Copy link
Author

The HTTP Request factory is customised.

  1. To Operate against TLS endpoints with self-signed certs. By adding :

.setSSLHostnameVerifier(new NoopHostnameVerifier())

to the HTTPClient configuration.

  1. To Allow the 302 Redirect that come backs from Keycloak to be handled within the scope of this code. By Adding :

.disableRedirectHandling()

to the HTTPClient configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment