| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Microsoft.Identity.Client; |
| | | 7 | | using Microsoft.Identity.Client.Extensions.Msal; |
| | | 8 | | |
| | | 9 | | namespace 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> |
| | 132 | 19 | | protected MsalClientBase() |
| | | 20 | | { |
| | 132 | 21 | | } |
| | | 22 | | |
| | 324 | 23 | | protected MsalClientBase(CredentialPipeline pipeline, string tenantId, string clientId, ITokenCacheOptions cache |
| | | 24 | | { |
| | 324 | 25 | | Pipeline = pipeline; |
| | | 26 | | |
| | 324 | 27 | | TenantId = tenantId; |
| | | 28 | | |
| | 324 | 29 | | ClientId = clientId; |
| | | 30 | | |
| | 324 | 31 | | EnablePersistentCache = cacheOptions?.EnablePersistentCache ?? false; |
| | | 32 | | |
| | 324 | 33 | | AllowUnencryptedCache = cacheOptions?.AllowUnencryptedCache ?? false; |
| | | 34 | | |
| | 324 | 35 | | _ensureInitAsync = new Lazy<Task>(InitializeAsync); |
| | 324 | 36 | | } |
| | | 37 | | |
| | 132 | 38 | | protected string TenantId { get; } |
| | | 39 | | |
| | 132 | 40 | | protected string ClientId { get; } |
| | | 41 | | |
| | 116 | 42 | | protected bool EnablePersistentCache { get; } |
| | | 43 | | |
| | 0 | 44 | | protected bool AllowUnencryptedCache { get; } |
| | | 45 | | |
| | 264 | 46 | | protected CredentialPipeline Pipeline { get; } |
| | | 47 | | |
| | 280 | 48 | | protected TClient Client { get; private set; } |
| | | 49 | | |
| | | 50 | | protected abstract Task<TClient> CreateClientAsync(); |
| | | 51 | | |
| | | 52 | | protected async Task EnsureInitializedAsync(bool async) |
| | | 53 | | { |
| | 196 | 54 | | if (async) |
| | | 55 | | { |
| | 128 | 56 | | await _ensureInitAsync.Value.ConfigureAwait(false); |
| | | 57 | | } |
| | | 58 | | else |
| | | 59 | | { |
| | | 60 | | #pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). |
| | 68 | 61 | | _ensureInitAsync.Value.GetAwaiter().GetResult(); |
| | | 62 | | #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). |
| | | 63 | | } |
| | 164 | 64 | | } |
| | | 65 | | |
| | | 66 | | private async Task InitializeAsync() |
| | | 67 | | { |
| | 132 | 68 | | Client = await CreateClientAsync().ConfigureAwait(false); |
| | | 69 | | |
| | 116 | 70 | | if (EnablePersistentCache) |
| | | 71 | | { |
| | | 72 | | MsalCacheHelper cacheHelper; |
| | | 73 | | |
| | 0 | 74 | | StorageCreationProperties storageProperties = new StorageCreationPropertiesBuilder(Constants.DefaultMsal |
| | 0 | 75 | | .WithMacKeyChain(Constants.DefaultMsalTokenCacheKeychainService, Constants.DefaultMsalTokenCacheKeyc |
| | 0 | 76 | | .WithLinuxKeyring(Constants.DefaultMsalTokenCacheKeyringSchema, Constants.DefaultMsalTokenCacheKeyri |
| | 0 | 77 | | .Build(); |
| | | 78 | | |
| | | 79 | | try |
| | | 80 | | { |
| | 0 | 81 | | cacheHelper = await MsalCacheHelper.CreateAsync(storageProperties).ConfigureAwait(false); |
| | | 82 | | |
| | 0 | 83 | | cacheHelper.VerifyPersistence(); |
| | 0 | 84 | | } |
| | 0 | 85 | | catch (MsalCachePersistenceException) |
| | | 86 | | { |
| | 0 | 87 | | if (AllowUnencryptedCache) |
| | | 88 | | { |
| | 0 | 89 | | storageProperties = new StorageCreationPropertiesBuilder(Constants.DefaultMsalTokenCacheName, Co |
| | 0 | 90 | | .WithMacKeyChain(Constants.DefaultMsalTokenCacheKeychainService, Constants.DefaultMsalTokenC |
| | 0 | 91 | | .WithLinuxUnprotectedFile() |
| | 0 | 92 | | .Build(); |
| | | 93 | | |
| | 0 | 94 | | cacheHelper = await MsalCacheHelper.CreateAsync(storageProperties).ConfigureAwait(false); |
| | | 95 | | |
| | 0 | 96 | | cacheHelper.VerifyPersistence(); |
| | | 97 | | } |
| | | 98 | | else |
| | | 99 | | { |
| | 0 | 100 | | throw; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | cacheHelper.RegisterCache(Client.UserTokenCache); |
| | 0 | 105 | | } |
| | 116 | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |