diff --git a/Authentication/Core/MemoryCacheTicketStore.cs b/Authentication/Core/MemoryCacheTicketStore.cs new file mode 100644 index 0000000..5944eb9 --- /dev/null +++ b/Authentication/Core/MemoryCacheTicketStore.cs @@ -0,0 +1,43 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.Extensions.Caching.Memory; + +namespace maintenance.Authentication.Core; + +public sealed class MemoryCacheTicketStore : ITicketStore +{ + private const string KeyPrefix = "auth-ticket-"; + private readonly IMemoryCache _cache; + private readonly TimeSpan _fallbackLifetime = TimeSpan.FromHours(8); + + public MemoryCacheTicketStore(IMemoryCache cache) + { + _cache = cache; + } + + public async Task StoreAsync(AuthenticationTicket ticket) + { + var key = $"{KeyPrefix}{Guid.NewGuid():N}"; + await RenewAsync(key, ticket); + return key; + } + + public Task RenewAsync(string key, AuthenticationTicket ticket) + { + var expiration = ticket.Properties.ExpiresUtc?.UtcDateTime ?? DateTime.UtcNow.Add(_fallbackLifetime); + _cache.Set(key, ticket, expiration); + return Task.CompletedTask; + } + + public Task RetrieveAsync(string key) + { + _cache.TryGetValue(key, out AuthenticationTicket? ticket); + return Task.FromResult(ticket); + } + + public Task RemoveAsync(string key) + { + _cache.Remove(key); + return Task.CompletedTask; + } +} diff --git a/Authentication/Providers/Oidc/OidcAuthProvider.cs b/Authentication/Providers/Oidc/OidcAuthProvider.cs index a3c0586..cc3e8e6 100644 --- a/Authentication/Providers/Oidc/OidcAuthProvider.cs +++ b/Authentication/Providers/Oidc/OidcAuthProvider.cs @@ -49,7 +49,7 @@ public sealed class OidcAuthProvider : IAuthProvider options.CallbackPath = callbackPath; options.SignedOutCallbackPath = signedOutCallbackPath; options.ResponseType = "code"; - options.SaveTokens = true; + options.SaveTokens = false; options.GetClaimsFromUserInfoEndpoint = true; options.Scope.Clear(); diff --git a/Program.cs b/Program.cs index 1575507..84333c5 100644 --- a/Program.cs +++ b/Program.cs @@ -41,6 +41,8 @@ builder.Services.AddDbContext(options => { options.UseSqlServer(primaryDbConnection); }); +builder.Services.AddMemoryCache(); +builder.Services.AddSingleton(); if (adminGroupSource.Equals(AdminGroupSources.Ad, StringComparison.OrdinalIgnoreCase)) { @@ -86,6 +88,12 @@ authBuilder.AddCookie(options => options.Cookie.Name = "errorpages.auth"; options.AccessDeniedPath = "/AccessDenied"; }); +builder.Services + .AddOptions(CookieAuthenticationDefaults.AuthenticationScheme) + .Configure((options, ticketStore) => + { + options.SessionStore = ticketStore; + }); var providers = new IAuthProvider[] {