Compare commits

...

29 Commits

Author SHA1 Message Date
khwezi 5140da2c6c Merge pull request 'Passing token hint during signout' (#92) from payments into master
Reviewed-on: #92
2026-06-07 14:09:31 +02:00
Khwezi Mngoma 02ff14ccc8 Passing token hint during signout
continuous-integration/drone/pr Build is passing
2026-06-07 14:09:02 +02:00
khwezi 0ad410c64e Merge pull request 'payments' (#91) from payments into master
Reviewed-on: #91
2026-06-07 13:57:15 +02:00
Khwezi Mngoma e193aa7c1c Adopted standard logout flow
continuous-integration/drone/pr Build is running
2026-06-07 13:56:49 +02:00
Khwezi Mngoma 840d4568e2 Refactored logout process 2026-06-07 13:55:20 +02:00
khwezi 6e580ecdf6 Merge pull request 'Added token hint to logout event' (#90) from payments into master
Reviewed-on: #90
2026-06-07 13:12:05 +02:00
Khwezi Mngoma 60095057b7 Added token hint to logout event
continuous-integration/drone/pr Build is passing
2026-06-07 13:11:33 +02:00
khwezi 4c194c1141 Merge pull request 'Added AddSecurityEndpoints login endpoint' (#89) from payments into master
Reviewed-on: #89
2026-06-07 12:00:50 +02:00
Khwezi Mngoma b41136e2c7 Added AddSecurityEndpoints login endpoint
continuous-integration/drone/pr Build is passing
2026-06-07 12:00:07 +02:00
khwezi 41eb4daeb4 Merge pull request 'Refactored AddSecurityEndpoints' (#88) from payments into master
Reviewed-on: #88
2026-06-07 11:09:02 +02:00
Khwezi Mngoma c423f04b42 Refactored AddSecurityEndpoints
continuous-integration/drone/pr Build is passing
2026-06-07 11:08:05 +02:00
khwezi 7fe5f7aef3 Merge pull request 'Refactored client auth method' (#87) from payments into master
Reviewed-on: #87
2026-06-07 10:33:33 +02:00
Khwezi Mngoma a567fc7cd7 Refactored client auth method
continuous-integration/drone/pr Build is passing
2026-06-07 10:30:56 +02:00
khwezi 31254932ae Merge pull request 'Enabled api audience verification' (#86) from payments into master
Reviewed-on: #86
2026-06-06 23:54:13 +02:00
Khwezi Mngoma c53434a578 Enabled api audience verification
continuous-integration/drone/pr Build is passing
2026-06-06 23:53:19 +02:00
khwezi 5a0aae8182 Merge pull request 'Disabled audience verification on jwt tokens' (#85) from payments into master
Reviewed-on: #85
2026-06-06 21:50:48 +02:00
Khwezi Mngoma 3f3e0dbe88 Disabled audience verification on jwt tokens
continuous-integration/drone/pr Build is passing
2026-06-06 21:49:32 +02:00
khwezi 8d8ebffabf Merge pull request 'Removed required scope policy, no longer needed, audience covers the intent' (#84) from payments into master
Reviewed-on: #84
2026-06-06 16:44:52 +02:00
Khwezi Mngoma dc4addb43a Removed required scope policy, no longer needed, audience covers the intent
continuous-integration/drone/pr Build is passing
2026-06-06 16:44:22 +02:00
khwezi e2d29261da Merge pull request 'Updated API to use LiteCharms Security' (#83) from payments into master
Reviewed-on: #83
2026-06-06 16:27:16 +02:00
Khwezi Mngoma 5d5b59d610 Updated API to use LiteCharms Security
continuous-integration/drone/pr Build is passing
2026-06-06 16:26:27 +02:00
khwezi f001b02633 Merge pull request 'Refactored to deal with cookie hell' (#82) from payments into master
Reviewed-on: #82
2026-06-05 09:20:17 +02:00
Khwezi Mngoma 90a11dc65e Refactored to deal with cookie hell
continuous-integration/drone/pr Build is passing
2026-06-05 09:19:32 +02:00
khwezi de955a96a8 Merge pull request 'Removed login proto handling' (#81) from payments into master
Reviewed-on: #81
2026-06-05 08:56:17 +02:00
Khwezi Mngoma cdf5cfb5cd Removed login proto handling
continuous-integration/drone/pr Build is passing
2026-06-05 08:55:31 +02:00
khwezi c4d3bb4cdf Merge pull request 'Simplified login process' (#80) from payments into master
Reviewed-on: #80
2026-06-05 08:18:12 +02:00
Khwezi Mngoma 65f102f18a Simplified login process
continuous-integration/drone/pr Build is passing
2026-06-05 08:17:32 +02:00
khwezi cdc80db214 Merge pull request 'Refactored logout endpoint' (#79) from payments into master
Reviewed-on: #79
2026-06-05 08:15:50 +02:00
Khwezi Mngoma 4576b5aa2b Refactored logout endpoint
continuous-integration/drone/pr Build is passing
2026-06-05 08:15:13 +02:00
9 changed files with 75 additions and 99 deletions
@@ -11,7 +11,7 @@
<!-- Quartz Scheduler--> <!-- Quartz Scheduler-->
<ItemGroup> <ItemGroup>
<PackageReference Include="Bogus" Version="35.6.5" /> <PackageReference Include="Bogus" Version="35.6.5" />
<PackageReference Include="Meziantou.Analyzer" Version="3.0.98"> <PackageReference Include="Meziantou.Analyzer" Version="3.0.101">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
@@ -116,8 +116,8 @@
<!-- Amazon S3 SDK --> <!-- Amazon S3 SDK -->
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.3" /> <PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.4" />
<PackageReference Include="AWSSDK.S3" Version="4.0.24" /> <PackageReference Include="AWSSDK.S3" Version="4.0.24.1" />
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" /> <ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
<!-- global Usings --> <!-- global Usings -->
@@ -32,7 +32,7 @@
<!-- Quartz Scheduler--> <!-- Quartz Scheduler-->
<ItemGroup> <ItemGroup>
<PackageReference Include="Humanizer" Version="3.0.10" /> <PackageReference Include="Humanizer" Version="3.0.10" />
<PackageReference Include="Meziantou.Analyzer" Version="3.0.98"> <PackageReference Include="Meziantou.Analyzer" Version="3.0.101">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
@@ -136,8 +136,8 @@
<!-- Amazon S3 SDK --> <!-- Amazon S3 SDK -->
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.3" /> <PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.4" />
<PackageReference Include="AWSSDK.S3" Version="4.0.24" /> <PackageReference Include="AWSSDK.S3" Version="4.0.24.1" />
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" /> <ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
<!-- global Usings --> <!-- global Usings -->
@@ -136,8 +136,8 @@
<!-- Amazon S3 SDK --> <!-- Amazon S3 SDK -->
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.3" /> <PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.4" />
<PackageReference Include="AWSSDK.S3" Version="4.0.24" /> <PackageReference Include="AWSSDK.S3" Version="4.0.24.1" />
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" /> <ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
<!-- global Usings --> <!-- global Usings -->
@@ -1,6 +1,6 @@
### Authentik Token Request (Service Account Explicit) ### Authentik Token Request (Service Account Explicit)
POST {{authority}} POST {{authority}}/connect/token
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}}&username={{username}}&password={{password}}&scope={{scope}} grant_type={{grantType}}&client_id={{clientId}}&client_secret={{clientSecret}}&scope={{scope}}
@@ -0,0 +1,9 @@
{
"uat": {
"authority": "https://sts.security.khongisa.co.za",
"grantType": "client_credentials",
"clientId": "midrandbooks-api-scaler-uat",
"clientSecret": "secret_0a8dc1f99061590a52b1272db3a1871d2761c79fbd058b2a968911029e4b208a",
"scope": "midrandbooks-api"
}
}
@@ -1,22 +0,0 @@
namespace LiteCharms.Features.Api.Configuration;
public sealed class AuthentikSettings
{
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? ClientSecret { get; set; }
public string? RequiredClaimName { get; set; }
public string? RequiredClaimNameValue { get; set; }
public bool RequireHttpsMetadata { get; set; }
}
@@ -0,0 +1,12 @@
namespace LiteCharms.Features.Api.Configuration;
public sealed class LiteCharmsSettings
{
public string? Authority { get; set; }
public string? ClientId { get; set; }
public string? ClientSecret { get; set; }
public string? Audience { get; set; }
}
+37 -60
View File
@@ -1,6 +1,7 @@
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;
@@ -9,14 +10,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 AddAuthentikUiSecurity(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddLiteCharmsWebSecurity(this IServiceCollection services, IConfiguration configuration)
{ {
var configSection = configuration.GetSection(nameof(AuthentikSettings)); var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
var authOptions = new AuthentikSettings(); var authOptions = new LiteCharmsSettings();
configSection.Bind(authOptions); configSection.Bind(authOptions);
services.Configure<AuthentikSettings>(configSection); services.Configure<LiteCharmsSettings>(configSection);
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {
@@ -27,13 +28,11 @@ public static class Api
.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;
options.SignedOutCallbackPath = "/signout-callback-oidc";
options.ResponseType = "code"; options.ResponseType = "code";
options.SaveTokens = true; options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true; options.GetClaimsFromUserInfoEndpoint = true;
@@ -44,17 +43,16 @@ public static class Api
options.Events = new OpenIdConnectEvents options.Events = new OpenIdConnectEvents
{ {
OnRedirectToIdentityProvider = context => OnRedirectToIdentityProviderForSignOut = context =>
{ {
if (!string.IsNullOrEmpty(context.ProtocolMessage.RedirectUri) && context.ProtocolMessage.RedirectUri.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) var idToken = context.ProtocolMessage.IdTokenHint;
{
var uriBuilder = new UriBuilder(context.ProtocolMessage.RedirectUri)
{
Scheme = "https",
Port = -1,
};
context.ProtocolMessage.RedirectUri = uriBuilder.Uri.ToString(); if (string.IsNullOrEmpty(idToken))
{
var tokens = context.Properties.GetTokens();
var idTokenItem = tokens.FirstOrDefault(t => string.Equals(t.Name, "id_token", StringComparison.Ordinal));
if (idTokenItem != null) context.ProtocolMessage.IdTokenHint = idTokenItem.Value;
} }
return Task.CompletedTask; return Task.CompletedTask;
@@ -62,40 +60,34 @@ public static class Api
}; };
}); });
services.AddCascadingAuthenticationState();
return services; return services;
} }
public static IServiceCollection AddAuthentikApiSecurity(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddLiteCharmsApiSecurity(this IServiceCollection services, IConfiguration configuration)
{ {
var configSection = configuration.GetSection(nameof(AuthentikSettings)); var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
var authOptions = new AuthentikSettings(); var authOptions = new LiteCharmsSettings();
configSection.Bind(authOptions); configSection.Bind(authOptions);
services.Configure<AuthentikSettings>(configSection); services.Configure<LiteCharmsSettings>(configSection);
services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme) services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddOAuth2Introspection(OAuth2IntrospectionDefaults.AuthenticationScheme, options => .AddJwtBearer(options =>
{ {
options.Authority = authOptions.Authority; options.Authority = authOptions.Authority;
options.IntrospectionEndpoint = authOptions.IntrospectionEndpoint; options.Audience = authOptions.Audience;
options.ClientId = authOptions.ClientId; options.TokenValidationParameters = new TokenValidationParameters
options.ClientSecret = authOptions.ClientSecret; {
ValidIssuer = authOptions.Authority,
options.NameClaimType = "sub"; ValidateAudience = true,
options.DiscoveryPolicy.RequireHttps = authOptions.RequireHttpsMetadata; ValidateIssuer = true,
options.DiscoveryPolicy.ValidateEndpoints = false; };
options.EnableCaching = false;
}); });
if (!string.IsNullOrWhiteSpace(authOptions.RequiredClaimName) && !string.IsNullOrWhiteSpace(authOptions.RequiredClaimNameValue)) services.AddAuthorization();
{
services.AddAuthorizationBuilder()
.AddPolicy("RequiredScope", policy =>
policy.RequireClaim(authOptions.RequiredClaimName, authOptions.RequiredClaimNameValue));
}
else
services.AddAuthorization();
return services; return services;
} }
@@ -106,36 +98,21 @@ public static class Api
{ {
await context.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties await context.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties
{ {
RedirectUri = redirectUri, RedirectUri = redirectUri,
}); });
}); });
app.MapGet("/logout", async (HttpContext context, IHttpClientFactory httpClientFactory, IOptions<AuthentikSettings> settings) => app.MapGet("/logout", async (HttpContext context) =>
{ {
var authOptions = settings.Value; var idToken = await context.GetTokenAsync("id_token");
var accessToken = await context.GetTokenAsync("access_token");
if (!string.IsNullOrEmpty(accessToken)) var authProperties = new AuthenticationProperties { RedirectUri = "/", };
{
try
{
var client = httpClientFactory.CreateClient();
var requestContent = new FormUrlEncodedContent(new Dictionary<string, string>(StringComparer.Ordinal) if (!string.IsNullOrEmpty(idToken))
{ authProperties.Parameters.Add("id_token_hint", idToken);
{ "token", accessToken },
{ "client_id", authOptions.ClientId! },
{ "client_secret", authOptions.ClientSecret! },
});
await client.PostAsync(authOptions.RevokationEndpoint, requestContent, context.RequestAborted);
}
catch { }
}
await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, authProperties);
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Results.Redirect($"{authOptions.Authority}end-session/");
}); });
return app; return app;
@@ -38,10 +38,10 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.Certificate" Version="10.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.Certificate" Version="10.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.8" />
<Using Include="Microsoft.AspNetCore.Authentication"/> <Using Include="Microsoft.AspNetCore.Authentication" />
<Using Include="Microsoft.AspNetCore.Authentication.OpenIdConnect"/> <Using Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
<Using Include="Microsoft.AspNetCore.Authentication.Cookies"/> <Using Include="Microsoft.AspNetCore.Authentication.Cookies" />
<Using Include="IdentityModel.AspNetCore.OAuth2Introspection"/> <Using Include="IdentityModel.AspNetCore.OAuth2Introspection" />
</ItemGroup> </ItemGroup>
<!-- API Versioning --> <!-- API Versioning -->
@@ -67,7 +67,7 @@
<!-- Quartz Scheduler--> <!-- Quartz Scheduler-->
<ItemGroup> <ItemGroup>
<PackageReference Include="Hashids.net" Version="1.7.0" /> <PackageReference Include="Hashids.net" Version="1.7.0" />
<PackageReference Include="Meziantou.Analyzer" Version="3.0.98"> <PackageReference Include="Meziantou.Analyzer" Version="3.0.101">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
@@ -171,8 +171,8 @@
<!-- Amazon S3 SDK --> <!-- Amazon S3 SDK -->
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.3" /> <PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.4" />
<PackageReference Include="AWSSDK.S3" Version="4.0.24" /> <PackageReference Include="AWSSDK.S3" Version="4.0.24.1" />
<!-- global Usings --> <!-- global Usings -->
<Using Include="Amazon.S3" /> <Using Include="Amazon.S3" />