mirror of
https://github.com/bitwarden/server
synced 2025-12-18 17:23:28 +00:00
[PM-6196] Cleanup distributed cache for identity (#3704)
* cleanup distributed cache for identity * removed unused using * use persistent IDistributedCache
This commit is contained in:
63
src/Core/IdentityServer/DistributedCacheTicketStore.cs
Normal file
63
src/Core/IdentityServer/DistributedCacheTicketStore.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
|
||||
namespace Bit.Core.IdentityServer;
|
||||
|
||||
public class DistributedCacheTicketStore : ITicketStore
|
||||
{
|
||||
private const string KeyPrefix = "auth-";
|
||||
private readonly IDistributedCache _cache;
|
||||
|
||||
public DistributedCacheTicketStore(IDistributedCache distributedCache)
|
||||
{
|
||||
_cache = distributedCache;
|
||||
}
|
||||
|
||||
public async Task<string> StoreAsync(AuthenticationTicket ticket)
|
||||
{
|
||||
var key = $"{KeyPrefix}{Guid.NewGuid()}";
|
||||
await RenewAsync(key, ticket);
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
public Task RenewAsync(string key, AuthenticationTicket ticket)
|
||||
{
|
||||
var options = new DistributedCacheEntryOptions();
|
||||
var expiresUtc = ticket.Properties.ExpiresUtc ??
|
||||
DateTimeOffset.UtcNow.AddMinutes(15);
|
||||
options.SetAbsoluteExpiration(expiresUtc);
|
||||
|
||||
var val = SerializeToBytes(ticket);
|
||||
_cache.Set(key, val, options);
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task<AuthenticationTicket> RetrieveAsync(string key)
|
||||
{
|
||||
AuthenticationTicket ticket;
|
||||
var bytes = _cache.Get(key);
|
||||
ticket = DeserializeFromBytes(bytes);
|
||||
|
||||
return Task.FromResult(ticket);
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
_cache.Remove(key);
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
private static byte[] SerializeToBytes(AuthenticationTicket source)
|
||||
{
|
||||
return TicketSerializer.Default.Serialize(source);
|
||||
}
|
||||
|
||||
private static AuthenticationTicket DeserializeFromBytes(byte[] source)
|
||||
{
|
||||
return source == null ? null : TicketSerializer.Default.Deserialize(source);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user