-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve authentication performance by caching on file system
- Loading branch information
Gautam Sheth
committed
Aug 30, 2024
1 parent
83825f4
commit c8c35a3
Showing
2 changed files
with
126 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/lib/PnP.Framework/Utilities/Cache/MsalCacheHelperUtility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Microsoft.Identity.Client.Extensions.Msal; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace PnP.Framework.Utilities.Cache | ||
{ | ||
public class MsalCacheHelperUtility | ||
{ | ||
|
||
private static MsalCacheHelper MsalCacheHelper; | ||
private static readonly object ObjectLock = new(); | ||
|
||
private static class Config | ||
{ | ||
// Cache settings | ||
public const string CacheFileName = "m365pnpmsal.cache"; | ||
public readonly static string CacheDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ".M365PnPAuthService"); | ||
|
||
public const string KeyChainServiceName = "M365.PnP.Framework"; | ||
public const string KeyChainAccountName = "M365PnPAuthCache"; | ||
|
||
public const string LinuxKeyRingSchema = "com.m365.pnp.auth.tokencache"; | ||
public const string LinuxKeyRingCollection = MsalCacheHelper.LinuxKeyRingDefaultCollection; | ||
public const string LinuxKeyRingLabel = "MSAL token cache for M365 PnP Framework."; | ||
public static readonly KeyValuePair<string, string> LinuxKeyRingAttr1 = new KeyValuePair<string, string>("Version", "1"); | ||
public static readonly KeyValuePair<string, string> LinuxKeyRingAttr2 = new KeyValuePair<string, string>("Product", "M365PnPAuth"); | ||
} | ||
|
||
public static MsalCacheHelper CreateCacheHelper() | ||
{ | ||
if (MsalCacheHelper == null) | ||
{ | ||
lock (ObjectLock) | ||
{ | ||
if (MsalCacheHelper == null) | ||
{ | ||
StorageCreationProperties storageProperties; | ||
|
||
try | ||
{ | ||
storageProperties = new StorageCreationPropertiesBuilder( | ||
Config.CacheFileName, | ||
Config.CacheDir) | ||
.WithLinuxKeyring( | ||
Config.LinuxKeyRingSchema, | ||
Config.LinuxKeyRingCollection, | ||
Config.LinuxKeyRingLabel, | ||
Config.LinuxKeyRingAttr1, | ||
Config.LinuxKeyRingAttr2) | ||
.WithMacKeyChain( | ||
Config.KeyChainServiceName, | ||
Config.KeyChainAccountName) | ||
.Build(); | ||
|
||
var cacheHelper = MsalCacheHelper.CreateAsync(storageProperties).ConfigureAwait(false).GetAwaiter().GetResult(); | ||
|
||
cacheHelper.VerifyPersistence(); | ||
MsalCacheHelper = cacheHelper; | ||
|
||
} | ||
catch (MsalCachePersistenceException) | ||
{ | ||
// do not use the same file name so as not to overwrite the encrypted version | ||
storageProperties = new StorageCreationPropertiesBuilder( | ||
Config.CacheFileName + ".plaintext", | ||
Config.CacheDir) | ||
.WithUnprotectedFile() | ||
.Build(); | ||
|
||
var cacheHelper = MsalCacheHelper.CreateAsync(storageProperties).ConfigureAwait(false).GetAwaiter().GetResult(); | ||
cacheHelper.VerifyPersistence(); | ||
|
||
MsalCacheHelper = cacheHelper; | ||
} | ||
catch | ||
{ | ||
MsalCacheHelper = null; | ||
} | ||
} | ||
} | ||
} | ||
return MsalCacheHelper; | ||
} | ||
} | ||
} |