This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
MsalCacheHelper.cs
428 lines (367 loc) · 17.5 KB
/
MsalCacheHelper.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.Identity.Client.Extensions.Msal
{
/// <summary>
/// Helper to create the token cache
/// </summary>
public class MsalCacheHelper
{
/// <summary>
/// The name of the Default KeyRing collection. Secrets stored in this collection are persisted to disk
/// </summary>
public const string LinuxKeyRingDefaultCollection = "default";
/// <summary>
/// The name of the Session KeyRing collection. Secrets stored in this collection are not persisted to disk, but
/// will be available for the duration of the user session.
/// </summary>
public const string LinuxKeyRingSessionCollection = "session";
/// <summary>
/// A default logger for use if the user doesn't want to provide their own.
/// </summary>
private static readonly Lazy<TraceSourceLogger> s_staticLogger = new Lazy<TraceSourceLogger>(() =>
{
return new TraceSourceLogger(EnvUtils.GetNewTraceSource(nameof(MsalCacheHelper) + "Singleton"));
});
/// <summary>
/// A lock object for serialization
/// </summary>
private readonly object _lockObject = new object();
/// <summary>
/// Properties used to create storage on disk.
/// </summary>
private readonly StorageCreationProperties _storageCreationProperties;
/// <summary>
/// Holds a lock object when this helper is accessing the cache. Null otherwise.
/// </summary>
internal CrossPlatLock CacheLock { get; private set; }
/// <summary>
/// Storage that handles the storing of the adal cache file on disk. Internal for testing.
/// </summary>
internal /* internal for testing only */ MsalCacheStorage CacheStore { get; }
/// <summary>
/// Logger to log events to.
/// </summary>
private readonly TraceSourceLogger _logger;
/// <summary>
/// Contains a list of accounts that we know about. This is used as a 'before' list when the cache is changed on disk,
/// so that we know which accounts were added and removed. Used when sending the <see cref="CacheChanged"/> event.
/// </summary>
private HashSet<string> _knownAccountIds;
/// <summary>
/// Watches a filesystem location in order to fire events when the cache on disk is changed. Internal for testing.
/// </summary>
internal readonly FileSystemWatcher _cacheWatcher;
/// <summary>
/// Allows clients to listen for cache updates originating from disk.
/// </summary>
public event EventHandler<CacheChangedEventArgs> CacheChanged;
/// <summary>
/// Contains a reference to all caches currently registered to synchronize with this MsalCacheHelper, along with
/// timestamp of the cache file the last time they deserialized.
/// </summary>
internal readonly HashSet<ITokenCache> _registeredCaches = new HashSet<ITokenCache>();
/// <summary>
/// Gets the current set of accounts in the cache by creating a new public client, and
/// deserializing the cache into a temporary object.
/// </summary>
private static async Task<HashSet<string>> GetAccountIdentifiersAsync(StorageCreationProperties storageCreationProperties)
{
var accountIdentifiers = new HashSet<string>();
if (File.Exists(storageCreationProperties.CacheFilePath))
{
var pca = PublicClientApplicationBuilder.Create(storageCreationProperties.ClientId).Build();
pca.UserTokenCache.SetBeforeAccess((args) =>
{
var tempCache = MsalCacheStorage.Create(storageCreationProperties, s_staticLogger.Value.Source);
// We're using ReadData here so that decryption is gets handled within the store.
var data = tempCache.ReadData();
args.TokenCache.DeserializeMsalV3(data);
});
var accounts = await pca.GetAccountsAsync().ConfigureAwait(false);
foreach (var account in accounts)
{
accountIdentifiers.Add(account.HomeAccountId.Identifier);
}
}
return accountIdentifiers;
}
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="storageCreationProperties">Properties to use when creating storage on disk.</param>
/// <param name="logger">Passing null uses a default logger</param>
/// <param name="knownAccountIds">The set of known accounts</param>
/// <param name="cacheWatcher">Watcher for the cache file, to enable sending updated events</param>
private MsalCacheHelper(
StorageCreationProperties storageCreationProperties,
TraceSource logger,
HashSet<string> knownAccountIds,
FileSystemWatcher cacheWatcher)
{
_logger = logger == null ? s_staticLogger.Value : new TraceSourceLogger(logger);
_storageCreationProperties = storageCreationProperties;
CacheStore = MsalCacheStorage.Create(_storageCreationProperties, _logger.Source);
_knownAccountIds = knownAccountIds;
_cacheWatcher = cacheWatcher;
_cacheWatcher.Changed += OnCacheFileChangedAsync;
_cacheWatcher.Deleted += OnCacheFileChangedAsync;
}
private async void OnCacheFileChangedAsync(object sender, FileSystemEventArgs args)
{
// avoid the high cost of computing the added / removed accounts if nobody listens to this
if (CacheChanged == null)
{
return;
}
try
{
IEnumerable<string> added;
IEnumerable<string> removed;
using (CreateCrossPlatLock(_storageCreationProperties))
{
var currentAccountIds = await GetAccountIdentifiersAsync(_storageCreationProperties).ConfigureAwait(false);
var intersect = currentAccountIds.Intersect(_knownAccountIds);
removed = _knownAccountIds.Except(intersect);
added = currentAccountIds.Except(intersect);
_knownAccountIds = currentAccountIds;
}
if (added.Any() || removed.Any())
{
CacheChanged.Invoke(sender, new CacheChangedEventArgs(added, removed));
}
}
catch (Exception e)
{
// Never let this throw, just log errors
_logger.LogWarning($"Exception within File Watcher : {e}");
}
}
/// <summary>
/// An internal constructor allowing unit tests to data explicitly rather than initializing here.
/// </summary>
/// <param name="userTokenCache">The token cache to synchronize with the backing store</param>
/// <param name="store">The backing store to use.</param>
/// <param name="logger">Passing null uses the default logger</param>
internal MsalCacheHelper(ITokenCache userTokenCache, MsalCacheStorage store, TraceSource logger = null)
{
_logger = logger == null ? s_staticLogger.Value : new TraceSourceLogger(logger);
CacheStore = store;
_storageCreationProperties = store.StorageCreationProperties;
RegisterCache(userTokenCache);
}
#region Public API
/// <summary>
/// Creates a new instance of <see cref="MsalCacheHelper"/>. To configure MSAL to use this cache persistence, call <see cref="RegisterCache(ITokenCache)"/>
/// </summary>
/// <param name="storageCreationProperties">Properties to use when creating storage on disk.</param>
/// <param name="logger">Passing null uses a default logger</param>
/// <returns>A new instance of <see cref="MsalCacheHelper"/>.</returns>
public static async Task<MsalCacheHelper> CreateAsync(StorageCreationProperties storageCreationProperties, TraceSource logger = null)
{
if (storageCreationProperties is null)
{
throw new ArgumentNullException(nameof(storageCreationProperties));
}
// We want CrossPlatLock around this operation so that we don't have a race against first read of the file and creating the watcher
using (CreateCrossPlatLock(storageCreationProperties))
{
// Cache the list of accounts
var accountIdentifiers = await GetAccountIdentifiersAsync(storageCreationProperties).ConfigureAwait(false);
var cacheWatcher = new FileSystemWatcher(storageCreationProperties.CacheDirectory, storageCreationProperties.CacheFileName);
var helper = new MsalCacheHelper(storageCreationProperties, logger, accountIdentifiers, cacheWatcher);
cacheWatcher.EnableRaisingEvents = true;
return helper;
}
}
/// <summary>
/// Gets the user's root directory across platforms.
/// </summary>
public static string UserRootDirectory
{
get
{
return SharedUtilities.GetUserRootDirectory();
}
}
/// <summary>
/// Registers a token cache to synchronize with on disk storage.
/// </summary>
/// <param name="tokenCache">Token Cache</param>
public void RegisterCache(ITokenCache tokenCache)
{
if (tokenCache == null)
{
throw new ArgumentNullException(nameof(tokenCache));
}
lock (_lockObject)
{
_logger.LogInformation($"Registering token cache with on disk storage");
if (_registeredCaches.Contains(tokenCache))
{
_logger.LogWarning($"Redundant registration of {nameof(tokenCache)} in {nameof(MsalCacheHelper)}, skipping further registration.");
return;
}
tokenCache.SetBeforeAccess(BeforeAccessNotification);
tokenCache.SetAfterAccess(AfterAccessNotification);
_logger.LogInformation($"Initializing msal cache");
}
_registeredCaches.Add(tokenCache); // Ignore return value, since we already bail if _registeredCaches contains tokenCache earlier
_logger.LogInformation($"Done initializing");
}
/// <summary>
/// Unregisters a token cache so it no longer synchronizes with on disk storage.
/// </summary>
/// <param name="tokenCache"></param>
public void UnregisterCache(ITokenCache tokenCache)
{
lock (_lockObject)
{
_logger.LogInformation($"Unregistering token cache from on disk storage");
if (_registeredCaches.Contains(tokenCache))
{
_registeredCaches.Remove(tokenCache);
tokenCache.SetBeforeAccess(args => { });
tokenCache.SetAfterAccess(args => { });
}
else
{
_logger.LogWarning($"Attempting to unregister an already unregistered {nameof(tokenCache)} in {nameof(MsalCacheHelper)}");
}
}
}
/// <summary>
/// Clears the token store
/// </summary>
public void Clear()
{
CacheStore.Clear();
}
/// <summary>
/// Extracts the token cache data from the persistent store
/// </summary>
/// <returns>an UTF-8 byte array of the unencrypted token cache</returns>
/// <remarks>This method should be used with care. The data returned is unencrypted.</remarks>
public byte[] LoadUnencryptedTokenCache()
{
using (CreateCrossPlatLock(_storageCreationProperties))
{
return CacheStore.ReadData(ignoreExceptions: false);
}
}
/// <summary>
/// Saves an unencrypted, UTF-8 encoded byte array representing an MSAL token cache.
/// The save operation will persist the data in a secure location, as configured in <see cref="StorageCreationProperties"/>
/// </summary>
public void SaveUnencryptedTokenCache(byte[] tokenCache)
{
using (CreateCrossPlatLock(_storageCreationProperties))
{
CacheStore.WriteData(tokenCache, ignoreExceptions: false);
}
}
#endregion
/// <summary>
/// Gets a new instance of a lock for synchronizing against a cache made with the same creation properties.
/// </summary>
private static CrossPlatLock CreateCrossPlatLock(StorageCreationProperties storageCreationProperties)
{
return new CrossPlatLock(
storageCreationProperties.CacheFilePath + ".lockfile",
storageCreationProperties.LockRetryDelay,
storageCreationProperties.LockRetryCount);
}
/// <summary>
/// Before cache access
/// </summary>
/// <param name="args">Callback parameters from MSAL</param>
internal void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
_logger.LogInformation($"Before access");
_logger.LogInformation($"Acquiring lock for token cache");
// OK, we have two nested locks here. We need to maintain a clear ordering to avoid deadlocks.
// 1. Use the CrossPlatLock which is respected by all processes and is used around all cache accesses.
// 2. Use _lockObject which is used in UnregisterCache, and is needed for all accesses of _registeredCaches.
CacheLock = CreateCrossPlatLock(_storageCreationProperties);
_logger.LogInformation($"Before access, the store has changed");
var cachedStoreData = CacheStore.ReadData();
_logger.LogInformation($"Read '{cachedStoreData?.Length}' bytes from storage");
lock (_lockObject)
{
try
{
_logger.LogInformation($"Deserializing the store");
args.TokenCache.DeserializeMsalV3(cachedStoreData, shouldClearExistingCache: true);
}
catch (Exception e)
{
_logger.LogError($"An exception was encountered while deserializing the {nameof(MsalCacheHelper)} : {e}");
_logger.LogError($"No data found in the store, clearing the cache in memory.");
// Clear the memory cache
Clear();
throw;
}
}
}
/// <summary>
/// After cache access
/// </summary>
/// <param name="args">Callback parameters from MSAL</param>
internal void AfterAccessNotification(TokenCacheNotificationArgs args)
{
try
{
_logger.LogInformation($"After access");
// if the access operation resulted in a cache update
if (args.HasStateChanged)
{
_logger.LogInformation($"After access, cache in memory HasChanged");
try
{
_logger.LogInformation($"Before Write Store");
byte[] data = args.TokenCache.SerializeMsalV3();
_logger.LogInformation($"Serializing '{data.Length}' bytes");
CacheStore.WriteData(data);
_logger.LogInformation($"After write store");
}
catch (Exception e)
{
_logger.LogError($"An exception was encountered while serializing the {nameof(MsalCacheHelper)} : {e}");
_logger.LogError($"No data found in the store, clearing the cache in memory.");
// The cache is corrupt clear it out
Clear();
throw;
}
}
}
finally
{
ReleaseFileLock();
}
}
private void ReleaseFileLock()
{
// Get a local copy and call null before disposing because when the lock is disposed the next thread will replace CacheLock with its instance,
// therefore we do not want to null out CacheLock after dispose since this may orphan a CacheLock.
var localDispose = CacheLock;
CacheLock = null;
localDispose?.Dispose();
_logger.LogInformation($"Released lock");
}
/// <summary>
/// Performs a write -> read -> clear using the underlying persistence mechanism and
/// throws an <see cref="MsalCachePersistenceException"/> if something goes wrong.
/// </summary>
/// <remarks>Does not overwrite the token cache. Should never fail on Windows and Mac where the cache accessors are guaranteed to exist by the OS.</remarks>
public void VerifyPersistence()
{
CacheStore.VerifyPersistence();
}
}
}