|
|
@@ -3,9 +3,6 @@ using LiteCharms.Features.Api;
|
|
|
|
using LiteCharms.Features.Api.Configuration;
|
|
|
|
using LiteCharms.Features.Api.Configuration;
|
|
|
|
using LiteCharms.Features.Api.Sdk;
|
|
|
|
using LiteCharms.Features.Api.Sdk;
|
|
|
|
using LiteCharms.Features.Postgres;
|
|
|
|
using LiteCharms.Features.Postgres;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace LiteCharms.Features.Extensions;
|
|
|
|
namespace LiteCharms.Features.Extensions;
|
|
|
|
|
|
|
|
|
|
|
@@ -63,6 +60,8 @@ public static class Api
|
|
|
|
.ProtectKeysWithCertificate(certificate)
|
|
|
|
.ProtectKeysWithCertificate(certificate)
|
|
|
|
.SetApplicationName("LiteCharmsApp");
|
|
|
|
.SetApplicationName("LiteCharmsApp");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
services.ConfigureCookieOidcSameSiteSupport();
|
|
|
|
|
|
|
|
|
|
|
|
var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
|
|
|
|
var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
|
|
|
|
|
|
|
|
|
|
|
|
var authOptions = new LiteCharmsSettings();
|
|
|
|
var authOptions = new LiteCharmsSettings();
|
|
|
@@ -75,17 +74,26 @@ public static class Api
|
|
|
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
|
|
|
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
|
|
|
|
|
|
options.Cookie.SameSite = SameSiteMode.Lax;
|
|
|
|
|
|
|
|
options.Cookie.Name = "LiteCharmsApp.Session";
|
|
|
|
|
|
|
|
})
|
|
|
|
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
|
|
|
|
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
options.Authority = authOptions.Authority;
|
|
|
|
options.Authority = authOptions.Authority;
|
|
|
|
|
|
|
|
|
|
|
|
options.ClientId = authOptions.ClientId;
|
|
|
|
options.ClientId = authOptions.ClientId;
|
|
|
|
options.ClientSecret = authOptions.ClientSecret;
|
|
|
|
options.ClientSecret = authOptions.ClientSecret;
|
|
|
|
options.ResponseType = "code";
|
|
|
|
options.ResponseType = "code";
|
|
|
|
|
|
|
|
|
|
|
|
options.SaveTokens = true;
|
|
|
|
options.SaveTokens = true;
|
|
|
|
options.GetClaimsFromUserInfoEndpoint = true;
|
|
|
|
options.GetClaimsFromUserInfoEndpoint = true;
|
|
|
|
|
|
|
|
options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
|
|
|
|
|
|
options.CorrelationCookie.SameSite = SameSiteMode.None;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
options.NonceCookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
|
|
|
|
|
|
options.NonceCookie.SameSite = SameSiteMode.None;
|
|
|
|
|
|
|
|
|
|
|
|
options.ForwardSignOut = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
options.ForwardSignOut = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
|
|
|
|
|
|
|
@@ -118,6 +126,21 @@ public static class Api
|
|
|
|
return services;
|
|
|
|
return services;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void ConfigureCookieOidcSameSiteSupport(this IServiceCollection services) =>
|
|
|
|
|
|
|
|
services.Configure<CookiePolicyOptions>(options =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
|
|
|
|
|
|
|
|
options.OnAppendCookie = cookieContext => CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
|
|
|
|
|
|
|
|
options.OnDeleteCookie = cookieContext => CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void CheckSameSite(HttpContext httpContext, CookieOptions options)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (options.SameSite == SameSiteMode.None)
|
|
|
|
|
|
|
|
if (!httpContext.Request.IsHttps && httpContext.Request.Headers["X-Forwarded-Proto"] != "https")
|
|
|
|
|
|
|
|
options.SameSite = SameSiteMode.Unspecified;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddLiteCharmsApiSecurity(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
public static IServiceCollection AddLiteCharmsApiSecurity(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
|
|
|
|
var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
|
|
|
|