From 302a0b2d4566a85f566612ade5e4883c86814b9b Mon Sep 17 00:00:00 2001 From: siddhijain Date: Wed, 10 May 2023 17:28:55 -0500 Subject: [PATCH 1/3] Issue 447 --- ...AcquireTokenByInteractiveFlowSupplier.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java b/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java index f9dadf71..281f2bfc 100644 --- a/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java +++ b/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java @@ -108,6 +108,17 @@ private void updateRedirectUrl() { } private void openDefaultSystemBrowser(URL url) { + String os = System.getProperty("os.name").toLowerCase(); + if (os.contains("windows")) { + openDefaultSystemBrowserInWindows(url); + } else if (os.contains("mac")) { // mac os + openDefaultSystemBrowserInMac(url); + } else if(os.contains("nux") || os.contains("nix")) { //linux or unix os + openDefaultSystemBrowserInLinux(url); + } + } + + private static void openDefaultSystemBrowserInWindows(URL url){ try { if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { Desktop.getDesktop().browse(url.toURI()); @@ -121,6 +132,24 @@ private void openDefaultSystemBrowser(URL url) { } } + private static void openDefaultSystemBrowserInMac(URL url){ + Runtime runtime = Runtime.getRuntime(); + try { + runtime.exec("open " + url); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static void openDefaultSystemBrowserInLinux(URL url){ + Runtime runtime = Runtime.getRuntime(); + try { + runtime.exec("xdg-open " + url); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + private AuthorizationResult getAuthorizationResultFromHttpListener() { AuthorizationResult result = null; try { From 07ef572bfa93f511aa9262955e8b97928a76dbcc Mon Sep 17 00:00:00 2001 From: siddhijain Date: Thu, 11 May 2023 14:35:11 -0500 Subject: [PATCH 2/3] Feedback incorporation --- ...AcquireTokenByInteractiveFlowSupplier.java | 67 ++++++++++++++++--- .../com/microsoft/aad/msal4j/OSHelper.java | 38 +++++++++++ 2 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OSHelper.java diff --git a/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java b/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java index 281f2bfc..8029c5d8 100644 --- a/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java +++ b/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java @@ -7,17 +7,20 @@ import org.slf4j.LoggerFactory; import java.awt.*; +import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.util.Arrays; +import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; class AcquireTokenByInteractiveFlowSupplier extends AuthenticationResultSupplier { - private final static Logger LOG = LoggerFactory.getLogger(AcquireTokenByAuthorizationGrantSupplier.class); + private static final Logger LOG = LoggerFactory.getLogger(AcquireTokenByInteractiveFlowSupplier.class); private PublicClientApplication clientApplication; private InteractiveRequest interactiveRequest; @@ -25,6 +28,12 @@ class AcquireTokenByInteractiveFlowSupplier extends AuthenticationResultSupplier private BlockingQueue authorizationResultQueue; private HttpListener httpListener; + /**MSAL tried to open the browser on Linux using the xdg-open, gnome-open, or kfmclient tools, but failed. + Make sure you can open a page using xdg-open tool. See ... for details. */ + public static final String LINUX_XDG_OPEN = "linux_xdg_open_failed"; + + public static final String LINUX_OPEN_AS_SUDO_NOT_SUPPORTED = "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools in sudo mode. Please run the process as non-sudo user."; + AcquireTokenByInteractiveFlowSupplier(PublicClientApplication clientApplication, InteractiveRequest request) { super(clientApplication, request); @@ -106,15 +115,34 @@ private void updateRedirectUrl() { AuthenticationErrorCode.INVALID_REDIRECT_URI); } } + private static List getOpenToolsLinux() { + return Arrays.asList("xdg-open", "gnome-open", "kfmclient", "microsoft-edge", "wslview"); + } + + private static String getExecutablePath(String executable) { + String pathEnvVar = System.getenv("PATH"); + if (pathEnvVar != null) { + String[] paths = pathEnvVar.split(File + .pathSeparator); + for (String basePath : paths) { + String path = basePath + File.separator + executable; + if (new File(path).exists()) { + return path; + } + } + } + return null; + } - private void openDefaultSystemBrowser(URL url) { - String os = System.getProperty("os.name").toLowerCase(); - if (os.contains("windows")) { + private void openDefaultSystemBrowser(URL url){ + if (OSHelper.isWindows()) { //windows openDefaultSystemBrowserInWindows(url); - } else if (os.contains("mac")) { // mac os + } else if (OSHelper.isMac()) { // mac os openDefaultSystemBrowserInMac(url); - } else if(os.contains("nux") || os.contains("nix")) { //linux or unix os + } else if (OSHelper.isLinux()) { //linux or unix os openDefaultSystemBrowserInLinux(url); + } else { + throw new UnsupportedOperationException(OSHelper.getOs() + "Operating system not supported exception."); } } @@ -142,11 +170,28 @@ private static void openDefaultSystemBrowserInMac(URL url){ } private static void openDefaultSystemBrowserInLinux(URL url){ - Runtime runtime = Runtime.getRuntime(); - try { - runtime.exec("xdg-open " + url); - } catch (IOException e) { - throw new RuntimeException(e); + String sudoUser = System.getenv("SUDO_USER"); + if (sudoUser != null && !sudoUser.isEmpty()) { + throw new MsalClientException(LINUX_XDG_OPEN, LINUX_OPEN_AS_SUDO_NOT_SUPPORTED); + } + + boolean opened = false; + List openTools = getOpenToolsLinux(); + for (String openTool : openTools) { + String openToolPath = getExecutablePath(openTool); + if (openToolPath != null) { + Runtime runtime = Runtime.getRuntime(); + try { + runtime.exec(openTool + url); + } catch (IOException e) { + throw new RuntimeException(e); + } + opened = true; + break; + } + } + if (!opened) { + throw new MsalClientException(LINUX_XDG_OPEN, LINUX_OPEN_AS_SUDO_NOT_SUPPORTED); } } diff --git a/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OSHelper.java b/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OSHelper.java new file mode 100644 index 00000000..68623699 --- /dev/null +++ b/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OSHelper.java @@ -0,0 +1,38 @@ +package com.microsoft.aad.msal4j; + +public class OSHelper { + + private static String os; + private static boolean mac; + private static boolean windows; + private static boolean linux; + + static{ + os = System.getProperty("os.name").toLowerCase(); + if(os.contains("windows")){ + windows = true; + }else if (os.contains("mac")){ + mac = true; + }else if (os.contains("nux") || os.contains("nix")){ + linux = true; + } + } + + public static String getOs(){ + return os; + } + + public static boolean isMac(){ + return mac; + } + + public static boolean isWindows(){ + return windows; + } + + public static boolean isLinux(){ + return linux; + } + + +} From e215337e910fadfd5872bffb0d4c9d616e1dbd78 Mon Sep 17 00:00:00 2001 From: siddhijain Date: Mon, 15 May 2023 14:30:04 -0500 Subject: [PATCH 3/3] enum for os type --- .../com/microsoft/aad/msal4j/OSHelper.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OSHelper.java b/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OSHelper.java index 68623699..b1609f5a 100644 --- a/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OSHelper.java +++ b/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OSHelper.java @@ -1,38 +1,39 @@ package com.microsoft.aad.msal4j; public class OSHelper { + enum OSType{ + MAC, + WINDOWS, + LINUX + } - private static String os; - private static boolean mac; - private static boolean windows; - private static boolean linux; + private static final String OS; + private static OSType osType; static{ - os = System.getProperty("os.name").toLowerCase(); - if(os.contains("windows")){ - windows = true; - }else if (os.contains("mac")){ - mac = true; - }else if (os.contains("nux") || os.contains("nix")){ - linux = true; + OS = System.getProperty("os.name").toLowerCase(); + if(OS.contains("windows")){ + osType = OSType.WINDOWS; + }else if (OS.contains("mac")){ + osType = OSType.MAC; + }else if (OS.contains("nux") || OS.contains("nix")){ + osType = OSType.LINUX; } } public static String getOs(){ - return os; + return OS; } public static boolean isMac(){ - return mac; + return OSType.MAC.equals(osType); } public static boolean isWindows(){ - return windows; + return OSType.WINDOWS.equals(osType); } public static boolean isLinux(){ - return linux; + return OSType.LINUX.equals(osType); } - - }