Skip to content

Commit f576370

Browse files
committed
azrepos: suggest legacy auth for config errors
The new GCM Entra application can encounter registration problems that did not affect the legacy Visual Studio application. Automatically retrying every MSAL failure can hide those problems and encourage unintentional reliance on the compatibility client. Refusals, policy challenges and transient failures also do not justify changing application identity. Keep the original failure visible and leave recovery to an explicit user choice. Offer legacy-client guidance only for recognized application-configuration service errors from the new client, without treating authentication challenges or temporary failures as registration defects. This applies to user OAuth and PAT creation. Managed identity, workload federation and service-principal authentication do not use the public client ID and are unaffected. Assisted-by: GPT-5.6 Sol Assisted-by: GPT-6 Astra Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent a5d19e1 commit f576370

3 files changed

Lines changed: 76 additions & 25 deletions

File tree

‎docs/configuration.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,15 +854,22 @@ Use the legacy Visual Studio Entra application when authenticating to Azure
854854
Repos with Microsoft identity OAuth tokens. Set this value to `true` to restore
855855
the application identity used by earlier versions of GCM.
856856

857+
For recognized client application configuration failures, GCM suggests this
858+
option but does not retry authentication automatically. Use it as a temporary
859+
compatibility workaround. If retrying with the legacy application succeeds,
860+
please [report the original failure](https://aka.ms/gcm/bug).
861+
857862
The legacy application does not support broker authentication on macOS or
858863
Linux.
859864

860865
Defaults to `false`.
861866

862867
#### Example
863868

869+
Retry one Git command without persisting the setting:
870+
864871
```shell
865-
git config --global credential.azreposUseLegacyClientId true
872+
git -c credential.azreposUseLegacyClientId=true fetch
866873
```
867874

868875
**Also see: [GCM_AZREPOS_USE_LEGACY_CLIENTID][gcm-azrepos-legacy-client-id]**

‎docs/environment.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,10 @@ Use the legacy Visual Studio Entra application when authenticating to Azure
953953
Repos with Microsoft identity OAuth tokens. Set this value to `true` to restore
954954
the application identity used by earlier versions of GCM.
955955

956+
This is an explicit compatibility option; GCM does not switch to the legacy
957+
application automatically. For a one-command retry without changing the
958+
environment, use [credential.azreposUseLegacyClientId][legacy-client-id].
959+
956960
The legacy application does not support broker authentication on macOS or
957961
Linux.
958962

‎src/Microsoft.AzureRepos/AzureReposHostProvider.cs‎

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using GitCredentialManager;
1010
using GitCredentialManager.Authentication.Entra;
1111
using GitCredentialManager.Commands;
12+
using Microsoft.Identity.Client;
1213
using KnownGitCfg = GitCredentialManager.Constants.GitConfiguration;
1314

1415
namespace 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

Comments
 (0)