Skip to content

Commit 07ef572

Browse files
committed
Feedback incorporation
1 parent 302a0b2 commit 07ef572

File tree

2 files changed

+94
-11
lines changed

2 files changed

+94
-11
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,33 @@
77
import org.slf4j.LoggerFactory;
88

99
import java.awt.*;
10+
import java.io.File;
1011
import java.io.IOException;
1112
import java.net.URI;
1213
import java.net.URISyntaxException;
1314
import java.net.URL;
15+
import java.util.Arrays;
16+
import java.util.List;
1417
import java.util.concurrent.BlockingQueue;
1518
import java.util.concurrent.LinkedBlockingQueue;
1619
import java.util.concurrent.TimeUnit;
1720

1821
class AcquireTokenByInteractiveFlowSupplier extends AuthenticationResultSupplier {
1922

20-
private final static Logger LOG = LoggerFactory.getLogger(AcquireTokenByAuthorizationGrantSupplier.class);
23+
private static final Logger LOG = LoggerFactory.getLogger(AcquireTokenByInteractiveFlowSupplier.class);
2124

2225
private PublicClientApplication clientApplication;
2326
private InteractiveRequest interactiveRequest;
2427

2528
private BlockingQueue<AuthorizationResult> authorizationResultQueue;
2629
private HttpListener httpListener;
2730

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+
2837
AcquireTokenByInteractiveFlowSupplier(PublicClientApplication clientApplication,
2938
InteractiveRequest request) {
3039
super(clientApplication, request);
@@ -106,15 +115,34 @@ private void updateRedirectUrl() {
106115
AuthenticationErrorCode.INVALID_REDIRECT_URI);
107116
}
108117
}
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+
}
109136

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
113139
openDefaultSystemBrowserInWindows(url);
114-
} else if (os.contains("mac")) { // mac os
140+
} else if (OSHelper.isMac()) { // mac os
115141
openDefaultSystemBrowserInMac(url);
116-
} else if(os.contains("nux") || os.contains("nix")) { //linux or unix os
142+
} else if (OSHelper.isLinux()) { //linux or unix os
117143
openDefaultSystemBrowserInLinux(url);
144+
} else {
145+
throw new UnsupportedOperationException(OSHelper.getOs() + "Operating system not supported exception.");
118146
}
119147
}
120148

@@ -142,11 +170,28 @@ private static void openDefaultSystemBrowserInMac(URL url){
142170
}
143171

144172
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);
150195
}
151196
}
152197

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.microsoft.aad.msal4j;
2+
3+
public class OSHelper {
4+
5+
private static String os;
6+
private static boolean mac;
7+
private static boolean windows;
8+
private static boolean linux;
9+
10+
static{
11+
os = System.getProperty("os.name").toLowerCase();
12+
if(os.contains("windows")){
13+
windows = true;
14+
}else if (os.contains("mac")){
15+
mac = true;
16+
}else if (os.contains("nux") || os.contains("nix")){
17+
linux = true;
18+
}
19+
}
20+
21+
public static String getOs(){
22+
return os;
23+
}
24+
25+
public static boolean isMac(){
26+
return mac;
27+
}
28+
29+
public static boolean isWindows(){
30+
return windows;
31+
}
32+
33+
public static boolean isLinux(){
34+
return linux;
35+
}
36+
37+
38+
}

0 commit comments

Comments
 (0)