-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathMsalMemoryTokenCacheProvider.cs
126 lines (112 loc) · 5.05 KB
/
MsalMemoryTokenCacheProvider.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
namespace Microsoft.Identity.Web.TokenCacheProviders.InMemory
{
/// <summary>
/// An implementation of token cache for both Confidential and Public clients backed by MemoryCache.
/// </summary>
/// <seealso>https://aka.ms/msal-net-token-cache-serialization</seealso>
public class MsalMemoryTokenCacheProvider : MsalAbstractTokenCacheProvider
{
/// <summary>
/// .NET Core Memory cache.
/// </summary>
private readonly IMemoryCache _memoryCache;
/// <summary>
/// MSAL memory token cache options.
/// </summary>
private readonly MsalMemoryTokenCacheOptions _cacheOptions;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="memoryCache">serialization cache.</param>
/// <param name="cacheOptions">Memory cache options.</param>
public MsalMemoryTokenCacheProvider(
IMemoryCache memoryCache,
IOptions<MsalMemoryTokenCacheOptions> cacheOptions)
{
_ = Throws.IfNull(cacheOptions);
_memoryCache = memoryCache;
_cacheOptions = cacheOptions.Value;
}
/// <summary>
/// Removes a token cache identified by its key, from the serialization
/// cache.
/// </summary>
/// <param name="cacheKey">token cache key.</param>
/// <returns>A <see cref="Task"/> that completes when key removal has completed.</returns>
protected override Task RemoveKeyAsync(string cacheKey)
{
_memoryCache.Remove(cacheKey);
return Task.CompletedTask;
}
/// <summary>
/// Reads a blob from the serialization cache (identified by its key).
/// </summary>
/// <param name="cacheKey">Token cache key.</param>
/// <returns>Read Bytes.</returns>
protected override Task<byte[]?> ReadCacheBytesAsync(string cacheKey)
{
byte[]? tokenCacheBytes = (byte[]?)_memoryCache.Get(cacheKey);
return Task.FromResult(tokenCacheBytes);
}
/// <summary>
/// Method to be overridden by concrete cache serializers to Read the cache bytes.
/// </summary>
/// <param name="cacheKey">Cache key.</param>
/// <param name="cacheSerializerHints">Hints for the cache serialization implementation optimization.</param>
/// <returns>Read bytes.</returns>
protected override Task<byte[]?> ReadCacheBytesAsync(string cacheKey, CacheSerializerHints cacheSerializerHints)
{
byte[]? tokenCacheBytes = (byte[]?)_memoryCache.Get(cacheKey);
if (tokenCacheBytes != null && cacheSerializerHints.TelemetryData != null)
{
cacheSerializerHints.TelemetryData.CacheLevel = Client.Cache.CacheLevel.L1Cache;
}
return Task.FromResult(tokenCacheBytes);
}
/// <summary>
/// Writes a token cache blob to the serialization cache (identified by its key).
/// </summary>
/// <param name="cacheKey">Token cache key.</param>
/// <param name="bytes">Bytes to write.</param>
/// <returns>A <see cref="Task"/> that completes when a write operation has completed.</returns>
protected override Task WriteCacheBytesAsync(string cacheKey, byte[] bytes)
{
return WriteCacheBytesAsync(cacheKey, bytes, new CacheSerializerHints());
}
/// <summary>
/// Writes a token cache blob to the serialization cache (identified by its key).
/// </summary>
/// <param name="cacheKey">Token cache key.</param>
/// <param name="bytes">Bytes to write.</param>
/// <param name="cacheSerializerHints">Hints for the cache serialization implementation optimization.</param>
/// <returns>A <see cref="Task"/> that completes when a write operation has completed.</returns>
protected override Task WriteCacheBytesAsync(
string cacheKey,
byte[] bytes,
CacheSerializerHints cacheSerializerHints)
{
TimeSpan? cacheExpiry = null;
if (cacheSerializerHints != null && cacheSerializerHints.SuggestedCacheExpiry != null)
{
cacheExpiry = cacheSerializerHints.SuggestedCacheExpiry.Value.UtcDateTime - DateTime.UtcNow;
if (cacheExpiry < TimeSpan.Zero)
{
cacheExpiry = TimeSpan.FromMilliseconds(1);
}
}
MemoryCacheEntryOptions memoryCacheEntryOptions = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = cacheExpiry ?? _cacheOptions.AbsoluteExpirationRelativeToNow,
Size = bytes?.Length,
};
_memoryCache.Set(cacheKey, bytes, memoryCacheEntryOptions);
return Task.CompletedTask;
}
}
}