Skip to content

Commit 89b099e

Browse files
committed
msauth: abstract token cache init helpers
Refactor the token cache helper methods to allow us to re-use the existing cache registration logic with a different ITokenCache and StorageCreationProperties. This will be useful when we later introduce a confidential client application (for service principals) that needs a different cache location, and uses the AppTokenCache, rather than the User one.
1 parent b627044 commit 89b099e

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/shared/Core/Authentication/MicrosoftAuthentication.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
422422

423423
IPublicClientApplication app = appBuilder.Build();
424424

425-
// Register the application token cache
426-
await RegisterTokenCacheAsync(app, Context.Trace2);
425+
// Register the user token cache
426+
await RegisterTokenCacheAsync(app.UserTokenCache, CreateUserTokenCacheProps, Context.Trace2);
427427

428428
return app;
429429
}
@@ -432,10 +432,11 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
432432

433433
#region Helpers
434434

435-
private async Task RegisterTokenCacheAsync(IPublicClientApplication app, ITrace2 trace2)
435+
private delegate StorageCreationProperties StoragePropertiesBuilder(bool useLinuxFallback);
436+
437+
private async Task RegisterTokenCacheAsync(ITokenCache cache, StoragePropertiesBuilder propsBuilder, ITrace2 trace2)
436438
{
437-
Context.Trace.WriteLine(
438-
"Configuring Microsoft Authentication token cache to instance shared with Microsoft developer tools...");
439+
Context.Trace.WriteLine("Configuring MSAL token cache...");
439440

440441
if (!PlatformUtils.IsWindows() && !PlatformUtils.IsPosix())
441442
{
@@ -445,11 +446,11 @@ private async Task RegisterTokenCacheAsync(IPublicClientApplication app, ITrace2
445446
}
446447

447448
// We use the MSAL extension library to provide us consistent cache file access semantics (synchronisation, etc)
448-
// as other Microsoft developer tools such as the Azure PowerShell CLI.
449+
// as other GCM processes, and other Microsoft developer tools such as the Azure PowerShell CLI.
449450
MsalCacheHelper helper = null;
450451
try
451452
{
452-
var storageProps = CreateTokenCacheProps(useLinuxFallback: false);
453+
StorageCreationProperties storageProps = propsBuilder(useLinuxFallback: false);
453454
helper = await MsalCacheHelper.CreateAsync(storageProps);
454455

455456
// Test that cache access is working correctly
@@ -477,24 +478,31 @@ private async Task RegisterTokenCacheAsync(IPublicClientApplication app, ITrace2
477478
// On Linux the SecretService/keyring might not be available so we must fall-back to a plaintext file.
478479
Context.Streams.Error.WriteLine("warning: using plain-text fallback token cache");
479480
Context.Trace.WriteLine("Using fall-back plaintext token cache on Linux.");
480-
var storageProps = CreateTokenCacheProps(useLinuxFallback: true);
481+
StorageCreationProperties storageProps = propsBuilder(useLinuxFallback: true);
481482
helper = await MsalCacheHelper.CreateAsync(storageProps);
482483
}
483484
}
484485

485486
if (helper is null)
486487
{
487-
Context.Streams.Error.WriteLine("error: failed to set up Microsoft Authentication token cache!");
488-
Context.Trace.WriteLine("Failed to integrate with shared token cache!");
488+
Context.Streams.Error.WriteLine("error: failed to set up token cache!");
489+
Context.Trace.WriteLine("Failed to integrate with token cache!");
489490
}
490491
else
491492
{
492-
helper.RegisterCache(app.UserTokenCache);
493-
Context.Trace.WriteLine("Microsoft developer tools token cache configured.");
493+
helper.RegisterCache(cache);
494+
Context.Trace.WriteLine("Token cache configured.");
494495
}
495496
}
496497

497-
internal StorageCreationProperties CreateTokenCacheProps(bool useLinuxFallback)
498+
/// <summary>
499+
/// Create the properties for the user token cache. This is used by public client applications only.
500+
/// This cache is shared between GCM processes, and also other Microsoft developer tools such as the Azure
501+
/// PowerShell CLI.
502+
/// </summary>
503+
/// <param name="useLinuxFallback"></param>
504+
/// <returns></returns>
505+
internal StorageCreationProperties CreateUserTokenCacheProps(bool useLinuxFallback)
498506
{
499507
const string cacheFileName = "msal.cache";
500508
string cacheDirectory;

src/shared/Core/Diagnostics/MicrosoftAuthenticationDiagnostic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected override async Task<bool> RunInternalAsync(StringBuilder log, IList<st
2020
log.AppendLine($"Flow type is: {msAuth.GetFlowType()}");
2121

2222
log.Append("Gathering MSAL token cache data...");
23-
StorageCreationProperties cacheProps = msAuth.CreateTokenCacheProps(true);
23+
StorageCreationProperties cacheProps = msAuth.CreateUserTokenCacheProps(true);
2424
log.AppendLine(" OK");
2525
log.AppendLine($"CacheDirectory: {cacheProps.CacheDirectory}");
2626
log.AppendLine($"CacheFileName: {cacheProps.CacheFileName}");

0 commit comments

Comments
 (0)