public void ImportExport_ThrowException() { // Arrange var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>(); var cache = new MockTokenCache(); var storage = new MsalCacheStorage( _storageCreationPropertiesBuilder.Build(), cacheAccessor, new TraceSourceLogger(new TraceSource("ts"))); var helper = new MsalCacheHelper(cache, storage, _logger); byte[] dataToSave = Encoding.UTF8.GetBytes("Hello World 2"); var ex = new InvalidCastException(); cacheAccessor.Read().Throws(ex); // Act var actualEx = AssertException.Throws <InvalidCastException>( () => helper.LoadUnencryptedTokenCache()); // Assert Assert.AreEqual(ex, actualEx); // Arrange cacheAccessor.WhenForAnyArgs(c => c.Write(default)).Throw(ex);
public void ImportExport() { var storageBuilder = new StorageCreationPropertiesBuilder( Path.GetFileName(CacheFilePath), Path.GetDirectoryName(CacheFilePath), ClientId); storageBuilder = storageBuilder.WithMacKeyChain( serviceName: "Microsoft.Developer.IdentityService.Test", accountName: "MSALCacheTest"); // unit tests run on Linux boxes without LibSecret storageBuilder.WithLinuxUnprotectedFile(); // 1. Use MSAL to create an instance of the Public Client Application var app = PublicClientApplicationBuilder.Create(ClientId) .Build(); // 3. Create the high level MsalCacheHelper based on properties and a logger _cacheHelper = MsalCacheHelper.CreateAsync( storageBuilder.Build(), new TraceSource("MSAL.CacheExtension.Test")) .GetAwaiter().GetResult(); // 4. Let the cache helper handle MSAL's cache _cacheHelper.RegisterCache(app.UserTokenCache); // Act string dataString = "Hello World"; byte[] dataBytes = Encoding.UTF8.GetBytes(dataString); var result = _cacheHelper.LoadUnencryptedTokenCache(); Assert.AreEqual(0, result.Length); _cacheHelper.SaveUnencryptedTokenCache(dataBytes); byte[] actualData = _cacheHelper.LoadUnencryptedTokenCache(); Assert.AreEqual(dataString, Encoding.UTF8.GetString(actualData)); }
/// <summary> /// Notification that is triggered before token acquisition. /// </summary> /// <param name="args">Arguments related to the cache item impacted</param> public override void BeforeAccessNotification(TokenCacheNotificationArgs args) { MsalCacheHelper cacheStorage = GetMsalCacheHelper(); args.AssertNotNull(nameof(args)); try { args.TokenCache.DeserializeMsalV3(cacheStorage.LoadUnencryptedTokenCache()); } catch (Exception) { cacheStorage.Clear(); throw; } }
/// <summary> /// Notification that is triggered before token acquisition. /// </summary> /// <param name="args">Arguments related to the cache item impacted</param> public override void BeforeAccessNotification(TokenCacheNotificationArgs args) { MsalCacheHelper cacheStorage = GetMsalCacheStorage(); args.AssertNotNull(nameof(args)); try { cacheLock = new CrossPlatformLock($"{CacheFilePath}.lockfile"); cacheLock.CreateLockAsync().ConfigureAwait(false); args.TokenCache.DeserializeMsalV3(cacheStorage.LoadUnencryptedTokenCache()); } catch (Exception) { cacheStorage.Clear(); throw; } }
public void ImportExport() { // Arrange var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>(); var cache = new MockTokenCache(); var storage = new Storage( _storageCreationPropertiesBuilder.Build(), cacheAccessor, new TraceSourceLogger(new TraceSource("ts"))); var helper = new MsalCacheHelper(cache, storage, _logger); byte[] dataToSave = Encoding.UTF8.GetBytes("Hello World 2"); cacheAccessor.Read().Returns(Encoding.UTF8.GetBytes("Hello World")); // Act byte[] actualData = helper.LoadUnencryptedTokenCache(); helper.SaveUnencryptedTokenCache(dataToSave); // Assert Assert.AreEqual("Hello World", Encoding.UTF8.GetString(actualData)); cacheAccessor.Received().Write(dataToSave); }
/// <summary> /// Extracts the token cache data from the persistent store /// </summary> /// <remarks> /// This method should be used with care. The data returned is unencrypted. /// </remarks> /// <returns>UTF-8 byte array of the unencrypted token cache</returns> public virtual byte[] LoadUnencryptedTokenCache() { return(_helper.LoadUnencryptedTokenCache()); }