1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-18 02:11:22 +01:00
bitwarden-server/src/Core/IdentityServer/ConfigureOpenIdConnectDistributedOptions.cs
Kyle Spearrin 6174df0874
[PM-6196] Cleanup distributed cache for identity (#3704)
* cleanup distributed cache for identity

* removed unused using

* use persistent IDistributedCache
2024-02-09 07:43:28 -05:00

44 lines
1.7 KiB
C#

using Duende.IdentityServer.Configuration;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace Bit.Core.IdentityServer;
public class ConfigureOpenIdConnectDistributedOptions : IPostConfigureOptions<CookieAuthenticationOptions>
{
private readonly IdentityServerOptions _idsrv;
private readonly IDistributedCache _distributedCache;
private readonly IDataProtectionProvider _dataProtectionProvider;
public ConfigureOpenIdConnectDistributedOptions(
[FromKeyedServices("persistent")]
IDistributedCache distributedCache,
IDataProtectionProvider dataProtectionProvider,
IdentityServerOptions idsrv)
{
_idsrv = idsrv;
_distributedCache = distributedCache;
_dataProtectionProvider = dataProtectionProvider;
}
public void PostConfigure(string name, CookieAuthenticationOptions options)
{
options.CookieManager = new DistributedCacheCookieManager();
if (name != AuthenticationSchemes.BitwardenExternalCookieAuthenticationScheme)
{
// Ignore
return;
}
options.Cookie.Name = AuthenticationSchemes.BitwardenExternalCookieAuthenticationScheme;
options.Cookie.IsEssential = true;
options.Cookie.SameSite = _idsrv.Authentication.CookieSameSiteMode;
options.TicketDataFormat = new DistributedCacheTicketDataFormatter(_distributedCache, _dataProtectionProvider, name);
options.SessionStore = new DistributedCacheTicketStore(_distributedCache);
}
}