Skip to content

Commit 38e5b92

Browse files
committed
azrepos: make shared token cache configurable
Azure Repos OAuth tokens currently always use the shared Microsoft developer tooling cache. While this enables authentication reuse across GCM, Visual Studio, and Azure CLI, users have no way to isolate GCM's authentication state. Allow users to select a GCM-specific token cache through Git configuration or an environment variable. Keep the shared cache as the default to preserve existing behaviour and document the interoperability and opt-out semantics. Assisted-by: GPT-5.6 Sol Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent cbb59cc commit 38e5b92

6 files changed

Lines changed: 107 additions & 1 deletion

File tree

‎docs/azrepos-users-and-tokens.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ including the Visual Studio IDE and Azure CLI. This means that as long as you're
4242
using Git or one of these tools with the same account, you'll never need to
4343
re-authenticate due to expired tokens!
4444

45+
GCM uses the shared Microsoft developer tooling token cache by default. To keep
46+
Azure Repos OAuth tokens in a GCM-specific cache instead, set
47+
[`credential.azreposUseMicrosoftSharedCache`][credential-azrepos-shared-cache]
48+
or [`GCM_AZREPOS_USE_MSFT_CACHE`][gcm-azrepos-shared-cache] to `false`.
49+
4550
#### User accounts
4651

4752
In versions of Git Credential Manager that support Microsoft identity OAuth
@@ -220,6 +225,8 @@ fabrikam:
220225

221226
[azure-devops-pats]: https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
222227
[credential-azreposCredentialType]: configuration.md#credentialazreposcredentialtype
228+
[credential-azrepos-shared-cache]: configuration.md#credentialazreposusemicrosoftsharedcache
223229
[gcm-azrepos-credential-type]: environment.md#GCM_AZREPOS_CREDENTIALTYPE
230+
[gcm-azrepos-shared-cache]: environment.md#GCM_AZREPOS_USE_MSFT_CACHE
224231
[azure-devops-api]: https://docs.microsoft.com/en-gb/rest/api/azure/devops/tokens/pats
225232
[rfc3986-s321]: https://www.rfc-editor.org/rfc/rfc3986#section-3.2.1

‎docs/configuration.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,27 @@ Credential: "git:https://bob@github.com/example/myrepo" (user = bob)
827827

828828
---
829829

830+
### credential.azreposUseMicrosoftSharedCache
831+
832+
Use the token cache shared by Microsoft developer tools when authenticating to
833+
Azure Repos with Microsoft identity OAuth tokens. This allows GCM to reuse
834+
authentication performed by tools such as Visual Studio and Azure CLI, and
835+
allows those tools to reuse authentication performed by GCM.
836+
837+
Set this value to `false` to use a GCM-specific token cache instead.
838+
839+
Defaults to `true`.
840+
841+
#### Example
842+
843+
```shell
844+
git config --global credential.azreposUseMicrosoftSharedCache false
845+
```
846+
847+
**Also see: [GCM_AZREPOS_USE_MSFT_CACHE][gcm-azrepos-shared-cache]**
848+
849+
---
850+
830851
### credential.azreposCredentialType
831852

832853
Specify the type of credential the Azure Repos host provider should return.
@@ -1179,6 +1200,7 @@ Defaults to disabled.
11791200
[gcm-authority]: environment.md#GCM_AUTHORITY-deprecated
11801201
[gcm-autodetect-timeout]: environment.md#GCM_AUTODETECT_TIMEOUT
11811202
[gcm-azrepos-credentialtype]: environment.md#GCM_AZREPOS_CREDENTIALTYPE
1203+
[gcm-azrepos-shared-cache]: environment.md#GCM_AZREPOS_USE_MSFT_CACHE
11821204
[gcm-azrepos-credentialmanagedidentity]: environment.md#GCM_AZREPOS_MANAGEDIDENTITY
11831205
[gcm-azrepos-wif]: environment.md#GCM_AZREPOS_WIF
11841206
[gcm-azrepos-wif-clientid]: environment.md#GCM_AZREPOS_WIF_CLIENTID

‎docs/environment.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,33 @@ export GCM_MSAUTH_USEDEFAULTACCOUNT="false"
920920

921921
---
922922

923+
### GCM_AZREPOS_USE_MSFT_CACHE
924+
925+
Use the token cache shared by Microsoft developer tools when authenticating to
926+
Azure Repos with Microsoft identity OAuth tokens. This allows GCM to reuse
927+
authentication performed by tools such as Visual Studio and Azure CLI, and
928+
allows those tools to reuse authentication performed by GCM.
929+
930+
Set this value to `false` to use a GCM-specific token cache instead.
931+
932+
Defaults to `true`.
933+
934+
#### Windows
935+
936+
```batch
937+
SET GCM_AZREPOS_USE_MSFT_CACHE=false
938+
```
939+
940+
#### macOS/Linux
941+
942+
```bash
943+
export GCM_AZREPOS_USE_MSFT_CACHE="false"
944+
```
945+
946+
**Also see: [credential.azreposUseMicrosoftSharedCache][shared-cache]**
947+
948+
---
949+
923950
### GCM_AZREPOS_CREDENTIALTYPE
924951

