Skip to content

Commit ac5289a

Browse files
committed
msauth: use new Windows broker based on MSALRuntime
Use the new Windows broker which is based on the MSALRuntime; an export wrapper around a native, cross-platform MSAL library. In this new set up, we drop the `.Desktop` package in favour of the `.Broker` package that also means we drop the WebView2Loader.dll, which we didn't make use of anyway. There are a few new binaries to be distrubuted in the new model, including a P/Invoke layer, IdentityModel abstractions library, and the native msalruntime_x86.dll. Note that GCM still only support x86 on Windows, and only supports broker use on Windows. For this reason we don't bother adding the broker package on non-.NET Framework builds to keep the sizes on Mac/Linux to a minimium.
1 parent aecd7db commit ac5289a

File tree

5 files changed

+25
-110
lines changed

5 files changed

+25
-110
lines changed

src/shared/Core/Authentication/MicrosoftAuthentication.cs

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.Identity.Client.Extensions.Msal;
88

99
#if NETFRAMEWORK
10-
using Microsoft.Identity.Client.Desktop;
10+
using Microsoft.Identity.Client.Broker;
1111
#endif
1212

1313
namespace GitCredentialManager.Authentication
@@ -41,55 +41,6 @@ public class MicrosoftAuthentication : AuthenticationBase, IMicrosoftAuthenticat
4141
"live", "liveconnect", "liveid",
4242
};
4343

44-
#region Broker Initialization
45-
46-
public static bool IsBrokerInitialized { get; private set; }
47-
48-
public static void InitializeBroker()
49-
{
50-
if (IsBrokerInitialized)
51-
{
52-
return;
53-
}
54-
55-
IsBrokerInitialized = true;
56-
57-
// Broker is only supported on Windows 10 and later
58-
if (!PlatformUtils.IsWindowsBrokerSupported())
59-
{
60-
return;
61-
}
62-
63-
// Nothing to do when not an elevated user
64-
if (!PlatformUtils.IsElevatedUser())
65-
{
66-
return;
67-
}
68-
69-
// Lower COM security so that MSAL can make the calls to WAM
70-
int result = Interop.Windows.Native.Ole32.CoInitializeSecurity(
71-
IntPtr.Zero,
72-
-1,
73-
IntPtr.Zero,
74-
IntPtr.Zero,
75-
Interop.Windows.Native.Ole32.RpcAuthnLevel.None,
76-
Interop.Windows.Native.Ole32.RpcImpLevel.Impersonate,
77-
IntPtr.Zero,
78-
Interop.Windows.Native.Ole32.EoAuthnCap.None,
79-
IntPtr.Zero
80-
);
81-
82-
if (result != 0)
83-
{
84-
throw new Exception(
85-
$"Failed to set COM process security to allow Windows broker from an elevated process (0x{result:x})." +
86-
Environment.NewLine +
87-
$"See {Constants.HelpUrls.GcmWamComSecurity} for more information.");
88-
}
89-
}
90-
91-
#endregion
92-
9344
public MicrosoftAuthentication(ICommandContext context)
9445
: base(context) { }
9546

@@ -99,17 +50,10 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenAsync(
9950
string authority, string clientId, Uri redirectUri, string[] scopes, string userName)
10051
{
10152
// Check if we can and should use OS broker authentication
102-
bool useBroker = false;
103-
if (CanUseBroker(Context))
104-
{
105-
// Can only use the broker if it has been initialized
106-
useBroker = IsBrokerInitialized;
107-
108-
if (IsBrokerInitialized)
109-
Context.Trace.WriteLine("OS broker is available and enabled.");
110-
else
111-
Context.Trace.WriteLine("OS broker has not been initialized and cannot not be used.");
112-
}
53+
bool useBroker = CanUseBroker();
54+
Context.Trace.WriteLine(useBroker
55+
? "OS broker is available and enabled."
56+
: "OS broker has not been initialized and cannot not be used.");
11357

11458
// Create the public client application for authentication
11559
IPublicClientApplication app = await CreatePublicClientApplicationAsync(authority, clientId, redirectUri, useBroker);
@@ -287,12 +231,19 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
287231
appBuilder.WithParentActivityOrWindow(() => new IntPtr(hWndInt));
288232
}
289233

