Skip to content

Commit

Permalink
Allow Microsoft authentication to use xdg-open on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehog1029 committed Jan 4, 2022
1 parent 28017e8 commit 291f62a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
23 changes: 13 additions & 10 deletions launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,23 +105,25 @@ 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) {
log.log(Level.WARNING, "Malformed URL; this is a programming error!", e);
}
}

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.
Expand Down

0 comments on commit 291f62a

Please sign in to comment.