Skip to content

Commit fe7e69c

Browse files
authored
Merge pull request #651 from AzureAD/SJAIN/support-for-mac-unix-linux
Issue 447 - support for macOs, Linux and Unix
2 parents 60eb345 + e215337 commit fe7e69c

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

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

Lines changed: 76 additions & 2 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,8 +115,38 @@ 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+
}
136+
137+
private void openDefaultSystemBrowser(URL url){
138+
if (OSHelper.isWindows()) { //windows
139+
openDefaultSystemBrowserInWindows(url);
140+
} else if (OSHelper.isMac()) { // mac os
141+
openDefaultSystemBrowserInMac(url);
142+
} else if (OSHelper.isLinux()) { //linux or unix os
143+
openDefaultSystemBrowserInLinux(url);
144+
} else {
145+
throw new UnsupportedOperationException(OSHelper.getOs() + "Operating system not supported exception.");
146+
}
147+
}
109148

110-
private void openDefaultSystemBrowser(URL url) {
149+
private static void openDefaultSystemBrowserInWindows(URL url){
111150
try {
112151
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
113152
Desktop.getDesktop().browse(url.toURI());
@@ -121,6 +160,41 @@ private void openDefaultSystemBrowser(URL url) {
121160
}
122161
}
123162

163+
private static void openDefaultSystemBrowserInMac(URL url){
164+
Runtime runtime = Runtime.getRuntime();
165+
try {
166+
runtime.exec("open " + url);
167+
} catch (IOException e) {
168+
throw new RuntimeException(e);
169+
}
170+
}
171+
172+
private static void openDefaultSystemBrowserInLinux(URL url){
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);
195+
}
196+
}
197+
124198
private AuthorizationResult getAuthorizationResultFromHttpListener() {
125199
AuthorizationResult result = null;
126200
try {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.microsoft.aad.msal4j;
2+
3+
public class OSHelper {
4+
enum OSType{
5+
MAC,
6+
WINDOWS,
7+
LINUX
8+
}
9+
10+
private static final String OS;
11+
private static OSType osType;
12+
13+
static{
14+
OS = System.getProperty("os.name").toLowerCase();
15+
if(OS.contains("windows")){
16+
osType = OSType.WINDOWS;
17+
}else if (OS.contains("mac")){
18+
osType = OSType.MAC;
19+
}else if (OS.contains("nux") || OS.contains("nix")){
20+
osType = OSType.LINUX;
21+
}
22+
}
23+
24+
public static String getOs(){
25+
return OS;
26+
}
27+
28+
public static boolean isMac(){
29+
return OSType.MAC.equals(osType);
30+
}
31+
32+
public static boolean isWindows(){
33+
return OSType.WINDOWS.equals(osType);
34+
}
35+
36+
public static boolean isLinux(){
37+
return OSType.LINUX.equals(osType);
38+
}
39+
}

0 commit comments

Comments
 (0)