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 <jmaury@redhat.com>
This commit is contained in:
parent
355a5d65fb
commit
1be81bff7a
1 changed files with 27 additions and 4 deletions
|
@ -107,6 +107,8 @@ public class KeycloakInstalled {
|
||||||
Pattern callbackPattern = Pattern.compile("callback\\s*=\\s*\"([^\"]+)\"");
|
Pattern callbackPattern = Pattern.compile("callback\\s*=\\s*\"([^\"]+)\"");
|
||||||
Pattern paramPattern = Pattern.compile("param=\"([^\"]+)\"\\s+label=\"([^\"]+)\"\\s+mask=(\\S+)");
|
Pattern paramPattern = Pattern.compile("param=\"([^\"]+)\"\\s+label=\"([^\"]+)\"\\s+mask=(\\S+)");
|
||||||
Pattern codePattern = Pattern.compile("code=([^&]+)");
|
Pattern codePattern = Pattern.compile("code=([^&]+)");
|
||||||
|
private CallbackListener callback;
|
||||||
|
private DesktopProvider desktopProvider = new DesktopProvider();
|
||||||
|
|
||||||
|
|
||||||
public KeycloakInstalled() {
|
public KeycloakInstalled() {
|
||||||
|
@ -194,7 +196,7 @@ public class KeycloakInstalled {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loginDesktop() throws IOException, VerificationException, OAuthErrorException, URISyntaxException, ServerRequest.HttpFailure, InterruptedException {
|
public void loginDesktop() throws IOException, VerificationException, OAuthErrorException, URISyntaxException, ServerRequest.HttpFailure, InterruptedException {
|
||||||
CallbackListener callback = new CallbackListener();
|
callback = new CallbackListener();
|
||||||
callback.start();
|
callback.start();
|
||||||
|
|
||||||
String redirectUri = getRedirectUri(callback);
|
String redirectUri = getRedirectUri(callback);
|
||||||
|
@ -203,7 +205,7 @@ public class KeycloakInstalled {
|
||||||
|
|
||||||
String authUrl = createAuthUrl(redirectUri, state, pkce);
|
String authUrl = createAuthUrl(redirectUri, state, pkce);
|
||||||
|
|
||||||
Desktop.getDesktop().browse(new URI(authUrl));
|
desktopProvider.browse(new URI(authUrl));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
callback.await();
|
callback.await();
|
||||||
|
@ -225,6 +227,12 @@ public class KeycloakInstalled {
|
||||||
status = Status.LOGGED_DESKTOP;
|
status = Status.LOGGED_DESKTOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
if (callback != null) {
|
||||||
|
callback.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected String createAuthUrl(String redirectUri, String state, Pkce pkce) {
|
protected String createAuthUrl(String redirectUri, String state, Pkce pkce) {
|
||||||
|
|
||||||
KeycloakUriBuilder builder = deployment.getAuthUrl().clone()
|
KeycloakUriBuilder builder = deployment.getAuthUrl().clone()
|
||||||
|
@ -265,7 +273,7 @@ public class KeycloakInstalled {
|
||||||
.queryParam("id_token_hint", idTokenString)
|
.queryParam("id_token_hint", idTokenString)
|
||||||
.build().toString();
|
.build().toString();
|
||||||
|
|
||||||
Desktop.getDesktop().browse(new URI(logoutUrl));
|
desktopProvider.browse(new URI(logoutUrl));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
callback.await();
|
callback.await();
|
||||||
|
@ -623,8 +631,12 @@ public class KeycloakInstalled {
|
||||||
return tokenResponse;
|
return tokenResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDesktopProvider(DesktopProvider desktopProvider) {
|
||||||
|
this.desktopProvider = desktopProvider;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isDesktopSupported() {
|
public boolean isDesktopSupported() {
|
||||||
return Desktop.isDesktopSupported();
|
return desktopProvider.isDesktopSupported();
|
||||||
}
|
}
|
||||||
|
|
||||||
public KeycloakDeployment getDeployment() {
|
public KeycloakDeployment getDeployment() {
|
||||||
|
@ -685,6 +697,7 @@ public class KeycloakInstalled {
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
// it is OK to happen if thread is modified while stopping the server, specially when a security manager is enabled
|
// 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() {
|
public int getLocalPort() {
|
||||||
|
@ -772,4 +785,14 @@ public class KeycloakInstalled {
|
||||||
return Base64Url.encode(md.digest());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue