< Summary

Class:Azure.Identity.MsalClientBase`1
Assembly:Azure.Identity
File(s):C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\MsalClientBase.cs
Covered lines:22
Uncovered lines:19
Coverable lines:41
Total lines:108
Line coverage:53.6% (22 of 41)
Covered branches:7
Total branches:14
Branch coverage:50% (7 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
.ctor(...)-100%100%
get_TenantId()-100%100%
get_ClientId()-100%100%
get_EnablePersistentCache()-100%100%
get_AllowUnencryptedCache()-0%100%
get_Pipeline()-100%100%
get_Client()-100%100%
EnsureInitializedAsync()-100%100%
InitializeAsync()-14.29%12.5%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Threading.Tasks;
 6using Microsoft.Identity.Client;
 7using Microsoft.Identity.Client.Extensions.Msal;
 8
 9namespace Azure.Identity
 10{
 11    internal abstract class MsalClientBase<TClient>
 12        where TClient : IClientApplicationBase
 13    {
 14        private readonly Lazy<Task> _ensureInitAsync;
 15
 16        /// <summary>
 17        /// For mocking purposes only.
 18        /// </summary>
 13219        protected MsalClientBase()
 20        {
 13221        }
 22
 32423        protected MsalClientBase(CredentialPipeline pipeline, string tenantId, string clientId, ITokenCacheOptions cache
 24        {
 32425            Pipeline = pipeline;
 26
 32427            TenantId = tenantId;
 28
 32429            ClientId = clientId;
 30
 32431            EnablePersistentCache = cacheOptions?.EnablePersistentCache ?? false;
 32
 32433            AllowUnencryptedCache = cacheOptions?.AllowUnencryptedCache ?? false;
 34
 32435            _ensureInitAsync = new Lazy<Task>(InitializeAsync);
 32436        }
 37
 13238        protected string TenantId { get; }
 39
 13240        protected string ClientId { get; }
 41
 11642        protected bool EnablePersistentCache { get; }
 43
 044        protected bool AllowUnencryptedCache { get; }
 45
 26446        protected CredentialPipeline Pipeline { get; }
 47
 28048        protected TClient Client { get; private set; }
 49
 50        protected abstract Task<TClient> CreateClientAsync();
 51
 52        protected async Task EnsureInitializedAsync(bool async)
 53        {
 19654            if (async)
 55            {
 12856                await _ensureInitAsync.Value.ConfigureAwait(false);
 57            }
 58            else
 59            {
 60#pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult().
 6861                _ensureInitAsync.Value.GetAwaiter().GetResult();
 62#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult().
 63            }
 16464        }
 65
 66        private async Task InitializeAsync()
 67        {
 13268            Client = await CreateClientAsync().ConfigureAwait(false);
 69
 11670            if (EnablePersistentCache)
 71            {
 72                MsalCacheHelper cacheHelper;
 73
 074                StorageCreationProperties storageProperties = new StorageCreationPropertiesBuilder(Constants.DefaultMsal
 075                    .WithMacKeyChain(Constants.DefaultMsalTokenCacheKeychainService, Constants.DefaultMsalTokenCacheKeyc
 076                    .WithLinuxKeyring(Constants.DefaultMsalTokenCacheKeyringSchema, Constants.DefaultMsalTokenCacheKeyri
 077                    .Build();
 78
 79                try
 80                {
 081                    cacheHelper = await MsalCacheHelper.CreateAsync(storageProperties).ConfigureAwait(false);
 82
 083                    cacheHelper.VerifyPersistence();
 084                }
 085                catch (MsalCachePersistenceException)
 86                {
 087                    if (AllowUnencryptedCache)
 88                    {
 089                        storageProperties = new StorageCreationPropertiesBuilder(Constants.DefaultMsalTokenCacheName, Co
 090                            .WithMacKeyChain(Constants.DefaultMsalTokenCacheKeychainService, Constants.DefaultMsalTokenC
 091                            .WithLinuxUnprotectedFile()
 092                            .Build();
 93
 094                        cacheHelper = await MsalCacheHelper.CreateAsync(storageProperties).ConfigureAwait(false);
 95
 096                        cacheHelper.VerifyPersistence();
 97                    }
 98                    else
 99                    {
 0100                        throw;
 101                    }
 102                }
 103
 0104                cacheHelper.RegisterCache(Client.UserTokenCache);
 0105            }
 116106        }
 107    }
 108}