Skip to content

Commit f256672

Browse files
committed
browser: split browser-check from desktop session check
Split the web browser check from the desktop session checks. In future commits we will enhance the browser-check to take in to account the WSL special case.
1 parent d37c201 commit f256672

File tree

9 files changed

+44
-13
lines changed

9 files changed

+44
-13
lines changed

src/shared/Core/Authentication/MicrosoftAuthentication.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,14 @@ private void EnsureCanUseEmbeddedWebView()
508508
private bool CanUseSystemWebView(IPublicClientApplication app, Uri redirectUri)
509509
{
510510
// MSAL requires the application redirect URI is a loopback address to use the System WebView
511-
return Context.SessionManager.IsDesktopSession && app.IsSystemWebViewAvailable && redirectUri.IsLoopback;
511+
return Context.SessionManager.IsWebBrowserAvailable && app.IsSystemWebViewAvailable && redirectUri.IsLoopback;
512512
}
513513

514514
private void EnsureCanUseSystemWebView(IPublicClientApplication app, Uri redirectUri)
515515
{
516-
if (!Context.SessionManager.IsDesktopSession)
516+
if (!Context.SessionManager.IsWebBrowserAvailable)
517517
{
518-
throw new InvalidOperationException("System web view is not available without a desktop session.");
518+
throw new InvalidOperationException("System web view is not available without a way to start a browser.");
519519
}
520520

521521
if (!app.IsSystemWebViewAvailable)

src/shared/Core/BrowserUtils.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static void OpenDefaultBrowser(IEnvironment environment, Uri uri)
2828
ProcessStartInfo psi = null;
2929
if (PlatformUtils.IsLinux())
3030
{
31+
//
3132
// On Linux, 'shell execute' utilities like xdg-open launch a process without
3233
// detaching from the standard in/out descriptors. Some applications (like
3334
// Chromium) write messages to stdout, which is currently hooked up and being

src/shared/Core/EnvironmentBase.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,18 @@ public static string LocateExecutable(this IEnvironment environment, string prog
191191

192192
throw new Exception($"Failed to locate '{program}' executable on the path.");
193193
}
194+
195+
/// <summary>
196+
/// Retrieves the value of an environment variable from the current process.
197+
/// </summary>
198+
/// <param name="environment">The <see cref="IEnvironment"/>.</param>
199+
/// <param name="variable">The name of the environment variable.</param>
200+
/// <returns>
201+
/// The value of the environment variable specified by variable, or null if the environment variable is not found.
202+
/// </returns>
203+
public static string GetEnvironmentVariable(this IEnvironment environment, string variable)
204+
{
205+
return environment.Variables.TryGetValue(variable, out string value) ? value : null;
206+
}
194207
}
195208
}

src/shared/Core/ISessionManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,18 @@ public interface ISessionManager
77
/// </summary>
88
/// <returns>True if the session can display UI, false otherwise.</returns>
99
bool IsDesktopSession { get; }
10+
11+
/// <summary>
12+
/// Determine if the current session has access to a web browser.
13+
/// </summary>
14+
/// <returns>True if the session can display a web browser, false otherwise.</returns>
15+
bool IsWebBrowserAvailable { get; }
16+
}
17+
18+
public abstract class SessionManager : ISessionManager
19+
{
20+
public abstract bool IsDesktopSession { get; }
21+
22+
public virtual bool IsWebBrowserAvailable => IsDesktopSession;
1023
}
1124
}

src/shared/Core/Interop/Posix/PosixSessionManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace GitCredentialManager.Interop.Posix
44
{
5-
public class PosixSessionManager : ISessionManager
5+
public class PosixSessionManager : SessionManager
66
{
77
public PosixSessionManager()
88
{
99
PlatformUtils.EnsurePosix();
1010
}
1111

1212
// Check if we have an X11 or Wayland display environment available
13-
public virtual bool IsDesktopSession =>
13+
public override bool IsDesktopSession =>
1414
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("DISPLAY")) ||
1515
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("WAYLAND_DISPLAY"));
1616
}

src/shared/Core/Interop/Windows/WindowsSessionManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
namespace GitCredentialManager.Interop.Windows
55
{
6-
public class WindowsSessionManager : ISessionManager
6+
public class WindowsSessionManager : SessionManager
77
{
88
public WindowsSessionManager()
99
{
1010
PlatformUtils.EnsureWindows();
1111
}
1212

13-
public unsafe bool IsDesktopSession
13+
public override unsafe bool IsDesktopSession
1414
{
1515
get
1616
{

src/shared/GitHub/GitHubAuthentication.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public GitHubAuthentication(ICommandContext context)
6464

6565
public async Task<AuthenticationPromptResult> GetAuthenticationAsync(Uri targetUri, string userName, AuthenticationModes modes)
6666
{
67-
// If we don't have a desktop session/GUI then we cannot offer browser
68-
if (!Context.SessionManager.IsDesktopSession)
67+
// If we cannot start a browser then don't offer the option
68+
if (!Context.SessionManager.IsWebBrowserAvailable)
6969
{
7070
modes = modes & ~AuthenticationModes.Browser;
7171
}
@@ -257,8 +257,8 @@ public async Task<OAuth2TokenResult> GetOAuthTokenViaBrowserAsync(Uri targetUri,
257257

258258
var oauthClient = new GitHubOAuth2Client(HttpClient, Context.Settings, targetUri);
259259

260-
// We require a desktop session to launch the user's default web browser
261-
if (!Context.SessionManager.IsDesktopSession)
260+
// Can we launch the user's default web browser?
261+
if (!Context.SessionManager.IsWebBrowserAvailable)
262262
{
263263
throw new InvalidOperationException("Browser authentication requires a desktop session");
264264
}

src/shared/GitLab/GitLabAuthentication.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public GitLabAuthentication(ICommandContext context)
5555

5656
public async Task<AuthenticationPromptResult> GetAuthenticationAsync(Uri targetUri, string userName, AuthenticationModes modes)
5757
{
58-
// If we don't have a desktop session/GUI then we cannot offer browser
59-
if (!Context.SessionManager.IsDesktopSession)
58+
// If we cannot start a browser then don't offer the option
59+
if (!Context.SessionManager.IsWebBrowserAvailable)
6060
{
6161
modes = modes & ~AuthenticationModes.Browser;
6262
}

src/shared/TestInfrastructure/Objects/TestSessionManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ namespace GitCredentialManager.Tests.Objects
33
{
44
public class TestSessionManager : ISessionManager
55
{
6+
public bool? IsWebBrowserAvailableOverride { get; set; }
7+
68
public bool IsDesktopSession { get; set; }
9+
10+
bool ISessionManager.IsWebBrowserAvailable => IsWebBrowserAvailableOverride ?? IsDesktopSession;
711
}
812
}

0 commit comments

Comments
 (0)