-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathWebAuthnChallengeCacheProvider.cs
More file actions
33 lines (27 loc) · 1.13 KB
/
WebAuthnChallengeCacheProvider.cs
File metadata and controls
33 lines (27 loc) · 1.13 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
using Bit.Core.Utilities;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Core.Auth.UserFeatures.WebAuthnLogin.Implementations;
internal class WebAuthnChallengeCacheProvider(
[FromKeyedServices("persistent")] IDistributedCache distributedCache) : IWebAuthnChallengeCacheProvider
{
private const string _cacheKeyPrefix = "WebAuthnLoginAssertion_";
private static readonly DistributedCacheEntryOptions _cacheOptions = new()
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(17)
};
private readonly IDistributedCache _distributedCache = distributedCache;
public async Task<bool> TryMarkChallengeAsUsedAsync(byte[] challenge)
{
var cacheKey = BuildCacheKey(challenge);
var cached = await _distributedCache.GetAsync(cacheKey);
if (cached != null)
{
return false;
}
await _distributedCache.SetAsync(cacheKey, [1], _cacheOptions);
return true;
}
private static string BuildCacheKey(byte[] challenge)
=> $"{_cacheKeyPrefix}{CoreHelpers.Base64UrlEncode(challenge)}";
}