forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenIdApplicationStore.cs
More file actions
291 lines (258 loc) · 10.7 KB
/
Copy pathOpenIdApplicationStore.cs
File metadata and controls
291 lines (258 loc) · 10.7 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using DotNetOpenAuth.Messaging.Bindings;
using DotNetOpenAuth.OpenId;
namespace ServiceStack.Auth.Tests
{
public class InMemoryOpenIdApplicationStore : IOpenIdApplicationStore, ICryptoKeyStore, INonceStore
{
/// <summary>
/// How frequently to check for and remove expired secrets.
/// </summary>
private static readonly TimeSpan cleaningInterval = TimeSpan.FromMinutes(30);
/// <summary>
/// An in-memory cache of decrypted symmetric keys.
/// </summary>
/// <remarks>
/// The key is the bucket name. The value is a dictionary whose key is the handle and whose value is the cached key.
/// </remarks>
private readonly Dictionary<string, Dictionary<string, CryptoKey>> store = new Dictionary<string, Dictionary<string, CryptoKey>>(StringComparer.Ordinal);
/// <summary>
/// The last time the cache had expired keys removed from it.
/// </summary>
private DateTime lastCleaning = DateTime.UtcNow;
/// <summary>
/// Gets the key in a given bucket and handle.
/// </summary>
/// <param name="bucket">The bucket name. Case sensitive.</param>
/// <param name="handle">The key handle. Case sensitive.</param>
/// <returns>
/// The cryptographic key, or <c>null</c> if no matching key was found.
/// </returns>
public CryptoKey GetKey(string bucket, string handle)
{
lock (this.store)
{
Dictionary<string, CryptoKey> cacheBucket;
if (this.store.TryGetValue(bucket, out cacheBucket))
{
CryptoKey key;
if (cacheBucket.TryGetValue(handle, out key))
{
return key;
}
}
}
return null;
}
/// <summary>
/// Gets a sequence of existing keys within a given bucket.
/// </summary>
/// <param name="bucket">The bucket name. Case sensitive.</param>
/// <returns>
/// A sequence of handles and keys, ordered by descending <see cref="CryptoKey.ExpiresUtc"/>.
/// </returns>
public IEnumerable<KeyValuePair<string, CryptoKey>> GetKeys(string bucket)
{
lock (this.store)
{
Dictionary<string, CryptoKey> cacheBucket;
if (this.store.TryGetValue(bucket, out cacheBucket))
{
return cacheBucket.ToList();
}
else
{
return Enumerable.Empty<KeyValuePair<string, CryptoKey>>();
}
}
}
/// <summary>
/// Stores a cryptographic key.
/// </summary>
/// <param name="bucket">The name of the bucket to store the key in. Case sensitive.</param>
/// <param name="handle">The handle to the key, unique within the bucket. Case sensitive.</param>
/// <param name="key">The key to store.</param>
/// <exception cref="CryptoKeyCollisionException">Thrown in the event of a conflict with an existing key in the same bucket and with the same handle.</exception>
public void StoreKey(string bucket, string handle, CryptoKey key)
{
lock (this.store)
{
Dictionary<string, CryptoKey> cacheBucket;
if (!this.store.TryGetValue(bucket, out cacheBucket))
{
this.store[bucket] = cacheBucket = new Dictionary<string, CryptoKey>(StringComparer.Ordinal);
}
if (cacheBucket.ContainsKey(handle))
{
throw new CryptoKeyCollisionException();
}
cacheBucket[handle] = key;
this.CleanExpiredKeysFromMemoryCacheIfAppropriate();
}
}
/// <summary>
/// Removes the key.
/// </summary>
/// <param name="bucket">The bucket name. Case sensitive.</param>
/// <param name="handle">The key handle. Case sensitive.</param>
public void RemoveKey(string bucket, string handle)
{
lock (this.store)
{
Dictionary<string, CryptoKey> cacheBucket;
if (this.store.TryGetValue(bucket, out cacheBucket))
{
cacheBucket.Remove(handle);
}
}
}
/// <summary>
/// Cleans the expired keys from memory cache if the cleaning interval has passed.
/// </summary>
private void CleanExpiredKeysFromMemoryCacheIfAppropriate()
{
if (DateTime.UtcNow > this.lastCleaning + cleaningInterval)
{
lock (this.store)
{
if (DateTime.UtcNow > this.lastCleaning + cleaningInterval)
{
this.ClearExpiredKeysFromMemoryCache();
}
}
}
}
/// <summary>
/// Weeds out expired keys from the in-memory cache.
/// </summary>
private void ClearExpiredKeysFromMemoryCache()
{
lock (this.store)
{
var emptyBuckets = new List<string>();
foreach (var bucketPair in this.store)
{
var expiredKeys = new List<string>();
foreach (var handlePair in bucketPair.Value)
{
if (handlePair.Value.ExpiresUtc < DateTime.UtcNow)
{
expiredKeys.Add(handlePair.Key);
}
}
foreach (var expiredKey in expiredKeys)
{
bucketPair.Value.Remove(expiredKey);
}
if (bucketPair.Value.Count == 0)
{
emptyBuckets.Add(bucketPair.Key);
}
}
foreach (string emptyBucket in emptyBuckets)
{
this.store.Remove(emptyBucket);
}
this.lastCleaning = DateTime.UtcNow;
}
}
/// <summary>
/// How frequently we should take time to clear out old nonces.
/// </summary>
private const int AutoCleaningFrequency = 10;
/// <summary>
/// The maximum age a message can be before it is discarded.
/// </summary>
/// <remarks>
/// This is useful for knowing how long used nonces must be retained.
/// </remarks>
private readonly TimeSpan maximumMessageAge;
/// <summary>
/// A list of the consumed nonces.
/// </summary>
private readonly SortedDictionary<DateTime, List<string>> usedNonces = new SortedDictionary<DateTime, List<string>>();
/// <summary>
/// A lock object used around accesses to the <see cref="usedNonces"/> field.
/// </summary>
private object nonceLock = new object();
/// <summary>
/// Where we're currently at in our periodic nonce cleaning cycle.
/// </summary>
private int nonceClearingCounter;
#region INonceStore Members
/// <summary>
/// Stores a given nonce and timestamp.
/// </summary>
/// <param name="context">The context, or namespace, within which the <paramref name="nonce"/> must be unique.</param>
/// <param name="nonce">A series of random characters.</param>
/// <param name="timestamp">The timestamp that together with the nonce string make it unique.
/// The timestamp may also be used by the data store to clear out old nonces.</param>
/// <returns>
/// True if the nonce+timestamp (combination) was not previously in the database.
/// False if the nonce was stored previously with the same timestamp.
/// </returns>
/// <remarks>
/// The nonce must be stored for no less than the maximum time window a message may
/// be processed within before being discarded as an expired message.
/// If the binding element is applicable to your channel, this expiration window
/// is retrieved or set using the
/// <see cref="StandardExpirationBindingElement.MaximumMessageAge"/> property.
/// </remarks>
public bool StoreNonce(string context, string nonce, DateTime timestamp)
{
if (timestamp.ToUniversalTime() + this.maximumMessageAge < DateTime.UtcNow)
{
// The expiration binding element should have taken care of this, but perhaps
// it's at the boundary case. We should fail just to be safe.
return false;
}
// We just concatenate the context with the nonce to form a complete, namespace-protected nonce.
string completeNonce = context + "\0" + nonce;
lock (this.nonceLock)
{
List<string> nonces;
if (!this.usedNonces.TryGetValue(timestamp, out nonces))
{
this.usedNonces[timestamp] = nonces = new List<string>(4);
}
if (nonces.Contains(completeNonce))
{
return false;
}
nonces.Add(completeNonce);
// Clear expired nonces if it's time to take a moment to do that.
// Unchecked so that this can int overflow without an exception.
unchecked
{
this.nonceClearingCounter++;
}
if (this.nonceClearingCounter % AutoCleaningFrequency == 0)
{
this.ClearExpiredNonces();
}
return true;
}
}
#endregion
/// <summary>
/// Clears consumed nonces from the cache that are so old they would be
/// rejected if replayed because it is expired.
/// </summary>
public void ClearExpiredNonces()
{
lock (this.nonceLock)
{
var oldNonceLists = this.usedNonces.Keys.Where(time => time.ToUniversalTime() + this.maximumMessageAge < DateTime.UtcNow).ToList();
foreach (DateTime time in oldNonceLists)
{
this.usedNonces.Remove(time);
}
// Reset the auto-clean counter so that if this method was called externally
// we don't auto-clean right away.
this.nonceClearingCounter = 0;
}
}
}
}