7
7
using Microsoft . Identity . Client . Extensions . Msal ;
8
8
9
9
#if NETFRAMEWORK
10
- using Microsoft . Identity . Client . Desktop ;
10
+ using Microsoft . Identity . Client . Broker ;
11
11
#endif
12
12
13
13
namespace GitCredentialManager . Authentication
@@ -41,55 +41,6 @@ public class MicrosoftAuthentication : AuthenticationBase, IMicrosoftAuthenticat
41
41
"live" , "liveconnect" , "liveid" ,
42
42
} ;
43
43
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
-
93
44
public MicrosoftAuthentication ( ICommandContext context )
94
45
: base ( context ) { }
95
46
@@ -99,17 +50,10 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenAsync(
99
50
string authority , string clientId , Uri redirectUri , string [ ] scopes , string userName )
100
51
{
101
52
// 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." ) ;
113
57
114
58
// Create the public client application for authentication
115
59
IPublicClientApplication app = await CreatePublicClientApplicationAsync ( authority , clientId , redirectUri , useBroker ) ;
@@ -287,12 +231,19 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
287
231
appBuilder . WithParentActivityOrWindow ( ( ) => new IntPtr ( hWndInt ) ) ;
288
232
}
289
233
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 )
292
238
{
293
239
#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
+ ) ;
296
247
#endif
297
248
}
298
249
@@ -458,19 +409,19 @@ public HttpClient GetHttpClient()
458
409
459
410
#region Auth flow capability detection
460
411
461
- public static bool CanUseBroker ( ICommandContext context )
412
+ public bool CanUseBroker ( )
462
413
{
463
414
#if NETFRAMEWORK
464
415
// 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 ( ) )
466
417
{
467
418
return false ;
468
419
}
469
420
470
421
// Default to not using the OS broker
471
422
const bool defaultValue = false ;
472
423
473
- if ( context . Settings . TryGetSetting ( Constants . EnvironmentVariables . MsAuthUseBroker ,
424
+ if ( Context . Settings . TryGetSetting ( Constants . EnvironmentVariables . MsAuthUseBroker ,
474
425
Constants . GitConfiguration . Credential . SectionName ,
475
426
Constants . GitConfiguration . Credential . MsAuthUseBroker ,
476
427
out string valueStr ) )
0 commit comments