290-
// On Windows 10+ & .NET Framework try and use the WAM broker
291-
if (enableBroker && PlatformUtils.IsWindowsBrokerSupported())
234+
// Configure the broker if enabled
235+
// Currently only supported on Windows so only included in the .NET Framework builds
236+
// to save on the distribution size of the .NET builds (no need for MSALRuntime bits).
237+
if (enableBroker)
292238
{
293239
#if NETFRAMEWORK
294-
appBuilder.WithExperimentalFeatures();
295-
appBuilder.WithWindowsBroker();
240+
appBuilder.WithBroker(
241+
new BrokerOptions(BrokerOptions.OperatingSystems.Windows)
242+
{
243+
Title = "Git Credential Manager",
244+
MsaPassthrough = true,
245+
}
246+
);
296247
#endif
297248
}
298249

@@ -458,19 +409,19 @@ public HttpClient GetHttpClient()
458409

459410
#region Auth flow capability detection
460411

461-
public static bool CanUseBroker(ICommandContext context)
412+
public bool CanUseBroker()
462413
{
463414
#if NETFRAMEWORK
464415
// We only support the broker on Windows 10+ and in an interactive session
465-
if (!context.SessionManager.IsDesktopSession || !PlatformUtils.IsWindowsBrokerSupported())
416+
if (!Context.SessionManager.IsDesktopSession || !PlatformUtils.IsWindowsBrokerSupported())
466417
{
467418
return false;
468419
}
469420

470421
// Default to not using the OS broker
471422
const bool defaultValue = false;
472423

473-
if (context.Settings.TryGetSetting(Constants.EnvironmentVariables.MsAuthUseBroker,
424+
if (Context.Settings.TryGetSetting(Constants.EnvironmentVariables.MsAuthUseBroker,
474425
Constants.GitConfiguration.Credential.SectionName,
475426
Constants.GitConfiguration.Credential.MsAuthUseBroker,
476427
out string valueStr))

src/shared/Core/Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
1414
<Reference Include="System.Net.Http" />
1515
<Reference Include="System.Web" />
16-
<PackageReference Include="Microsoft.Identity.Client.Desktop" Version="4.52.0" />
16+
<PackageReference Include="Microsoft.Identity.Client.Broker" Version="4.52.0" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

src/shared/Core/Diagnostics/MicrosoftAuthenticationDiagnostic.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,8 @@ public MicrosoftAuthenticationDiagnostic(ICommandContext context)
1515

1616
protected override async Task<bool> RunInternalAsync(StringBuilder log, IList<string> additionalFiles)
1717
{
18-
if (MicrosoftAuthentication.CanUseBroker(CommandContext))
19-
{
20-
log.Append("Checking broker initialization state...");
21-
if (MicrosoftAuthentication.IsBrokerInitialized)
22-
{
23-
log.AppendLine(" Initialized");
24-
}
25-
else
26-
{
27-
log.AppendLine(" Not initialized");
28-
log.Append("Initializing broker...");
29-
MicrosoftAuthentication.InitializeBroker();
30-
log.AppendLine("OK");
31-
}
32-
}
33-
else
34-
{
35-
log.AppendLine("Broker not supported.");
36-
}
37-
3818
var msAuth = new MicrosoftAuthentication(CommandContext);
19+
log.AppendLine(msAuth.CanUseBroker() ? "Broker is enabled." : "Broker is not enabled.");
3920
log.AppendLine($"Flow type is: {msAuth.GetFlowType()}");
4021

4122
log.Append("Gathering MSAL token cache data...");

src/shared/Git-Credential-Manager/Program.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@ public static void Main(string[] args)
2525
// Write the start and version events
2626
context.Trace2.Start(context.ApplicationPath, args);
2727

28-
// Workaround for https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/2560
29-
if (MicrosoftAuthentication.CanUseBroker(context))
30-
{
31-
try
32-
{
33-
MicrosoftAuthentication.InitializeBroker();
34-
}
35-
catch (Exception ex)
36-
{
37-
context.Streams.Error.WriteLine(
38-
"warning: broker initialization failed{0}{1}",
39-
Environment.NewLine, ex.Message
40-
);
41-
}
42-
}
43-
4428
//
4529
// Git Credential Manager's executable used to be named "git-credential-manager-core" before
4630
// dropping the "-core" suffix. In order to prevent "helper not found" errors for users who

src/windows/Installer.Windows/Setup.iss

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,19 @@ Source: "{#PayloadDir}\Microsoft.AzureRepos.dll"; DestDir:
125125
Source: "{#PayloadDir}\gcmcore.dll"; DestDir: "{app}"; Flags: ignoreversion
126126
Source: "{#PayloadDir}\gcmcoreui.dll"; DestDir: "{app}"; Flags: ignoreversion
127127
Source: "{#PayloadDir}\gcmcoreuiwpf.dll"; DestDir: "{app}"; Flags: ignoreversion
128-
Source: "{#PayloadDir}\Microsoft.Identity.Client.Desktop.dll"; DestDir: "{app}"; Flags: ignoreversion
128+
Source: "{#PayloadDir}\Microsoft.Identity.Client.Broker.dll"; DestDir: "{app}"; Flags: ignoreversion
129129
Source: "{#PayloadDir}\Microsoft.Identity.Client.dll"; DestDir: "{app}"; Flags: ignoreversion
130130
Source: "{#PayloadDir}\Microsoft.Identity.Client.Extensions.Msal.dll"; DestDir: "{app}"; Flags: ignoreversion
131-
Source: "{#PayloadDir}\Microsoft.Web.WebView2.Core.dll"; DestDir: "{app}"; Flags: ignoreversion
132-
Source: "{#PayloadDir}\Microsoft.Web.WebView2.WinForms.dll"; DestDir: "{app}"; Flags: ignoreversion
133-
Source: "{#PayloadDir}\Microsoft.Web.WebView2.Wpf.dll"; DestDir: "{app}"; Flags: ignoreversion
131+
Source: "{#PayloadDir}\Microsoft.Identity.Client.NativeInterop.dll"; DestDir: "{app}"; Flags: ignoreversion
132+
Source: "{#PayloadDir}\Microsoft.IdentityModel.Abstractions.dll"; DestDir: "{app}"; Flags: ignoreversion
133+
Source: "{#PayloadDir}\msalruntime_x86.dll"; DestDir: "{app}"; Flags: ignoreversion
134134
Source: "{#PayloadDir}\Newtonsoft.Json.dll"; DestDir: "{app}"; Flags: ignoreversion
135135
Source: "{#PayloadDir}\NOTICE"; DestDir: "{app}"; Flags: ignoreversion
136136
Source: "{#PayloadDir}\System.Buffers.dll"; DestDir: "{app}"; Flags: ignoreversion
137137
Source: "{#PayloadDir}\System.CommandLine.dll"; DestDir: "{app}"; Flags: ignoreversion
138138
Source: "{#PayloadDir}\System.Memory.dll"; DestDir: "{app}"; Flags: ignoreversion
139139
Source: "{#PayloadDir}\System.Numerics.Vectors.dll"; DestDir: "{app}"; Flags: ignoreversion
140140
Source: "{#PayloadDir}\System.Runtime.CompilerServices.Unsafe.dll"; DestDir: "{app}"; Flags: ignoreversion
141-
Source: "{#PayloadDir}\WebView2Loader.dll"; DestDir: "{app}"; Flags: ignoreversion
142141

143142
[Code]
144143
// Don't allow installing conflicting architectures

0 commit comments

Comments
 (0)