925952
Specify the type of credential the Azure Repos host provider should return.
@@ -1351,6 +1378,7 @@ Defaults to disabled.
13511378
[credential-authority]: configuration.md#credentialauthority-deprecated
13521379
[credential-autodetecttimeout]: configuration.md#credentialautodetecttimeout
13531380
[credential-azrepos-credential-type]: configuration.md#credentialazreposcredentialtype
1381+
[shared-cache]: configuration.md#credentialazreposusemicrosoftsharedcache
13541382
[credential-azrepos-managedidentity]: configuration.md#credentialazreposmanagedidentity
13551383
[credential-azrepos-wif]: configuration.md#credentialazreposworkloadfederation
13561384
[credential-azrepos-wif-clientid]: configuration.md#credentialazreposworkloadfederationclientid

‎src/Microsoft.AzureRepos.Tests/AzureReposHostProviderTests.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,39 @@ public class AzureReposHostProviderTests
1717
$"{Constants.GitConfiguration.Credential.SectionName}.{Constants.GitConfiguration.Credential.Helper}";
1818
private static readonly string AzDevUseHttpPathKey =
1919
$"{Constants.GitConfiguration.Credential.SectionName}.https://dev.azure.com.{Constants.GitConfiguration.Credential.UseHttpPath}";
20+
private static readonly string AzDevUseSharedCacheKey =
21+
$"{Constants.GitConfiguration.Credential.SectionName}.{AzureDevOpsConstants.GitConfiguration.Credential.UseSharedCache}";
2022
private static readonly string OrgName = "org";
2123

24+
[Fact]
25+
public void AzureReposProvider_GetUseSharedCache_NoConfiguration_ReturnsTrue()
26+
{
27+
var provider = new AzureReposHostProvider(new TestCommandContext());
28+
29+
Assert.True(provider.GetUseSharedCache());
30+
}
31+
32+
[Fact]
33+
public void AzureReposProvider_GetUseSharedCache_GitConfigFalse_ReturnsFalse()
34+
{
35+
var context = new TestCommandContext();
36+
context.Git.Configuration.Global[AzDevUseSharedCacheKey] = new List<string> {"false"};
37+
var provider = new AzureReposHostProvider(context);
38+
39+
Assert.False(provider.GetUseSharedCache());
40+
}
41+
42+
[Fact]
43+
public void AzureReposProvider_GetUseSharedCache_EnvironmentOverridesGitConfig()
44+
{
45+
var context = new TestCommandContext();
46+
context.Git.Configuration.Global[AzDevUseSharedCacheKey] = new List<string> {"false"};
47+
context.Environment.Variables[AzureDevOpsConstants.EnvironmentVariables.UseSharedCache] = "true";
48+
var provider = new AzureReposHostProvider(context);
49+
50+
Assert.True(provider.GetUseSharedCache());
51+
}
52+
2253
[Fact]
2354
public void AzureReposProvider_IsSupported_AzureHost_UnencryptedHttp_ReturnsTrue()
2455
{

‎src/Microsoft.AzureRepos/AzureDevOpsConstants.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static class EnvironmentVariables
3636
{
3737
public const string DevAadClientId = "GCM_DEV_AZREPOS_CLIENTID";
3838
public const string DevAadAuthorityBaseUri = "GCM_DEV_AZREPOS_AUTHORITYBASEURI";
39+
public const string UseSharedCache = "GCM_AZREPOS_USE_MSFT_CACHE";
3940
public const string CredentialType = "GCM_AZREPOS_CREDENTIALTYPE";
4041
public const string ServicePrincipalId = "GCM_AZREPOS_SERVICE_PRINCIPAL";
4142
public const string ServicePrincipalSecret = "GCM_AZREPOS_SP_SECRET";
@@ -56,6 +57,7 @@ public static class Credential
5657
{
5758
public const string DevAadClientId = "azreposDevClientId";
5859
public const string DevAadAuthorityBaseUri = "azreposDevAuthorityBaseUri";
60+
public const string UseSharedCache = "azreposUseMicrosoftSharedCache";
5961
public const string CredentialType = "azreposCredentialType";
6062
public const string AzureAuthority = "azureAuthority";
6163
public const string ServicePrincipal = "azreposServicePrincipal";

‎src/Microsoft.AzureRepos/AzureReposHostProvider.cs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,30 @@ private PublicClientConfig GetEntraConfig()
406406
{
407407
ClientId = GetClientId(),
408408
IsMsaPassthroughEnabled = true,
409-
UseSharedCache = true,
409+
UseSharedCache = GetUseSharedCache(),
410410
SupportsWindowsBroker = true,
411411
// TODO: enable once our app registration has the appropriate redirect URLs
412412
//SupportsMacBroker = true,
413413
//SupportsLinuxBroker = true,
414414
};
415415
}
416416

417+
internal bool GetUseSharedCache()
418+
{
419+
const bool defaultValue = true; // prefer using the shared Microsoft dev cache
420+
421+
if (_context.Settings.TryGetSetting(
422+
AzureDevOpsConstants.EnvironmentVariables.UseSharedCache,
423+
Constants.GitConfiguration.Credential.SectionName,
424+
AzureDevOpsConstants.GitConfiguration.Credential.UseSharedCache,
425+
out string str))
426+
{
427+
return str.ToBooleanyOrDefault(defaultValue);
428+
}
429+
430+
return defaultValue;
431+
}
432+
417433
private string GetClientId()
418434
{
419435
// Check for developer override value

0 commit comments

Comments
 (0)