-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
TokenCache.cs
289 lines (242 loc) · 11.5 KB
/
TokenCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Extensions.Msal;
namespace Azure.Identity
{
/// <summary>
/// A cache for Tokens.
/// </summary>
#pragma warning disable CA1001 // Types that own disposable fields should be disposable
// SemaphoreSlim only needs to be disposed when AvailableWaitHandle is called.
internal class TokenCache
#pragma warning restore CA1001 // Types that own disposable fields should be disposable
{
private readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
private DateTimeOffset _lastUpdated;
private ConditionalWeakTable<object, CacheTimestamp> _cacheAccessMap;
internal Func<IPublicClientApplication> _publicClientApplicationFactory;
private readonly bool _allowUnencryptedStorage;
private readonly string _name;
private readonly bool _persistToDisk;
private AsyncLockWithValue<MsalCacheHelperWrapper> cacheHelperLock = new AsyncLockWithValue<MsalCacheHelperWrapper>();
private readonly MsalCacheHelperWrapper _cacheHelperWrapper;
/// <summary>
/// The internal state of the cache.
/// </summary>
internal byte[] Data { get; private set; }
/// <summary>
/// Determines whether the token cache will be associated with CAE enabled requests.
/// </summary>
/// <value>If true, this cache services only CAE enabled requests.Otherwise, this cache services non-CAE enabled requests.</value>
internal bool IsCaeEnabled { get;}
private class CacheTimestamp
{
private DateTimeOffset _timestamp;
public CacheTimestamp()
{
Update();
}
public DateTimeOffset Update()
{
_timestamp = DateTimeOffset.UtcNow;
return _timestamp;
}
public DateTimeOffset Value { get { return _timestamp; } }
}
/// <summary>
/// Creates a new instance of <see cref="TokenCache"/> with the specified options.
/// </summary>
/// <param name="options">Options controlling the storage of the <see cref="TokenCache"/>.</param>
/// <param name="enableCae">Controls whether this cache will be associated with CAE requests or non-CAE requests.</param>
public TokenCache(TokenCachePersistenceOptions options = null, bool enableCae = false)
: this(options, default, default, enableCae)
{ }
internal TokenCache(TokenCachePersistenceOptions options, MsalCacheHelperWrapper cacheHelperWrapper, Func<IPublicClientApplication> publicApplicationFactory = null, bool enableCae = false)
{
_cacheHelperWrapper = cacheHelperWrapper ?? new MsalCacheHelperWrapper();
_publicClientApplicationFactory = publicApplicationFactory ?? new Func<IPublicClientApplication>(() => PublicClientApplicationBuilder.Create(Guid.NewGuid().ToString()).Build());
IsCaeEnabled = enableCae;
if (options is UnsafeTokenCacheOptions inMemoryOptions)
{
TokenCacheUpdatedAsync = inMemoryOptions.TokenCacheUpdatedAsync;
RefreshCacheFromOptionsAsync = inMemoryOptions.RefreshCacheAsync;
_lastUpdated = DateTimeOffset.UtcNow;
_cacheAccessMap = new ConditionalWeakTable<object, CacheTimestamp>();
}
else
{
_allowUnencryptedStorage = options?.UnsafeAllowUnencryptedStorage ?? false;
_name = (options?.Name ?? Constants.DefaultMsalTokenCacheName) + (enableCae ? Constants.CaeEnabledCacheSuffix : Constants.CaeDisabledCacheSuffix);
_persistToDisk = true;
}
}
/// <summary>
/// A delegate that is called with the cache contents when the underlying <see cref="TokenCache"/> has been updated.
/// </summary>
internal Func<TokenCacheUpdatedArgs, Task> TokenCacheUpdatedAsync;
/// <summary>
/// A delegate that will be called before the cache is accessed. The data returned will be used to set the current state of the cache.
/// </summary>
internal Func<TokenCacheRefreshArgs, CancellationToken, Task<TokenCacheData>> RefreshCacheFromOptionsAsync;
internal virtual async Task RegisterCache(bool async, ITokenCache tokenCache, CancellationToken cancellationToken)
{
if (_persistToDisk)
{
MsalCacheHelperWrapper cacheHelper = await GetCacheHelperAsync(async, cancellationToken).ConfigureAwait(false);
cacheHelper.RegisterCache(tokenCache);
}
else
{
if (async)
{
await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);
}
else
{
_lock.Wait(cancellationToken);
}
try
{
if (!_cacheAccessMap.TryGetValue(tokenCache, out _))
{
tokenCache.SetBeforeAccessAsync(OnBeforeCacheAccessAsync);
tokenCache.SetAfterAccessAsync(OnAfterCacheAccessAsync);
_cacheAccessMap.Add(tokenCache, new CacheTimestamp());
}
}
finally
{
_lock.Release();
}
}
}
private async Task OnBeforeCacheAccessAsync(TokenCacheNotificationArgs args)
{
await _lock.WaitAsync().ConfigureAwait(false);
try
{
if (RefreshCacheFromOptionsAsync != null)
{
Data = (await RefreshCacheFromOptionsAsync(new TokenCacheRefreshArgs(args, IsCaeEnabled), default).ConfigureAwait(false))
.CacheBytes.ToArray();
}
args.TokenCache.DeserializeMsalV3(Data, true);
_cacheAccessMap.GetOrCreateValue(args.TokenCache).Update();
}
finally
{
_lock.Release();
}
}
private async Task OnAfterCacheAccessAsync(TokenCacheNotificationArgs args)
{
if (args.HasStateChanged)
{
await UpdateCacheDataAsync(args.TokenCache).ConfigureAwait(false);
}
}
private async Task UpdateCacheDataAsync(ITokenCacheSerializer tokenCache)
{
await _lock.WaitAsync().ConfigureAwait(false);
try
{
if (!_cacheAccessMap.TryGetValue(tokenCache, out CacheTimestamp lastRead) || lastRead.Value < _lastUpdated)
{
Data = await MergeCacheData(Data, tokenCache.SerializeMsalV3()).ConfigureAwait(false);
}
else
{
Data = tokenCache.SerializeMsalV3();
}
if (TokenCacheUpdatedAsync != null)
{
var eventBytes = Data.ToArray();
await TokenCacheUpdatedAsync(new TokenCacheUpdatedArgs(eventBytes, IsCaeEnabled)).ConfigureAwait(false);
}
_lastUpdated = _cacheAccessMap.GetOrCreateValue(tokenCache).Update();
}
finally
{
_lock.Release();
}
}
private async Task<byte[]> MergeCacheData(byte[] cacheA, byte[] cacheB)
{
byte[] merged = null;
IPublicClientApplication client = _publicClientApplicationFactory();
client.UserTokenCache.SetBeforeAccess(args => args.TokenCache.DeserializeMsalV3(cacheA));
await client.GetAccountsAsync().ConfigureAwait(false);
client.UserTokenCache.SetBeforeAccess(args => args.TokenCache.DeserializeMsalV3(cacheB, shouldClearExistingCache: false));
client.UserTokenCache.SetAfterAccess(args => merged = args.TokenCache.SerializeMsalV3());
await client.GetAccountsAsync().ConfigureAwait(false);
return merged;
}
private async Task<MsalCacheHelperWrapper> GetCacheHelperAsync(bool async, CancellationToken cancellationToken)
{
using var asyncLock = await cacheHelperLock.GetLockOrValueAsync(async, cancellationToken).ConfigureAwait(false);
if (asyncLock.HasValue)
{
return asyncLock.Value;
}
MsalCacheHelperWrapper cacheHelper;
try
{
cacheHelper = await GetProtectedCacheHelperAsync(async, _name).ConfigureAwait(false);
cacheHelper.VerifyPersistence();
}
catch (MsalCachePersistenceException)
{
if (_allowUnencryptedStorage)
{
cacheHelper = await GetFallbackCacheHelperAsync(async, _name).ConfigureAwait(false);
cacheHelper.VerifyPersistence();
}
else
{
throw;
}
}
asyncLock.SetValue(cacheHelper);
return cacheHelper;
}
private async Task<MsalCacheHelperWrapper> GetProtectedCacheHelperAsync(bool async, string name)
{
StorageCreationProperties storageProperties = new StorageCreationPropertiesBuilder(name, Constants.DefaultMsalTokenCacheDirectory)
.WithMacKeyChain(Constants.DefaultMsalTokenCacheKeychainService, name)
.WithLinuxKeyring(Constants.DefaultMsalTokenCacheKeyringSchema, Constants.DefaultMsalTokenCacheKeyringCollection, name, Constants.DefaultMsaltokenCacheKeyringAttribute1, Constants.DefaultMsaltokenCacheKeyringAttribute2)
.Build();
MsalCacheHelperWrapper cacheHelper = await InitializeCacheHelper(async, storageProperties).ConfigureAwait(false);
return cacheHelper;
}
private async Task<MsalCacheHelperWrapper> GetFallbackCacheHelperAsync(bool async, string name = Constants.DefaultMsalTokenCacheName)
{
StorageCreationProperties storageProperties = new StorageCreationPropertiesBuilder(name, Constants.DefaultMsalTokenCacheDirectory)
.WithMacKeyChain(Constants.DefaultMsalTokenCacheKeychainService, name)
.WithLinuxUnprotectedFile()
.Build();
MsalCacheHelperWrapper cacheHelper = await InitializeCacheHelper(async, storageProperties).ConfigureAwait(false);
return cacheHelper;
}
private async Task<MsalCacheHelperWrapper> InitializeCacheHelper(bool async, StorageCreationProperties storageProperties)
{
if (async)
{
await _cacheHelperWrapper.InitializeAsync(storageProperties).ConfigureAwait(false);
}
else
{
#pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extension method instead.
_cacheHelperWrapper.InitializeAsync(storageProperties).GetAwaiter().GetResult();
#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extension method instead.
}
return _cacheHelperWrapper;
}
}
}