From 1be81bff7a10e931ec3e180b0b79d1a9b8c39a7b Mon Sep 17 00:00:00 2001 From: Jeff MAURY Date: Wed, 12 May 2021 14:46:00 +0200 Subject: [PATCH] KEYCLOAK-17400: allow installed adapter to be reused (#7853) * KEYCLOAK-17400: allow installed adapter to be reused Also add a close method to stop callback if response has not been received yet Signed-off-by: Jeff MAURY --- .../adapters/installed/KeycloakInstalled.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/adapters/oidc/installed/src/main/java/org/keycloak/adapters/installed/KeycloakInstalled.java b/adapters/oidc/installed/src/main/java/org/keycloak/adapters/installed/KeycloakInstalled.java index b383baa111..8632e7ec3a 100644 --- a/adapters/oidc/installed/src/main/java/org/keycloak/adapters/installed/KeycloakInstalled.java +++ b/adapters/oidc/installed/src/main/java/org/keycloak/adapters/installed/KeycloakInstalled.java @@ -107,6 +107,8 @@ public class KeycloakInstalled { Pattern callbackPattern = Pattern.compile("callback\\s*=\\s*\"([^\"]+)\""); Pattern paramPattern = Pattern.compile("param=\"([^\"]+)\"\\s+label=\"([^\"]+)\"\\s+mask=(\\S+)"); Pattern codePattern = Pattern.compile("code=([^&]+)"); + private CallbackListener callback; + private DesktopProvider desktopProvider = new DesktopProvider(); public KeycloakInstalled() { @@ -194,7 +196,7 @@ public class KeycloakInstalled { } public void loginDesktop() throws IOException, VerificationException, OAuthErrorException, URISyntaxException, ServerRequest.HttpFailure, InterruptedException { - CallbackListener callback = new CallbackListener(); + callback = new CallbackListener(); callback.start(); String redirectUri = getRedirectUri(callback); @@ -203,7 +205,7 @@ public class KeycloakInstalled { String authUrl = createAuthUrl(redirectUri, state, pkce); - Desktop.getDesktop().browse(new URI(authUrl)); + desktopProvider.browse(new URI(authUrl)); try { callback.await(); @@ -225,6 +227,12 @@ public class KeycloakInstalled { status = Status.LOGGED_DESKTOP; } + public void close() { + if (callback != null) { + callback.stop(); + } + } + protected String createAuthUrl(String redirectUri, String state, Pkce pkce) { KeycloakUriBuilder builder = deployment.getAuthUrl().clone() @@ -265,7 +273,7 @@ public class KeycloakInstalled { .queryParam("id_token_hint", idTokenString) .build().toString(); - Desktop.getDesktop().browse(new URI(logoutUrl)); + desktopProvider.browse(new URI(logoutUrl)); try { callback.await(); @@ -623,8 +631,12 @@ public class KeycloakInstalled { return tokenResponse; } + public void setDesktopProvider(DesktopProvider desktopProvider) { + this.desktopProvider = desktopProvider; + } + public boolean isDesktopSupported() { - return Desktop.isDesktopSupported(); + return desktopProvider.isDesktopSupported(); } public KeycloakDeployment getDeployment() { @@ -685,6 +697,7 @@ public class KeycloakInstalled { } catch (Exception ignore) { // it is OK to happen if thread is modified while stopping the server, specially when a security manager is enabled } + shutdownSignal.countDown(); } public int getLocalPort() { @@ -772,4 +785,14 @@ public class KeycloakInstalled { return Base64Url.encode(md.digest()); } } + + public static class DesktopProvider { + public boolean isDesktopSupported() { + return Desktop.isDesktopSupported(); + } + + public void browse(URI uri) throws IOException { + Desktop.getDesktop().browse(uri); + } + } }