mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-05-30 19:35:21 -04:00
OpenID Connect support (#3975)
Co-authored-by: DieselTech <30128380+DieselTech@users.noreply.github.com> Co-authored-by: majora2007 <josephmajora@gmail.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace API.Services.Store;
|
||||
|
||||
public class CustomTicketStore(IMemoryCache cache): ITicketStore
|
||||
{
|
||||
|
||||
public async Task<string> StoreAsync(AuthenticationTicket ticket)
|
||||
{
|
||||
// Note: It might not be needed to make this cryptographic random, but better safe than sorry
|
||||
var bytes = new byte[32];
|
||||
RandomNumberGenerator.Fill(bytes);
|
||||
var key = Convert.ToBase64String(bytes);
|
||||
|
||||
await RenewAsync(key, ticket);
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
public Task RenewAsync(string key, AuthenticationTicket ticket)
|
||||
{
|
||||
var options = new MemoryCacheEntryOptions
|
||||
{
|
||||
Priority = CacheItemPriority.NeverRemove,
|
||||
Size = 1,
|
||||
};
|
||||
|
||||
var expiresUtc = ticket.Properties.ExpiresUtc;
|
||||
if (expiresUtc.HasValue)
|
||||
{
|
||||
options.AbsoluteExpiration = expiresUtc.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
options.SlidingExpiration = TimeSpan.FromDays(7);
|
||||
}
|
||||
|
||||
cache.Set(key, ticket, options);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<AuthenticationTicket> RetrieveAsync(string key)
|
||||
{
|
||||
return Task.FromResult(cache.Get<AuthenticationTicket>(key));
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
cache.Remove(key);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user