Compare commits

..

1 Commits

Author SHA1 Message Date
khwezi 95ae2c74cf Merge commit '4576b5aa2b6c6034247d3cd136fe15c82d27a6f4' 2026-06-05 06:15:50 +00:00
4 changed files with 49 additions and 40 deletions
@@ -1,6 +1,6 @@
### Authentik Token Request (Service Account Explicit) ### Authentik Token Request (Service Account Explicit)
POST {{authority}}/connect/token POST {{authority}}
Content-Type: application/x-www-form-urlencoded Content-Type: application/x-www-form-urlencoded
Accept-Encoding: identity Accept-Encoding: identity
grant_type={{grantType}}&client_id={{clientId}}&client_secret={{clientSecret}}&scope={{scope}} grant_type={{grantType}}&client_id={{clientId}}&username={{username}}&password={{password}}&scope={{scope}}
@@ -1,9 +0,0 @@
{
"uat": {
"authority": "https://sts.security.khongisa.co.za",
"grantType": "client_credentials",
"clientId": "midrandbooks-api-uat",
"clientSecret": "secret_5a36d0024980544c875447a4b052938becc3fbbb10b8b2c097310c1a53ba3c0a",
"scope": "midrandbooks-api"
}
}
@@ -1,16 +1,22 @@
namespace LiteCharms.Features.Api.Configuration; namespace LiteCharms.Features.Api.Configuration;
public sealed class LiteCharmsSettings public sealed class AuthentikSettings
{ {
public string? Authority { get; set; } public string? Authority { get; set; }
public string? IntrospectionEndpoint { get; set; }
public string? MetadataEndpoint { get; set; }
public string? RevokationEndpoint { get; set; }
public string? ClientId { get; set; } public string? ClientId { get; set; }
public string? ClientSecret { get; set; } public string? ClientSecret { get; set; }
public string? Audience { get; set; }
public string? RequiredClaimName { get; set; } public string? RequiredClaimName { get; set; }
public string? RequiredClaimNameValue { get; set; } public string? RequiredClaimNameValue { get; set; }
public bool RequireHttpsMetadata { get; set; }
} }
+38 -26
View File
@@ -1,7 +1,6 @@
using LiteCharms.Features.Abstractions; using LiteCharms.Features.Abstractions;
using LiteCharms.Features.Api; using LiteCharms.Features.Api;
using LiteCharms.Features.Api.Configuration; using LiteCharms.Features.Api.Configuration;
using Microsoft.AspNetCore.Authentication.JwtBearer;
namespace LiteCharms.Features.Extensions; namespace LiteCharms.Features.Extensions;
@@ -10,14 +9,14 @@ public static class Api
public const string Books = nameof(Books); public const string Books = nameof(Books);
public const string Payments = nameof(Payments); public const string Payments = nameof(Payments);
public static IServiceCollection AddLiteCharmsUiSecurity(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddAuthentikUiSecurity(this IServiceCollection services, IConfiguration configuration)
{ {
var configSection = configuration.GetSection(nameof(LiteCharmsSettings)); var configSection = configuration.GetSection(nameof(AuthentikSettings));
var authOptions = new LiteCharmsSettings(); var authOptions = new AuthentikSettings();
configSection.Bind(authOptions); configSection.Bind(authOptions);
services.Configure<LiteCharmsSettings>(configSection); services.Configure<AuthentikSettings>(configSection);
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {
@@ -27,7 +26,8 @@ public static class Api
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options => .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{ {
options.Authority = authOptions.Authority; options.Authority = authOptions.Authority;
options.MetadataAddress = authOptions.MetadataEndpoint;
options.ClientId = authOptions.ClientId; options.ClientId = authOptions.ClientId;
options.ClientSecret = authOptions.ClientSecret; options.ClientSecret = authOptions.ClientSecret;
@@ -42,38 +42,50 @@ public static class Api
options.Scope.Add("profile"); options.Scope.Add("profile");
options.Scope.Add("email"); options.Scope.Add("email");
options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always; options.Events = new OpenIdConnectEvents
options.CorrelationCookie.SameSite = SameSiteMode.None; {
options.CorrelationCookie.HttpOnly = true; OnRedirectToIdentityProvider = context =>
{
if (!string.IsNullOrEmpty(context.ProtocolMessage.RedirectUri) && context.ProtocolMessage.RedirectUri.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
var uriBuilder = new UriBuilder(context.ProtocolMessage.RedirectUri)
{
Scheme = "https",
Port = -1,
};
options.NonceCookie.SecurePolicy = CookieSecurePolicy.Always; context.ProtocolMessage.RedirectUri = uriBuilder.Uri.ToString();
options.NonceCookie.SameSite = SameSiteMode.None; }
options.NonceCookie.HttpOnly = true;
return Task.CompletedTask;
},
};
}); });
return services; return services;
} }
public static IServiceCollection AddLiteCharmsApiSecurity(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddAuthentikApiSecurity(this IServiceCollection services, IConfiguration configuration)
{ {
var configSection = configuration.GetSection(nameof(LiteCharmsSettings)); var configSection = configuration.GetSection(nameof(AuthentikSettings));
var authOptions = new LiteCharmsSettings(); var authOptions = new AuthentikSettings();
configSection.Bind(authOptions); configSection.Bind(authOptions);
services.Configure<LiteCharmsSettings>(configSection); services.Configure<AuthentikSettings>(configSection);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
.AddJwtBearer(options => .AddOAuth2Introspection(OAuth2IntrospectionDefaults.AuthenticationScheme, options =>
{ {
options.Authority = authOptions.Authority; options.Authority = authOptions.Authority;
options.Audience = authOptions.Audience; options.IntrospectionEndpoint = authOptions.IntrospectionEndpoint;
options.TokenValidationParameters = new TokenValidationParameters options.ClientId = authOptions.ClientId;
{ options.ClientSecret = authOptions.ClientSecret;
ValidIssuer = authOptions.Authority,
ValidateAudience = true, options.NameClaimType = "sub";
ValidateIssuer = true, options.DiscoveryPolicy.RequireHttps = authOptions.RequireHttpsMetadata;
}; options.DiscoveryPolicy.ValidateEndpoints = false;
options.EnableCaching = false;
}); });
if (!string.IsNullOrWhiteSpace(authOptions.RequiredClaimName) && !string.IsNullOrWhiteSpace(authOptions.RequiredClaimNameValue)) if (!string.IsNullOrWhiteSpace(authOptions.RequiredClaimName) && !string.IsNullOrWhiteSpace(authOptions.RequiredClaimNameValue))
@@ -98,7 +110,7 @@ public static class Api
}); });
}); });
app.MapGet("/logout", async (HttpContext context, IHttpClientFactory httpClientFactory, IOptions<LiteCharmsSettings> settings) => app.MapGet("/logout", async (HttpContext context, IHttpClientFactory httpClientFactory, IOptions<AuthentikSettings> settings) =>
{ {
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);