@@ -5,23 +5,16 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers
55{
66 using Microsoft . Graph . Auth ;
77 using Microsoft . Graph . PowerShell . Authentication . Models ;
8+ using Microsoft . Graph . PowerShell . Authentication . TokenCache ;
89 using Microsoft . Identity . Client ;
910 using System ;
1011 using System . IO ;
1112 using System . Linq ;
12- using System . Security . Cryptography ;
1313 using System . Security . Cryptography . X509Certificates ;
1414
1515 internal static class AuthenticationHelpers
1616 {
1717 private static readonly object FileLock = new object ( ) ;
18- private static readonly string UserCacheFileName = "userTokenCache.bin3" ;
19- private static readonly string AppCacheFileName = "appTokenCache.bin3" ;
20-
21- /// <summary>
22- /// Path to the token cache.
23- /// </summary>
24- internal static readonly string CacheFilePath = Path . GetDirectoryName ( System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ) ;
2518
2619 internal static IAuthenticationProvider GetAuthProvider ( AuthConfig authConfig )
2720 {
@@ -32,7 +25,7 @@ internal static IAuthenticationProvider GetAuthProvider(AuthConfig authConfig)
3225 . WithTenantId ( authConfig . TenantId )
3326 . Build ( ) ;
3427
35- ConfigureTokenCache ( publicClientApp . UserTokenCache , Path . Combine ( CacheFilePath , UserCacheFileName ) ) ;
28+ ConfigureTokenCache ( publicClientApp . UserTokenCache , Constants . UserCacheFileName ) ;
3629 return new DeviceCodeProvider ( publicClientApp , authConfig . Scopes , async ( result ) => {
3730 await Console . Out . WriteLineAsync ( result . Message ) ;
3831 } ) ;
@@ -45,7 +38,7 @@ internal static IAuthenticationProvider GetAuthProvider(AuthConfig authConfig)
4538 . WithCertificate ( string . IsNullOrEmpty ( authConfig . CertificateThumbprint ) ? GetCertificateByName ( authConfig . CertificateName ) : GetCertificateByThumbprint ( authConfig . CertificateThumbprint ) )
4639 . Build ( ) ;
4740
48- ConfigureTokenCache ( confidentialClientApp . AppTokenCache , Path . Combine ( CacheFilePath , AppCacheFileName ) ) ;
41+ ConfigureTokenCache ( confidentialClientApp . AppTokenCache , Constants . AppCacheFileName ) ;
4942 return new ClientCredentialProvider ( confidentialClientApp ) ;
5043 }
5144 }
@@ -55,19 +48,24 @@ internal static void Logout(AuthConfig authConfig)
5548 lock ( FileLock )
5649 {
5750 if ( authConfig . AuthType == AuthenticationType . Delegated )
58- File . Delete ( Path . Combine ( CacheFilePath , UserCacheFileName ) ) ;
51+ File . Delete ( Path . Combine ( Constants . TokenCacheDirectory , Constants . UserCacheFileName ) ) ;
5952 else
60- File . Delete ( Path . Combine ( CacheFilePath , AppCacheFileName ) ) ;
53+ File . Delete ( Path . Combine ( Constants . TokenCacheDirectory , Constants . AppCacheFileName ) ) ;
6154 }
6255 }
6356
64- private static void ConfigureTokenCache ( ITokenCache tokenCache , string tokenCachePath )
57+ private static void ConfigureTokenCache ( ITokenCache tokenCache , string tokenCacheFile )
6558 {
59+ if ( ! Directory . Exists ( Constants . TokenCacheDirectory ) )
60+ Directory . CreateDirectory ( Constants . TokenCacheDirectory ) ;
61+
62+ string tokenCacheFilePath = Path . Combine ( Constants . TokenCacheDirectory , tokenCacheFile ) ;
63+
6664 tokenCache . SetBeforeAccess ( ( TokenCacheNotificationArgs args ) => {
6765 lock ( FileLock )
6866 {
69- args . TokenCache . DeserializeMsalV3 ( File . Exists ( tokenCachePath )
70- ? TokenCryptoHelpers . DecryptToken ( File . ReadAllBytes ( tokenCachePath ) )
67+ args . TokenCache . DeserializeMsalV3 ( File . Exists ( tokenCacheFilePath )
68+ ? TokenCryptographer . DecryptToken ( File . ReadAllBytes ( tokenCacheFilePath ) )
7169 : null ,
7270 shouldClearExistingCache : true ) ;
7371 }
@@ -78,7 +76,7 @@ private static void ConfigureTokenCache(ITokenCache tokenCache, string tokenCach
7876 {
7977 if ( args . HasStateChanged )
8078 {
81- File . WriteAllBytes ( tokenCachePath , TokenCryptoHelpers . EncryptToken ( args . TokenCache . SerializeMsalV3 ( ) ) ) ;
79+ File . WriteAllBytes ( tokenCacheFilePath , TokenCryptographer . EncryptToken ( args . TokenCache . SerializeMsalV3 ( ) ) ) ;
8280 }
8381 }
8482 } ) ;
0 commit comments