Skip to content

Commit

Permalink
Share MSAL cache with GCM and VS (#333)
Browse files Browse the repository at this point in the history
* Use same MSAL cache as VS and GCM

* fix typo

* MSAL cache should be enabled by default

* make cache enabled by default

* PR feedback
  • Loading branch information
johnterickson authored Sep 22, 2022
1 parent 30ec36b commit 29d3b5d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CredentialProvider.Microsoft.Tests.CredentialProviders.Vsts
{
internal static class EnvironmentLock
{
private static readonly Semaphore _lock = new Semaphore(1, 1, "CredentialProvider.Microsoft.Tests.CredentialProviders.Vsts");
private static readonly Semaphore _lock = new Semaphore(1, 1);
private static readonly Dictionary<string,string> savedEnvVars = new Dictionary<string, string>();

public static async Task<IDisposable> WaitAsync()
Expand Down
15 changes: 10 additions & 5 deletions CredentialProvider.Microsoft/Util/EnvUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ public static class EnvUtil
public const string MsalFileCacheEnvVar = "NUGET_CREDENTIALPROVIDER_MSAL_FILECACHE_ENABLED";
public const string MsalFileCacheLocationEnvVar = "NUGET_CREDENTIALPROVIDER_MSAL_FILECACHE_LOCATION";

private static readonly string LocalAppDataLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create), "MicrosoftCredentialProvider");
private static readonly string LocalAppDataLocation = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create);

public static string AdalTokenCacheLocation { get; } = Path.Combine(LocalAppDataLocation, "ADALTokenCache.dat");
private const string CredenetialProviderFolderName = "MicrosoftCredentialProvider";
public static string AdalTokenCacheLocation { get; } = Path.Combine(LocalAppDataLocation, CredenetialProviderFolderName, "ADALTokenCache.dat");

public static string DefaultMsalCacheLocation { get; } = Path.Combine(LocalAppDataLocation, "MSALTokenCache.dat");
// from /~https://github.com/GitCredentialManager/git-credential-manager/blob/df90676d1249759eef8cec57155c27e869503225/src/shared/Microsoft.Git.CredentialManager/Authentication/MicrosoftAuthentication.cs#L277
// The Visual Studio MSAL cache is located at "%LocalAppData%\.IdentityService\msal.cache" on Windows.
// We use the MSAL extension library to provide us consistent cache file access semantics (synchronisation, etc)
// as Visual Studio itself follows, as well as other Microsoft developer tools such as the Azure PowerShell CLI.
public static string DefaultMsalCacheLocation { get; } = Path.Combine(LocalAppDataLocation, ".IdentityService", "msal.cache");

public static string FileLogLocation { get; } = Environment.GetEnvironmentVariable(LogPathEnvVar);

public static string SessionTokenCacheLocation { get; } = Path.Combine(LocalAppDataLocation, "SessionTokenCache.dat");
public static string SessionTokenCacheLocation { get; } = Path.Combine(LocalAppDataLocation, CredenetialProviderFolderName, "SessionTokenCache.dat");

public static Uri GetAuthorityFromEnvironment(ILogger logger)
{
Expand Down Expand Up @@ -81,7 +86,7 @@ internal static bool MsalEnabled()

public static bool MsalFileCacheEnabled()
{
return GetEnabledFromEnvironment(MsalFileCacheEnvVar, defaultValue: false);
return GetEnabledFromEnvironment(MsalFileCacheEnvVar, defaultValue: true);
}

public static IList<string> GetHostsFromEnvironment(ILogger logger, string envVar, IEnumerable<string> defaultHosts, [CallerMemberName] string collectionName = null)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ Cache Location
Session Token Cache
C:\Users\someuser\AppData\Local\MicrosoftCredentialProvider\SessionTokenCache.dat
MSAL Token Cache
C:\Users\someuser\AppData\Local\.IdentityService\msal.cache
Windows Integrated Authentication
NUGET_CREDENTIALPROVIDER_WINDOWSINTEGRATEDAUTHENTICATION_ENABLED
Boolean to enable/disable using silent Windows Integrated Authentication
Expand Down

0 comments on commit 29d3b5d

Please sign in to comment.