-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathInMemoryApiUserStore.cs
43 lines (35 loc) · 1.22 KB
/
InMemoryApiUserStore.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
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Bet.AspNetCore.ApiKeyAuthentication.Options;
using Microsoft.Extensions.Options;
namespace Bet.AspNetCore.ApiKeyAuthentication
{
public class InMemoryApiUserStore : IApiUserStore
{
private readonly ConcurrentDictionary<string, ApiKeyUser> _store = new ConcurrentDictionary<string, ApiKeyUser>();
private ApiUserStoreOptions _options;
public InMemoryApiUserStore(IOptionsMonitor<ApiUserStoreOptions> optionsMonitor)
{
_options = optionsMonitor.Get(nameof(InMemoryApiUserStore));
optionsMonitor.OnChange((o, n) =>
{
if (n == nameof(InMemoryApiUserStore))
{
_options = o;
}
});
foreach (var user in _options.ApiKeyUsers)
{
_store.AddOrUpdate(user.Key, i => user, (i, u) => u);
}
}
public Task<ApiKeyUser> GetAsync(string apiKey)
{
if (_store.TryGetValue(apiKey, out var user))
{
return Task.FromResult(user);
}
return Task.FromResult(default(ApiKeyUser) !);
}
}
}