|
7 | 7 | import org.slf4j.LoggerFactory; |
8 | 8 |
|
9 | 9 | import java.awt.*; |
| 10 | +import java.io.File; |
10 | 11 | import java.io.IOException; |
11 | 12 | import java.net.URI; |
12 | 13 | import java.net.URISyntaxException; |
13 | 14 | import java.net.URL; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
14 | 17 | import java.util.concurrent.BlockingQueue; |
15 | 18 | import java.util.concurrent.LinkedBlockingQueue; |
16 | 19 | import java.util.concurrent.TimeUnit; |
17 | 20 |
|
18 | 21 | class AcquireTokenByInteractiveFlowSupplier extends AuthenticationResultSupplier { |
19 | 22 |
|
20 | | - private final static Logger LOG = LoggerFactory.getLogger(AcquireTokenByAuthorizationGrantSupplier.class); |
| 23 | + private static final Logger LOG = LoggerFactory.getLogger(AcquireTokenByInteractiveFlowSupplier.class); |
21 | 24 |
|
22 | 25 | private PublicClientApplication clientApplication; |
23 | 26 | private InteractiveRequest interactiveRequest; |
24 | 27 |
|
25 | 28 | private BlockingQueue<AuthorizationResult> authorizationResultQueue; |
26 | 29 | private HttpListener httpListener; |
27 | 30 |
|
| 31 | + /**MSAL tried to open the browser on Linux using the xdg-open, gnome-open, or kfmclient tools, but failed. |
| 32 | + Make sure you can open a page using xdg-open tool. See <a href="https://aka.ms/msal-net-os-browser">...</a> for details. */ |
| 33 | + public static final String LINUX_XDG_OPEN = "linux_xdg_open_failed"; |
| 34 | + |
| 35 | + 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."; |
| 36 | + |
28 | 37 | AcquireTokenByInteractiveFlowSupplier(PublicClientApplication clientApplication, |
29 | 38 | InteractiveRequest request) { |
30 | 39 | super(clientApplication, request); |
@@ -106,15 +115,34 @@ private void updateRedirectUrl() { |
106 | 115 | AuthenticationErrorCode.INVALID_REDIRECT_URI); |
107 | 116 | } |
108 | 117 | } |
| 118 | + private static List<String> getOpenToolsLinux() { |
| 119 | + return Arrays.asList("xdg-open", "gnome-open", "kfmclient", "microsoft-edge", "wslview"); |
| 120 | + } |
| 121 | + |
| 122 | + private static String getExecutablePath(String executable) { |
| 123 | + String pathEnvVar = System.getenv("PATH"); |
| 124 | + if (pathEnvVar != null) { |
| 125 | + String[] paths = pathEnvVar.split(File |
| 126 | + .pathSeparator); |
| 127 | + for (String basePath : paths) { |
| 128 | + String path = basePath + File.separator + executable; |
| 129 | + if (new File(path).exists()) { |
| 130 | + return path; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return null; |
| 135 | + } |
109 | 136 |
|
110 | | - private void openDefaultSystemBrowser(URL url) { |
111 | | - String os = System.getProperty("os.name").toLowerCase(); |
112 | | - if (os.contains("windows")) { |
| 137 | + private void openDefaultSystemBrowser(URL url){ |
| 138 | + if (OSHelper.isWindows()) { //windows |
113 | 139 | openDefaultSystemBrowserInWindows(url); |
114 | | - } else if (os.contains("mac")) { // mac os |
| 140 | + } else if (OSHelper.isMac()) { // mac os |
115 | 141 | openDefaultSystemBrowserInMac(url); |
116 | | - } else if(os.contains("nux") || os.contains("nix")) { //linux or unix os |
| 142 | + } else if (OSHelper.isLinux()) { //linux or unix os |
117 | 143 | openDefaultSystemBrowserInLinux(url); |
| 144 | + } else { |
| 145 | + throw new UnsupportedOperationException(OSHelper.getOs() + "Operating system not supported exception."); |
118 | 146 | } |
119 | 147 | } |
120 | 148 |
|
@@ -142,11 +170,28 @@ private static void openDefaultSystemBrowserInMac(URL url){ |
142 | 170 | } |
143 | 171 |
|
144 | 172 | private static void openDefaultSystemBrowserInLinux(URL url){ |
145 | | - Runtime runtime = Runtime.getRuntime(); |
146 | | - try { |
147 | | - runtime.exec("xdg-open " + url); |
148 | | - } catch (IOException e) { |
149 | | - throw new RuntimeException(e); |
| 173 | + String sudoUser = System.getenv("SUDO_USER"); |
| 174 | + if (sudoUser != null && !sudoUser.isEmpty()) { |
| 175 | + throw new MsalClientException(LINUX_XDG_OPEN, LINUX_OPEN_AS_SUDO_NOT_SUPPORTED); |
| 176 | + } |
| 177 | + |
| 178 | + boolean opened = false; |
| 179 | + List<String> openTools = getOpenToolsLinux(); |
| 180 | + for (String openTool : openTools) { |
| 181 | + String openToolPath = getExecutablePath(openTool); |
| 182 | + if (openToolPath != null) { |
| 183 | + Runtime runtime = Runtime.getRuntime(); |
| 184 | + try { |
| 185 | + runtime.exec(openTool + url); |
| 186 | + } catch (IOException e) { |
| 187 | + throw new RuntimeException(e); |
| 188 | + } |
| 189 | + opened = true; |
| 190 | + break; |
| 191 | + } |
| 192 | + } |
| 193 | + if (!opened) { |
| 194 | + throw new MsalClientException(LINUX_XDG_OPEN, LINUX_OPEN_AS_SUDO_NOT_SUPPORTED); |
150 | 195 | } |
151 | 196 | } |
152 | 197 |
|
|
0 commit comments