< Summary

Class:Azure.Identity.MsalCacheReader
Assembly:Azure.Identity
File(s):C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\MsalCacheReader.cs
Covered lines:0
Uncovered lines:24
Coverable lines:24
Total lines:74
Line coverage:0% (0 of 24)
Covered branches:0
Total branches:6
Branch coverage:0% (0 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%100%
OnBeforeAccessAsync()-0%0%
ReadCacheFromProtectedStorageAsync()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\MsalCacheReader.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using Microsoft.Identity.Client;
 5using System;
 6using System.IO;
 7using System.Threading.Tasks;
 8using System.Security.Cryptography;
 9
 10namespace Azure.Identity
 11{
 12    internal class MsalCacheReader
 13    {
 14        private readonly string _cachePath;
 15        private readonly string _cacheLockPath;
 16        private readonly int _cacheRetryCount;
 17        private readonly TimeSpan _cacheRetryDelay;
 18        private DateTimeOffset _lastReadTime;
 19
 020        public MsalCacheReader(ITokenCache cache, string cachePath, int cacheRetryCount, TimeSpan cacheRetryDelay)
 21        {
 022            _cachePath = cachePath;
 23
 024            _cacheLockPath = cachePath + ".lockfile";
 25
 026            _cacheRetryCount = cacheRetryCount;
 27
 028            _cacheRetryDelay = cacheRetryDelay;
 29
 030            cache.SetBeforeAccessAsync(OnBeforeAccessAsync);
 031        }
 32
 33        private async Task OnBeforeAccessAsync(TokenCacheNotificationArgs args)
 34        {
 35            try
 36            {
 037                DateTime cacheTimestamp = File.GetLastWriteTimeUtc(_cachePath);
 38
 039                if (File.Exists(_cachePath) && _lastReadTime < cacheTimestamp)
 40                {
 041                    using (SentinelFileLock cacheLock = await SentinelFileLock.AcquireAsync(_cacheLockPath, _cacheRetryC
 42                    {
 043                        byte[] cacheBytesFromDisk = await ReadCacheFromProtectedStorageAsync().ConfigureAwait(false);
 44
 45                        // update the last read time before deserialization so if deserialization fails we won't continu
 046                        _lastReadTime = cacheTimestamp;
 47
 048                        if (cacheBytesFromDisk != null)
 49                        {
 050                            args.TokenCache.DeserializeMsalV3(cacheBytesFromDisk, shouldClearExistingCache: false);
 51                        }
 052                    }
 53                }
 054            }
 055            catch (Exception)
 56            {
 57                // log
 058            }
 059        }
 60
 61        private async Task<byte[]> ReadCacheFromProtectedStorageAsync()
 62        {
 063            using (FileStream file = File.OpenRead(_cachePath))
 64            {
 065                byte[] protectedBytes = new byte[file.Length];
 66
 067                await file.ReadAsync(protectedBytes, 0, protectedBytes.Length).ConfigureAwait(false);
 68
 069                return ProtectedData.Unprotect(protectedBytes, optionalEntropy: null, scope: DataProtectionScope.Current
 70            }
 071        }
 72    }
 73
 74}