From 291f62a4dcf6686696fb01de8133a355ca5172d2 Mon Sep 17 00:00:00 2001 From: Henry Le Grys Date: Tue, 4 Jan 2022 00:41:53 +0000 Subject: [PATCH] Allow Microsoft authentication to use xdg-open on Linux --- .../microsoft/MicrosoftWebAuthorizer.java | 3 ++- .../skcraft/launcher/swing/SwingHelper.java | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/launcher/src/main/java/com/skcraft/launcher/auth/microsoft/MicrosoftWebAuthorizer.java b/launcher/src/main/java/com/skcraft/launcher/auth/microsoft/MicrosoftWebAuthorizer.java index eb07245b9..a4d79535c 100644 --- a/launcher/src/main/java/com/skcraft/launcher/auth/microsoft/MicrosoftWebAuthorizer.java +++ b/launcher/src/main/java/com/skcraft/launcher/auth/microsoft/MicrosoftWebAuthorizer.java @@ -1,6 +1,7 @@ package com.skcraft.launcher.auth.microsoft; import com.skcraft.launcher.auth.AuthenticationException; +import com.skcraft.launcher.swing.SwingHelper; import com.skcraft.launcher.util.HttpRequest; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -30,7 +31,7 @@ public OauthResult authorize() throws IOException, AuthenticationException, Inte private OauthResult authorizeInteractive() throws IOException, AuthenticationException, InterruptedException { OauthHttpHandler httpHandler = new OauthHttpHandler(); - Desktop.getDesktop().browse(generateInteractiveUrl(httpHandler.getPort())); + SwingHelper.openURL(generateInteractiveUrl(httpHandler.getPort())); return httpHandler.await(); } diff --git a/launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java b/launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java index 8024e0b2f..b8239dfe3 100644 --- a/launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java +++ b/launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java @@ -35,6 +35,7 @@ import java.io.*; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.util.List; @@ -104,16 +105,7 @@ public static void openURL(@NonNull String url, @NonNull Component parentCompone */ public static void openURL(URL url, Component parentComponent) { try { - Desktop.getDesktop().browse(url.toURI()); - } catch (UnsupportedOperationException e) { - if (Environment.detectPlatform() == Platform.LINUX) { - // Try xdg-open instead - try { - Runtime.getRuntime().exec(new String[]{"xdg-open", url.toString()}); - } catch (IOException ex) { - showErrorDialog(parentComponent, tr("errors.openUrlError", url.toString()), tr("errorTitle"), ex); - } - } + openURL(url.toURI()); } catch (IOException e) { showErrorDialog(parentComponent, tr("errors.openUrlError", url.toString()), SharedLocale.tr("errorTitle")); } catch (URISyntaxException e) { @@ -121,6 +113,17 @@ public static void openURL(URL url, Component parentComponent) { } } + public static void openURL(URI url) throws IOException { + try { + Desktop.getDesktop().browse(url); + } catch (UnsupportedOperationException e) { + if (Environment.detectPlatform() == Platform.LINUX) { + // Try xdg-open instead + Runtime.getRuntime().exec(new String[]{"xdg-open", url.toString()}); + } + } + } + /** * Shows an popup error dialog, with potential extra details shown either immediately * or available on the dialog.