From 097ecd64215506e22df789b26df654d998b6bbac Mon Sep 17 00:00:00 2001 From: Khwezi Mngoma Date: Thu, 4 Jun 2026 14:45:33 +0200 Subject: [PATCH 1/2] Configured security --- MidrandBookshop/MidrandBookshop.csproj | 4 ++-- MidrandBookshop/Program.cs | 1 + MidrandBookshop/appsettings.json | 5 ++++- midrandbooks-uat.yml | 13 +++++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/MidrandBookshop/MidrandBookshop.csproj b/MidrandBookshop/MidrandBookshop.csproj index c51dc1b..eed9056 100644 --- a/MidrandBookshop/MidrandBookshop.csproj +++ b/MidrandBookshop/MidrandBookshop.csproj @@ -18,13 +18,13 @@ - + - + diff --git a/MidrandBookshop/Program.cs b/MidrandBookshop/Program.cs index 2308885..a3d32c8 100644 --- a/MidrandBookshop/Program.cs +++ b/MidrandBookshop/Program.cs @@ -13,6 +13,7 @@ builder.AddMonitoring(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddMediator(); +builder.Services.AddAuthentikUiSecurity(builder.Configuration); builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(TelemetryPipelineBehavior<,>)); builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingPipelineBehavior<,>)); diff --git a/MidrandBookshop/appsettings.json b/MidrandBookshop/appsettings.json index 1b59dae..cefe3b0 100644 --- a/MidrandBookshop/appsettings.json +++ b/MidrandBookshop/appsettings.json @@ -1,5 +1,8 @@ { - "HasherSettings": { + "AuthentikSettings": { + "Authority": "https://id.khongisa.co.za/application/o/midrand-books-uat/" + }, + "HasherSettings": { "MinHashLength": 11 }, "BookshopS3Settings": { diff --git a/midrandbooks-uat.yml b/midrandbooks-uat.yml index 34bba68..11677e1 100644 --- a/midrandbooks-uat.yml +++ b/midrandbooks-uat.yml @@ -26,6 +26,7 @@ data: ValidPayfastHosts__4: "ips.payfast.co.za" ValidPayfastHosts__5: "api.payfast.co.za" ValidPayfastHosts__6: "payment.payfast.io" + AuthentikSettings__Authority: "https://id.khongisa.co.za/application/o/midrand-books-api-uat/" --- apiVersion: v1 kind: Secret @@ -41,6 +42,8 @@ data: hasher-payfastpassphrase: OUdBSVIwdFdwaFgwcU8= bookshop-s3-accesskey: R0s1MTRkMmNlOGRjNjkyMzdhMDVjMDFlZWY= bookshop-s3-secretkey: ZWFhZmVkYTFhZWQ0MDllY2ZlNjA3MTRlY2RhNTQ5YjgyYmRmNWEzZGFmOWYxOGRkNjFmNjZiNDk3M2E2NDgyZQ== + authentik-clientid: Nm9oZk1lSndQNWR0YWY1RFMzZU9MY2NNSHF6WXlma1YzRTNGeE5Tbw== + authentik-clientsecret: TXV2a0FLQklHR3BkdEsyaFlabVU1dFRaUmNuM2FhRzhoMWhlVE1nazFYOGVwczYyMzNCS0REWGdpNXo0T01RalVzMGZEUEFmakpmVVRNN1h3ZjllMU01MTQyVGlvOXRycUdmZTM1THhPaExEUnp6N2gxSm5jVkNLYXZXUllndmQ= --- apiVersion: v1 kind: PersistentVolumeClaim @@ -93,6 +96,16 @@ spec: - configMapRef: name: midrandbooks-config env: + - name: AuthentikSettings__ClientId + valueFrom: + secretKeyRef: + name: midrandbooksapi-secrets + key: authentik-clientid + - name: AuthentikSettings__ClientSecret + valueFrom: + secretKeyRef: + name: midrandbooksapi-secrets + key: authentik-clientsecret - name: BookshopS3Settings__AccessKey valueFrom: secretKeyRef: From 31a640d67245cdbe15d69fdf76955991dd0876f7 Mon Sep 17 00:00:00 2001 From: Khwezi Mngoma Date: Fri, 5 Jun 2026 05:58:05 +0200 Subject: [PATCH 2/2] Stable security --- .../Components/Layout/MainLayout.razor | 2 +- .../Components/Pages/Profile.razor | 359 +++++++++--------- .../Components/RedirectToLogin.razor | 10 + MidrandBookshop/Components/Routes.razor | 32 +- MidrandBookshop/MidrandBookshop.csproj | 4 +- MidrandBookshop/Program.cs | 13 + MidrandBookshop/appsettings.json | 4 +- midrandbooks-uat.yml | 2 + 8 files changed, 237 insertions(+), 189 deletions(-) create mode 100644 MidrandBookshop/Components/RedirectToLogin.razor diff --git a/MidrandBookshop/Components/Layout/MainLayout.razor b/MidrandBookshop/Components/Layout/MainLayout.razor index aaace25..49cd15c 100644 --- a/MidrandBookshop/Components/Layout/MainLayout.razor +++ b/MidrandBookshop/Components/Layout/MainLayout.razor @@ -167,7 +167,7 @@ - LogIn + Account -

My Account

-
-
- -
- -
-
- -
-
-
Order History
+ + +
+

My Account

+
+
+ +
-
- @if (orderHistory != null) - { - @foreach (var order in orderHistory) +
+
+ +
+
+
Order History
+
+ +
+ @if (orderHistory != null) + { + @foreach (var order in orderHistory) + { +
+
+ +
+
+
+ @order.OrderId +
+
+ @order.OrderDate.ToString("MMM dd, yyyy") +
+
+ + @order.Status + +
+
+ +
+ + @order.ProductTitle + +
+ +
+ + + + Shipped to: @order.ShippingAddressName +
+
+ +
+
+ Total Paid + R @order.Total.ToString("N2") +
+ + +
+ +
+
+ } + } + else + { +
+ Loading order history... +
+ } +
+
+ +
+
+
Saved Addresses
+ @if (!showAddForm && editingAddress == null) + { + + } +
+ + @if (showAddForm) { -
-
- - - -
-
- Total Paid - R @order.Total.ToString("N2") -
- - -
- +
+
+
New Address
+ +
+ + +
+ + +
+
+ + +
+
+
} - } - else - { -
- Loading order history... -
- } -
-
-
-
-
Saved Addresses
- @if (!showAddForm && editingAddress == null) - { - - } -
- - @if (showAddForm) - { -
-
-
New Address
- -
- - -
- - -
-
- - -
-
- -
-
- } - - @if (editingAddress != null) - { -
-
-
Edit Address
- -
- - -
- - -
-
- - -
-
- -
-
- } - - @foreach (var addr in savedAddresses) - { -
-
-
-
@addr.Name
-

@addr.Street, @addr.City, @addr.PostalCode

-
- @if (addr.IsBilling) - { - [Billing] - } - @if (addr.IsShipping) - { - [Shipping] - } + @if (editingAddress != null) + { +
+
+
Edit Address
+ +
+ + +
+ + +
+
+ + +
+
+
+ } -
- + @foreach (var addr in savedAddresses) + { +
+
+
+
@addr.Name
+

@addr.Street, @addr.City, @addr.PostalCode

+
+ @if (addr.IsBilling) + { + [Billing] + } + @if (addr.IsShipping) + { + [Shipping] + } +
+
- +
+ - + + + +
+
+ } +
+ +
+
+
Profile Settings
+
+
+

Manage your password and profile data here.

- } -
-
-
-
Profile Settings
-
-
-

Manage your password and profile data here.

-
-
-
+ + + + + + @code { private bool showAddForm = false; @@ -216,6 +227,8 @@ new AddressItem { Id = 3, Name = "Midrand Books Warehouse", Street = "Unit 8, Corporate Park North", City = "Randjespark", PostalCode = "1683", IsBilling = false, IsShipping = true, IsPrimary = false } }; + private void TriggerLogout() => Navigation.NavigateTo("/logout", forceLoad: true); + private void DownloadInvoice(string orderId) { // Handle invoice downloading logic here diff --git a/MidrandBookshop/Components/RedirectToLogin.razor b/MidrandBookshop/Components/RedirectToLogin.razor new file mode 100644 index 0000000..68ff98c --- /dev/null +++ b/MidrandBookshop/Components/RedirectToLogin.razor @@ -0,0 +1,10 @@ +@inject NavigationManager Navigation + +@code { + protected override void OnInitialized() + { + var returnUrl = Navigation.ToBaseRelativePath(Navigation.Uri); + + Navigation.NavigateTo($"/login?redirectUri={Uri.EscapeDataString(returnUrl)}", forceLoad: true); + } +} \ No newline at end of file diff --git a/MidrandBookshop/Components/Routes.razor b/MidrandBookshop/Components/Routes.razor index 71c38b1..6a23f48 100644 --- a/MidrandBookshop/Components/Routes.razor +++ b/MidrandBookshop/Components/Routes.razor @@ -1,12 +1,20 @@ -@using MidrandBookshop.Components.Pages - - - - - - - - - - - \ No newline at end of file +@using Microsoft.AspNetCore.Components.Authorization +@using MidrandBookshop.Components.Pages + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MidrandBookshop/MidrandBookshop.csproj b/MidrandBookshop/MidrandBookshop.csproj index eed9056..fd401c4 100644 --- a/MidrandBookshop/MidrandBookshop.csproj +++ b/MidrandBookshop/MidrandBookshop.csproj @@ -18,13 +18,13 @@ - + - + diff --git a/MidrandBookshop/Program.cs b/MidrandBookshop/Program.cs index a3d32c8..b2257d3 100644 --- a/MidrandBookshop/Program.cs +++ b/MidrandBookshop/Program.cs @@ -1,9 +1,12 @@ using LiteCharms.Features.Extensions; using LiteCharms.Features.Mediator; using LiteCharms.Features.MidrandBooks.Extensions; +using Microsoft.AspNetCore.HttpOverrides; using MidrandBookshop.Components; using static LiteCharms.Features.Extensions.Quartz; +AppContext.SetSwitch("Microsoft.IdentityModel.DisableTelemetry", true); + var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorComponents() @@ -31,9 +34,19 @@ builder.Services.AddMidrandShopDatabase(builder.Configuration); builder.Services.AddMidrandShopPostgresHealthCheck(); builder.Services.AddMidrandShopQuartzHealthCheck(); builder.Services.AddHealthChecksSupport(builder.Configuration); +builder.Services.AddCascadingAuthenticationState(); + +builder.Services.Configure(options => +{ + options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; + options.KnownProxies.Clear(); +}); var app = builder.Build(); +app.UseForwardedHeaders(); +app.AddSecurityEndpoints(); + var schedulerFactory = app.Services.GetRequiredService(); var scheduler = await schedulerFactory.GetScheduler(MidrandShopSchedulerName); diff --git a/MidrandBookshop/appsettings.json b/MidrandBookshop/appsettings.json index cefe3b0..fb852fd 100644 --- a/MidrandBookshop/appsettings.json +++ b/MidrandBookshop/appsettings.json @@ -1,6 +1,8 @@ { "AuthentikSettings": { - "Authority": "https://id.khongisa.co.za/application/o/midrand-books-uat/" + "Authority": "https://id.khongisa.co.za/application/o/midrand-books-uat/", + "MetadataEndpoint": "https://id.khongisa.co.za/application/o/midrand-books-uat/.well-known/openid-configuration", + "RevokationEndpoint": "https://id.khongisa.co.za/application/o/revoke/" }, "HasherSettings": { "MinHashLength": 11 diff --git a/midrandbooks-uat.yml b/midrandbooks-uat.yml index 11677e1..58211af 100644 --- a/midrandbooks-uat.yml +++ b/midrandbooks-uat.yml @@ -27,6 +27,8 @@ data: ValidPayfastHosts__5: "api.payfast.co.za" ValidPayfastHosts__6: "payment.payfast.io" AuthentikSettings__Authority: "https://id.khongisa.co.za/application/o/midrand-books-api-uat/" + AuthentikSettings__MetadataEndpoint: "https://id.khongisa.co.za/application/o/midrand-books-uat/.well-known/openid-configuration" + AuthentikSettings__RevokationEndpoint: "https://id.khongisa.co.za/application/o/revoke/" --- apiVersion: v1 kind: Secret