99using GitCredentialManager ;
1010using GitCredentialManager . Authentication . Entra ;
1111using GitCredentialManager . Commands ;
12+ using Microsoft . Identity . Client ;
1213using KnownGitCfg = GitCredentialManager . Constants . GitConfiguration ;
1314
1415namespace Microsoft . AzureRepos
@@ -21,37 +22,35 @@ public class AzureReposHostProvider : DisposableObject, IHostProvider, IConfigur
2122 private readonly IAzureReposBindingManager _bindingManager ;
2223 private readonly Lazy < IEntraAuthentication > _entraAuth ;
2324
24- public AzureReposHostProvider ( ICommandContext context )
25- : this ( context , new AzureDevOpsRestApi ( context ) ,
25+ public AzureReposHostProvider ( ICommandContext context ) :
26+ this ( context , new AzureDevOpsRestApi ( context ) , config => new EntraAuthentication ( context , config ) ,
2627 new AzureDevOpsAuthorityCache ( context ) , new AzureReposBindingManager ( context ) )
2728 {
2829 }
2930
30- public AzureReposHostProvider ( ICommandContext context , IAzureDevOpsRestApi azDevOps ,
31- IAzureDevOpsAuthorityCache authorityCache ,
31+ internal AzureReposHostProvider ( ICommandContext context , IAzureDevOpsRestApi azDevOps ,
32+ IEntraAuthentication entraAuth , IAzureDevOpsAuthorityCache authorityCache ,
3233 IAzureReposBindingManager bindingManager )
34+ : this ( context , azDevOps , _ => entraAuth , authorityCache , bindingManager )
35+ {
36+ EnsureArgument . NotNull ( entraAuth , nameof ( entraAuth ) ) ;
37+ }
38+
39+ internal AzureReposHostProvider ( ICommandContext context , IAzureDevOpsRestApi azDevOps ,
40+ Func < PublicClientConfig , IEntraAuthentication > entraAuthFactory ,
41+ IAzureDevOpsAuthorityCache authorityCache , IAzureReposBindingManager bindingManager )
3342 {
3443 EnsureArgument . NotNull ( context , nameof ( context ) ) ;
3544 EnsureArgument . NotNull ( azDevOps , nameof ( azDevOps ) ) ;
3645 EnsureArgument . NotNull ( authorityCache , nameof ( authorityCache ) ) ;
3746 EnsureArgument . NotNull ( bindingManager , nameof ( bindingManager ) ) ;
47+ EnsureArgument . NotNull ( entraAuthFactory , nameof ( entraAuthFactory ) ) ;
3848
3949 _context = context ;
4050 _azDevOps = azDevOps ;
4151 _authorityCache = authorityCache ;
4252 _bindingManager = bindingManager ;
43- _entraAuth = new Lazy < IEntraAuthentication > (
44- ( ) => new EntraAuthentication ( _context , GetEntraConfig ( ) ) ) ;
45- }
46-
47- public AzureReposHostProvider ( ICommandContext context , IAzureDevOpsRestApi azDevOps ,
48- IEntraAuthentication entraAuth , IAzureDevOpsAuthorityCache authorityCache ,
49- IAzureReposBindingManager bindingManager )
50- : this ( context , azDevOps , authorityCache , bindingManager )
51- {
52- EnsureArgument . NotNull ( entraAuth , nameof ( entraAuth ) ) ;
53-
54- _entraAuth = new Lazy < IEntraAuthentication > ( ( ) => entraAuth ) ;
53+ _entraAuth = new Lazy < IEntraAuthentication > ( ( ) => entraAuthFactory ( GetEntraConfig ( ) ) ) ;
5554 }
5655
5756 #region IHostProvider
@@ -116,6 +115,49 @@ public async Task<GitResponse> GetCredentialAsync(GitRequest request)
116115 ) ;
117116 }
118117
118+ try
119+ {
120+ return await GetUserCredentialAsync ( request ) ;
121+ }
122+ catch ( MsalException msalEx ) when ( IsClientConfigException ( msalEx ) &&
123+ _entraAuth . Value . PublicClientConfig . ClientId ==
124+ AzureDevOpsConstants . ClientId )
125+ {
126+ string legacyClientSetting =
127+ $ "{ KnownGitCfg . Credential . SectionName } .{ AzureDevOpsConstants . GitConfiguration . Credential . UseLegacyClientId } ";
128+ _context . Console . WriteWarning (
129+ "Authentication using the new GCM Entra application failed. " +
130+ "To retry this command with the legacy Entra application, " +
131+ $ "set '{ legacyClientSetting } ' to 'true' in Git configuration.") ;
132+ _context . Console . WriteInfo (
133+ $ "If that succeeds, please report the original failure at { Constants . HelpUrls . GcmNewIssue } ") ;
134+
135+ throw ;
136+ }
137+ }
138+
139+ private static bool IsClientConfigException ( MsalException ex )
140+ {
141+ // Not a client configuration error!
142+ if ( ex is MsalUiRequiredException or MsalThrottledServiceException or
143+ IntuneAppProtectionPolicyRequiredException )
144+ {
145+ return false ;
146+ }
147+
148+ if ( ex is MsalServiceException svcEx )
149+ {
150+ // Not retryable and no additionally required claims means this is likely a client configuration issue
151+ // (e.g. invalid client ID, missing or invalid redirect URI, etc.)
152+ return svcEx . ErrorCode is MsalError . InvalidClient or MsalError . UnauthorizedClient &&
153+ ! svcEx . IsRetryable && string . IsNullOrWhiteSpace ( svcEx . Claims ) ;
154+ }
155+
156+ return false ;
157+ }
158+
159+ private async Task < GitResponse > GetUserCredentialAsync ( GitRequest request )
160+ {
119161 if ( UsePersonalAccessTokens ( ) )
120162 {
121163 Uri remoteWithUserUri = request . GetRemoteUri ( includeUser : true ) ;
@@ -141,14 +183,12 @@ public async Task<GitResponse> GetCredentialAsync(GitRequest request)
141183
142184 return new GitResponse ( credential ) ;
143185 }
144- else
145- {
146- // Include the username request here so that we may use it as an override
147- // for user account lookups when getting Entra access tokens.
148- var entraResult = await GetEntraAccessTokenAsync ( request ) ;
149- var entraCredential = new GitCredential ( entraResult . Account . UserName , entraResult . AccessToken ) ;
150- return new GitResponse ( entraCredential ) ;
151- }
186+
187+ // Include the username request here so that we may use it as an override
188+ // for user account lookups when getting Entra access tokens.
189+ var entraResult = await GetEntraAccessTokenAsync ( request ) ;
190+ var entraCredential = new GitCredential ( entraResult . Account . UserName , entraResult . AccessToken ) ;
191+ return new GitResponse ( entraCredential ) ;
152192 }
153193
154194 public Task StoreCredentialAsync ( GitRequest request )
0 commit comments