Skip to content

Commit

Permalink
KEYCLOAK-13721 Allow to configure host and port of callbackserver in …
Browse files Browse the repository at this point in the history
…KeycloakInstalled adapter
  • Loading branch information
thomasdarimont authored and mposolda committed Jul 29, 2020
1 parent 0191f91 commit 6806dfa
Showing 1 changed file with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ private enum Status {
LOGGED_MANUAL, LOGGED_DESKTOP
}

/**
* local port to listen for callbacks. The value {@code 0} will choose a random port.
*/
private int listenPort = 0;

/**
* local hostname to listen for callbacks.
*/
private String listenHostname = "localhost";

private AccessTokenResponse tokenResponse;
private String tokenString;
private String idTokenString;
Expand Down Expand Up @@ -124,6 +134,33 @@ public void setLocale(Locale locale) {
this.locale = locale;
}

public int getListenPort() {
return listenPort;
}

/**
* Configures the local port to listen for callbacks. The value {@code 0} will choose a random port. Defaults to {@code 0}.
* @param listenPort a valid port number
*/
public void setListenPort(int listenPort) {
if (listenPort < 0 || listenPort > 65535) {
throw new IllegalArgumentException("localPort");
}
this.listenPort = listenPort;
}

public String getListenHostname() {
return listenHostname;
}

/**
* Configures the local hostname to listen for callbacks. The value {@code 0} will choose a random port
* @param listenHostname a valid local hostname
*/
public void setListenHostname(String listenHostname) {
this.listenHostname = listenHostname;
}

public void login() throws IOException, ServerRequest.HttpFailure, VerificationException, InterruptedException, OAuthErrorException, URISyntaxException {
if (isDesktopSupported()) {
loginDesktop();
Expand Down Expand Up @@ -160,7 +197,7 @@ public void loginDesktop() throws IOException, VerificationException, OAuthError
CallbackListener callback = new CallbackListener();
callback.start();

String redirectUri = "http://localhost:" + callback.getLocalPort();
String redirectUri = String.format("http://%s:%s", getListenHostname(), callback.getLocalPort());
String state = UUID.randomUUID().toString();
Pkce pkce = deployment.isPkce() ? generatePkce() : null;

Expand Down Expand Up @@ -220,7 +257,7 @@ private void logoutDesktop() throws IOException, URISyntaxException, Interrupted
CallbackListener callback = new CallbackListener();
callback.start();

String redirectUri = "http://localhost:" + callback.getLocalPort();
String redirectUri = String.format("http://%s:%s", getListenHostname(), callback.getLocalPort());

String logoutUrl = deployment.getLogoutUrl()
.queryParam(OAuth2Constants.REDIRECT_URI, redirectUri)
Expand Down Expand Up @@ -629,7 +666,7 @@ public void start() {
server = Undertow.builder()
.setIoThreads(1)
.setWorkerThreads(1)
.addHttpListener(0, "localhost")
.addHttpListener(getListenPort(), getListenHostname())
.setHandler(gracefulShutdownHandler)
.build();

Expand Down

0 comments on commit 6806dfa

Please sign in to comment.