2022-06-29 19:46:41 -04:00
|
|
|
|
using Bit.Core.Settings;
|
2023-11-20 16:32:23 -05:00
|
|
|
|
using Duende.IdentityServer.Configuration;
|
2021-01-11 11:03:46 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2022-07-19 11:58:32 -07:00
|
|
|
|
using Microsoft.Extensions.Caching.StackExchangeRedis;
|
2021-01-11 11:03:46 -05:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.IdentityServer;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2021-01-11 11:03:46 -05:00
|
|
|
|
public class ConfigureOpenIdConnectDistributedOptions : IPostConfigureOptions<CookieAuthenticationOptions>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IdentityServerOptions _idsrv;
|
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2021-01-11 11:03:46 -05:00
|
|
|
|
public ConfigureOpenIdConnectDistributedOptions(IHttpContextAccessor httpContextAccessor, GlobalSettings globalSettings,
|
|
|
|
|
|
IdentityServerOptions idsrv)
|
|
|
|
|
|
{
|
|
|
|
|
|
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
|
|
|
|
|
|
_globalSettings = globalSettings;
|
|
|
|
|
|
_idsrv = idsrv;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2021-01-11 11:03:46 -05:00
|
|
|
|
|
|
|
|
|
|
public void PostConfigure(string name, CookieAuthenticationOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
options.CookieManager = new DistributedCacheCookieManager();
|
|
|
|
|
|
|
|
|
|
|
|
if (name != AuthenticationSchemes.BitwardenExternalCookieAuthenticationScheme)
|
|
|
|
|
|
{
|
2022-08-29 15:53:48 -04:00
|
|
|
|
// Ignore
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-11 11:03:46 -05:00
|
|
|
|
options.Cookie.Name = AuthenticationSchemes.BitwardenExternalCookieAuthenticationScheme;
|
|
|
|
|
|
options.Cookie.IsEssential = true;
|
|
|
|
|
|
options.Cookie.SameSite = _idsrv.Authentication.CookieSameSiteMode;
|
|
|
|
|
|
options.TicketDataFormat = new DistributedCacheTicketDataFormatter(_httpContextAccessor, name);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
|
2021-01-11 11:03:46 -05:00
|
|
|
|
if (string.IsNullOrWhiteSpace(_globalSettings.IdentityServer?.RedisConnectionString))
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2021-01-11 11:03:46 -05:00
|
|
|
|
options.SessionStore = new MemoryCacheTicketStore();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-01-11 11:03:46 -05:00
|
|
|
|
var redisOptions = new RedisCacheOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
Configuration = _globalSettings.IdentityServer.RedisConnectionString,
|
|
|
|
|
|
};
|
|
|
|
|
|
options.SessionStore = new RedisCacheTicketStore(redisOptions);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|