From 9665ca6168336d0ed68198a6d8778b58880682df Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 19 Mar 2026 11:09:55 -0400 Subject: [PATCH] Cleanup and bugfix --- .../Core/AdminCookieValidationEvents.cs | 70 ------------------- MaxInventory.slnx | 2 +- .../Abstractions/IAdminGroupSource.cs | 0 .../Abstractions/IAuthProvider.cs | 0 .../Abstractions/IIdentityContextFactory.cs | 0 .../Abstractions/IPrincipalAccessEvaluator.cs | 10 +++ .../Configuration/AdminGroupSources.cs | 0 .../Configuration/AuthEnvironment.cs | 0 .../Configuration/AuthModes.cs | 0 .../Core/AdminCookieValidationEvents.cs | 40 +++++++++++ .../Core/AdminRoleClaimsTransformation.cs | 0 .../Core/AuthRoles.cs | 0 .../Core/ConfiguredAdminGroupSource.cs | 0 .../Core/IdentityContext.cs | 0 .../Core/IdentityContextFactory.cs | 0 .../Core/MemoryCacheTicketStore.cs | 0 .../Core/PrincipalAccess.cs | 4 ++ .../Core/PrincipalAccessEvaluator.cs | 62 ++++++++++++++++ ...thenticationServiceCollectionExtensions.cs | 4 +- .../MaxInventory.Authentication.csproj | 0 .../AdfsWsFed/AdfsWsFedAuthProvider.cs | 0 .../Providers/Oidc/OidcAuthProvider.cs | 0 src/MaxInventory.Core/Settings/ApiSettings.cs | 2 +- .../Settings/CertificateSettings.cs | 26 ++----- .../Certificates/FileCertificateProvider.cs | 20 +++--- .../Extensions/ServiceCollectionExtensions.cs | 38 ++++------ .../Http/InventoryHttpClientFactory.cs | 2 +- .../Components/Pages/ContentTypePage.razor.cs | 32 ++++----- src/MaxInventory.Web/MaxInventory.Web.csproj | 2 +- src/MaxInventory.Web/appsettings.json | 6 +- 30 files changed, 168 insertions(+), 152 deletions(-) delete mode 100644 MaxInventory.Authentication/Core/AdminCookieValidationEvents.cs rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Abstractions/IAdminGroupSource.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Abstractions/IAuthProvider.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Abstractions/IIdentityContextFactory.cs (100%) create mode 100644 src/MaxInventory.Authentication/Abstractions/IPrincipalAccessEvaluator.cs rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Configuration/AdminGroupSources.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Configuration/AuthEnvironment.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Configuration/AuthModes.cs (100%) create mode 100644 src/MaxInventory.Authentication/Core/AdminCookieValidationEvents.cs rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Core/AdminRoleClaimsTransformation.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Core/AuthRoles.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Core/ConfiguredAdminGroupSource.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Core/IdentityContext.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Core/IdentityContextFactory.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Core/MemoryCacheTicketStore.cs (100%) create mode 100644 src/MaxInventory.Authentication/Core/PrincipalAccess.cs create mode 100644 src/MaxInventory.Authentication/Core/PrincipalAccessEvaluator.cs rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Extensions/AuthenticationServiceCollectionExtensions.cs (96%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/MaxInventory.Authentication.csproj (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Providers/AdfsWsFed/AdfsWsFedAuthProvider.cs (100%) rename {MaxInventory.Authentication => src/MaxInventory.Authentication}/Providers/Oidc/OidcAuthProvider.cs (100%) diff --git a/MaxInventory.Authentication/Core/AdminCookieValidationEvents.cs b/MaxInventory.Authentication/Core/AdminCookieValidationEvents.cs deleted file mode 100644 index 680b944..0000000 --- a/MaxInventory.Authentication/Core/AdminCookieValidationEvents.cs +++ /dev/null @@ -1,70 +0,0 @@ -using MaxInventory.Authentication.Configuration; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Claims; -using System.Text; -using System.Threading.Tasks; - -namespace MaxInventory.Authentication.Core -{ - public sealed class AdminCookieValidationEvents : CookieAuthenticationEvents - { - private static readonly string[] GroupClaimTypes = - { - "groups", - "group", - "role", - "roles", - ClaimTypes.GroupSid, - ClaimTypes.Role, - "http://schemas.microsoft.com/ws/2008/06/identity/claims/role", - "http://schemas.xmlsoap.org/claims/Group" - }; - - private readonly IConfiguration _configuration; - - public AdminCookieValidationEvents(IConfiguration configuration) - { - _configuration = configuration; - } - - public override async Task ValidatePrincipal(CookieValidatePrincipalContext context) - { - if (context.Principal?.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated) - { - context.RejectPrincipal(); - await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - return; - } - - var path = context.HttpContext.Request.Path; - if (!path.StartsWithSegments("/Admin", StringComparison.OrdinalIgnoreCase)) - { - return; - } - - var configuredGroups = (_configuration[AuthEnvironment.AdminGroups] ?? string.Empty) - .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - - - - var principalGroups = identity.Claims - .Where(c => GroupClaimTypes.Contains(c.Type, StringComparer.OrdinalIgnoreCase)) - .Select(c => c.Value); - - var isAdmin = configuredGroups.Length > 0 && - principalGroups.Any(group => - configuredGroups.Contains(group, StringComparer.OrdinalIgnoreCase)); - - if (!isAdmin) - { - context.RejectPrincipal(); - await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - } - } - } -} diff --git a/MaxInventory.slnx b/MaxInventory.slnx index 3e2490d..ca999c9 100644 --- a/MaxInventory.slnx +++ b/MaxInventory.slnx @@ -1,5 +1,5 @@ - + diff --git a/MaxInventory.Authentication/Abstractions/IAdminGroupSource.cs b/src/MaxInventory.Authentication/Abstractions/IAdminGroupSource.cs similarity index 100% rename from MaxInventory.Authentication/Abstractions/IAdminGroupSource.cs rename to src/MaxInventory.Authentication/Abstractions/IAdminGroupSource.cs diff --git a/MaxInventory.Authentication/Abstractions/IAuthProvider.cs b/src/MaxInventory.Authentication/Abstractions/IAuthProvider.cs similarity index 100% rename from MaxInventory.Authentication/Abstractions/IAuthProvider.cs rename to src/MaxInventory.Authentication/Abstractions/IAuthProvider.cs diff --git a/MaxInventory.Authentication/Abstractions/IIdentityContextFactory.cs b/src/MaxInventory.Authentication/Abstractions/IIdentityContextFactory.cs similarity index 100% rename from MaxInventory.Authentication/Abstractions/IIdentityContextFactory.cs rename to src/MaxInventory.Authentication/Abstractions/IIdentityContextFactory.cs diff --git a/src/MaxInventory.Authentication/Abstractions/IPrincipalAccessEvaluator.cs b/src/MaxInventory.Authentication/Abstractions/IPrincipalAccessEvaluator.cs new file mode 100644 index 0000000..bb2caf9 --- /dev/null +++ b/src/MaxInventory.Authentication/Abstractions/IPrincipalAccessEvaluator.cs @@ -0,0 +1,10 @@ +using MaxInventory.Authentication.Core; +using System.Security.Claims; + +namespace MaxInventory.Authentication.Abstractions +{ + public interface IPrincipalAccessEvaluator + { + Task EvaluateAsync(ClaimsPrincipal principal, CancellationToken cancellationToken = default); + } +} diff --git a/MaxInventory.Authentication/Configuration/AdminGroupSources.cs b/src/MaxInventory.Authentication/Configuration/AdminGroupSources.cs similarity index 100% rename from MaxInventory.Authentication/Configuration/AdminGroupSources.cs rename to src/MaxInventory.Authentication/Configuration/AdminGroupSources.cs diff --git a/MaxInventory.Authentication/Configuration/AuthEnvironment.cs b/src/MaxInventory.Authentication/Configuration/AuthEnvironment.cs similarity index 100% rename from MaxInventory.Authentication/Configuration/AuthEnvironment.cs rename to src/MaxInventory.Authentication/Configuration/AuthEnvironment.cs diff --git a/MaxInventory.Authentication/Configuration/AuthModes.cs b/src/MaxInventory.Authentication/Configuration/AuthModes.cs similarity index 100% rename from MaxInventory.Authentication/Configuration/AuthModes.cs rename to src/MaxInventory.Authentication/Configuration/AuthModes.cs diff --git a/src/MaxInventory.Authentication/Core/AdminCookieValidationEvents.cs b/src/MaxInventory.Authentication/Core/AdminCookieValidationEvents.cs new file mode 100644 index 0000000..4e21d9c --- /dev/null +++ b/src/MaxInventory.Authentication/Core/AdminCookieValidationEvents.cs @@ -0,0 +1,40 @@ +using MaxInventory.Authentication.Abstractions; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using System.Security.Claims; + +namespace MaxInventory.Authentication.Core +{ + public sealed class AdminCookieValidationEvents : CookieAuthenticationEvents + { + private readonly IPrincipalAccessEvaluator _principalAccessEvaluator; + + public AdminCookieValidationEvents(IPrincipalAccessEvaluator principalAccessEvaluator) + { + _principalAccessEvaluator = principalAccessEvaluator; + } + + public override async Task ValidatePrincipal(CookieValidatePrincipalContext context) + { + if (context.Principal?.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated) + { + context.RejectPrincipal(); + await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + return; + } + + var path = context.HttpContext.Request.Path; + if (!path.StartsWithSegments("/Admin", StringComparison.OrdinalIgnoreCase)) + { + return; + } + + var access = await _principalAccessEvaluator.EvaluateAsync(context.Principal); + if (!access.CanDelete) + { + context.RejectPrincipal(); + await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + } + } + } +} diff --git a/MaxInventory.Authentication/Core/AdminRoleClaimsTransformation.cs b/src/MaxInventory.Authentication/Core/AdminRoleClaimsTransformation.cs similarity index 100% rename from MaxInventory.Authentication/Core/AdminRoleClaimsTransformation.cs rename to src/MaxInventory.Authentication/Core/AdminRoleClaimsTransformation.cs diff --git a/MaxInventory.Authentication/Core/AuthRoles.cs b/src/MaxInventory.Authentication/Core/AuthRoles.cs similarity index 100% rename from MaxInventory.Authentication/Core/AuthRoles.cs rename to src/MaxInventory.Authentication/Core/AuthRoles.cs diff --git a/MaxInventory.Authentication/Core/ConfiguredAdminGroupSource.cs b/src/MaxInventory.Authentication/Core/ConfiguredAdminGroupSource.cs similarity index 100% rename from MaxInventory.Authentication/Core/ConfiguredAdminGroupSource.cs rename to src/MaxInventory.Authentication/Core/ConfiguredAdminGroupSource.cs diff --git a/MaxInventory.Authentication/Core/IdentityContext.cs b/src/MaxInventory.Authentication/Core/IdentityContext.cs similarity index 100% rename from MaxInventory.Authentication/Core/IdentityContext.cs rename to src/MaxInventory.Authentication/Core/IdentityContext.cs diff --git a/MaxInventory.Authentication/Core/IdentityContextFactory.cs b/src/MaxInventory.Authentication/Core/IdentityContextFactory.cs similarity index 100% rename from MaxInventory.Authentication/Core/IdentityContextFactory.cs rename to src/MaxInventory.Authentication/Core/IdentityContextFactory.cs diff --git a/MaxInventory.Authentication/Core/MemoryCacheTicketStore.cs b/src/MaxInventory.Authentication/Core/MemoryCacheTicketStore.cs similarity index 100% rename from MaxInventory.Authentication/Core/MemoryCacheTicketStore.cs rename to src/MaxInventory.Authentication/Core/MemoryCacheTicketStore.cs diff --git a/src/MaxInventory.Authentication/Core/PrincipalAccess.cs b/src/MaxInventory.Authentication/Core/PrincipalAccess.cs new file mode 100644 index 0000000..e3b5f60 --- /dev/null +++ b/src/MaxInventory.Authentication/Core/PrincipalAccess.cs @@ -0,0 +1,4 @@ +namespace MaxInventory.Authentication.Core +{ + public sealed record PrincipalAccess(bool IsAuthenticated, bool CanView, bool CanEdit, bool CanDelete); +} diff --git a/src/MaxInventory.Authentication/Core/PrincipalAccessEvaluator.cs b/src/MaxInventory.Authentication/Core/PrincipalAccessEvaluator.cs new file mode 100644 index 0000000..f037b64 --- /dev/null +++ b/src/MaxInventory.Authentication/Core/PrincipalAccessEvaluator.cs @@ -0,0 +1,62 @@ +using MaxInventory.Authentication.Abstractions; +using System.Security.Claims; + +namespace MaxInventory.Authentication.Core +{ + public sealed class PrincipalAccessEvaluator : IPrincipalAccessEvaluator + { + private static readonly string[] GroupClaimTypes = + { + "groups", + "group", + "role", + "roles", + ClaimTypes.GroupSid, + ClaimTypes.Role, + "http://schemas.microsoft.com/ws/2008/06/identity/claims/role", + "http://schemas.xmlsoap.org/claims/Group" + }; + + private readonly IAdminGroupSource _adminGroupSource; + + public PrincipalAccessEvaluator(IAdminGroupSource adminGroupSource) + { + _adminGroupSource = adminGroupSource; + } + + public async Task EvaluateAsync(ClaimsPrincipal principal, CancellationToken cancellationToken = default) + { + if (principal.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated) + { + return new PrincipalAccess(false, false, false, false); + } + + var principalGroups = identity.Claims + .Where(c => GroupClaimTypes.Contains(c.Type, StringComparer.OrdinalIgnoreCase)) + .Select(c => c.Value); + + var groupSet = new HashSet(principalGroups, StringComparer.OrdinalIgnoreCase); + + var isAdmin = principal.IsInRole(AuthRoles.Admin) || + IsInConfiguredGroup(groupSet, await _adminGroupSource.GetAdminGroupsAsync(cancellationToken)); + var canEdit = isAdmin || + principal.IsInRole(AuthRoles.EditAdd) || + IsInConfiguredGroup(groupSet, await _adminGroupSource.GetEditGroupsAsync(cancellationToken)); + var canView = canEdit || + principal.IsInRole(AuthRoles.View) || + IsInConfiguredGroup(groupSet, await _adminGroupSource.GetViewGroupsAsync(cancellationToken)); + + return new PrincipalAccess(true, canView, canEdit, isAdmin); + } + + private static bool IsInConfiguredGroup(HashSet principalGroups, IReadOnlyCollection configuredGroups) + { + if (configuredGroups.Count == 0) + { + return false; + } + + return configuredGroups.Any(principalGroups.Contains); + } + } +} diff --git a/MaxInventory.Authentication/Extensions/AuthenticationServiceCollectionExtensions.cs b/src/MaxInventory.Authentication/Extensions/AuthenticationServiceCollectionExtensions.cs similarity index 96% rename from MaxInventory.Authentication/Extensions/AuthenticationServiceCollectionExtensions.cs rename to src/MaxInventory.Authentication/Extensions/AuthenticationServiceCollectionExtensions.cs index 478c266..d18cacd 100644 --- a/MaxInventory.Authentication/Extensions/AuthenticationServiceCollectionExtensions.cs +++ b/src/MaxInventory.Authentication/Extensions/AuthenticationServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Configuration; @@ -22,6 +22,7 @@ namespace MaxInventory.Authentication.Extensions { services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddScoped(); services.AddScoped(); services.TryAddEnumerable(ServiceDescriptor.Singleton()); @@ -78,3 +79,4 @@ namespace MaxInventory.Authentication.Extensions } } } + diff --git a/MaxInventory.Authentication/MaxInventory.Authentication.csproj b/src/MaxInventory.Authentication/MaxInventory.Authentication.csproj similarity index 100% rename from MaxInventory.Authentication/MaxInventory.Authentication.csproj rename to src/MaxInventory.Authentication/MaxInventory.Authentication.csproj diff --git a/MaxInventory.Authentication/Providers/AdfsWsFed/AdfsWsFedAuthProvider.cs b/src/MaxInventory.Authentication/Providers/AdfsWsFed/AdfsWsFedAuthProvider.cs similarity index 100% rename from MaxInventory.Authentication/Providers/AdfsWsFed/AdfsWsFedAuthProvider.cs rename to src/MaxInventory.Authentication/Providers/AdfsWsFed/AdfsWsFedAuthProvider.cs diff --git a/MaxInventory.Authentication/Providers/Oidc/OidcAuthProvider.cs b/src/MaxInventory.Authentication/Providers/Oidc/OidcAuthProvider.cs similarity index 100% rename from MaxInventory.Authentication/Providers/Oidc/OidcAuthProvider.cs rename to src/MaxInventory.Authentication/Providers/Oidc/OidcAuthProvider.cs diff --git a/src/MaxInventory.Core/Settings/ApiSettings.cs b/src/MaxInventory.Core/Settings/ApiSettings.cs index b27f6ab..485f5f5 100644 --- a/src/MaxInventory.Core/Settings/ApiSettings.cs +++ b/src/MaxInventory.Core/Settings/ApiSettings.cs @@ -9,7 +9,7 @@ /// /// Base URL for the inventory API. /// - public string BaseUrl { get; set; } = "https://directauth.max.gov/inventory"; + public string? BaseUrl { get; set; } /// /// HTTP request timeout in seconds. Default is 30. diff --git a/src/MaxInventory.Core/Settings/CertificateSettings.cs b/src/MaxInventory.Core/Settings/CertificateSettings.cs index 0bf7d33..c95dec9 100644 --- a/src/MaxInventory.Core/Settings/CertificateSettings.cs +++ b/src/MaxInventory.Core/Settings/CertificateSettings.cs @@ -3,35 +3,17 @@ /// /// Configuration settings for client certificate authentication. /// Bound from the "Certificate" section of appsettings.json. - /// Supports two modes: Windows certificate store lookup (by friendly name) - /// or file-based PFX loading (for Docket/Linux environments). + /// Uses a mounted PFX file path, typically sourced from a Kubernetes secret volume. /// public sealed class CertificateSettings { /// - /// When true, loads certificate from a PFX file (FilePath + Password). - /// When false, searches the Windows machine certificate store by FriendlyName. + /// Path to the mounted PFX certificate file. /// - public bool UseFile { get; set; } = false; - - ///Friendly name of the certificate in the Windows machine store. - ///Used when UseFile is false. - /// - public string? FriendlyName { get; set; } = "alleman_j inventory.max.gov client"; - - /// - ///Certificate thumbprint in the windows machine store. - ///Used as a fallback when UseFile is false and FriendlyName lookup does not find a certificate. - /// - public string? Thumbprint { get; set; } + public string? Path { get; set; } /// - /// Path to the PFX certificate file. Used when UseFile is true. - /// - public string? FilePath { get; set; } - - /// - /// Password for the PFC certificate file. Used when UseFile is true. + /// Password for the PFX certificate file when one is required. /// public string? Password { get; set; } } diff --git a/src/MaxInventory.Infrastructure/Certificates/FileCertificateProvider.cs b/src/MaxInventory.Infrastructure/Certificates/FileCertificateProvider.cs index 932b5f8..7b88009 100644 --- a/src/MaxInventory.Infrastructure/Certificates/FileCertificateProvider.cs +++ b/src/MaxInventory.Infrastructure/Certificates/FileCertificateProvider.cs @@ -11,12 +11,12 @@ namespace MaxInventory.Infrastructure.Certificates public sealed class FileCertificateProvider : ICertificateProvider { private readonly string _filePath; - private readonly string _password; + private readonly string? _password; - public FileCertificateProvider(string filePath, string password) + public FileCertificateProvider(string filePath, string? password) { _filePath = filePath ?? throw new ArgumentNullException(nameof(filePath)); - _password = password ?? throw new ArgumentNullException(nameof(password)); + _password = password; } /// @@ -29,11 +29,15 @@ namespace MaxInventory.Infrastructure.Certificates $"is mounted or copied to the expected location.", _filePath); } - return new X509Certificate2( - _filePath, - _password, - X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet - ); + return string.IsNullOrWhiteSpace(_password) + ? new X509Certificate2( + _filePath, + (string?)null, + X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet) + : new X509Certificate2( + _filePath, + _password, + X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet); } } } diff --git a/src/MaxInventory.Infrastructure/Extensions/ServiceCollectionExtensions.cs b/src/MaxInventory.Infrastructure/Extensions/ServiceCollectionExtensions.cs index dfe7922..7f43a26 100644 --- a/src/MaxInventory.Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/src/MaxInventory.Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -5,7 +5,6 @@ using MaxInventory.Core.Services; using MaxInventory.Core.Settings; using MaxInventory.Infrastructure.Certificates; using MaxInventory.Infrastructure.Http; -using System.Runtime.CompilerServices; namespace MaxInventory.Infrastructure.Extensions { @@ -26,41 +25,28 @@ namespace MaxInventory.Infrastructure.Extensions // Bind settings from configuration var apiSettings = new ApiSettings(); configuration.GetSection("Api").Bind(apiSettings); + if (string.IsNullOrWhiteSpace(apiSettings.BaseUrl)) + { + throw new InvalidOperationException( + "Api.BaseUrl must be provided via configuration or Kubernetes secret."); + } services.AddSingleton(apiSettings); var certSettings = new CertificateSettings(); configuration.GetSection("Certificate").Bind(certSettings); + if (string.IsNullOrWhiteSpace(certSettings.Path)) + { + throw new InvalidOperationException( + "Certificate.Path must be provided via configuration and should point to the mounted Kubernetes secret file."); + } services.AddSingleton(certSettings); var proxySettings = new ProxySettings(); configuration.GetSection("Proxy").Bind(proxySettings); services.AddSingleton(proxySettings); - // Cert provider - if (certSettings.UseFile) - { - services.AddSingleton( - new FileCertificateProvider( - certSettings.FilePath ?? throw new InvalidOperationException( - "Certificate.FilePath must be set when Certificate.UseFile is true."), - certSettings.Password ?? throw new InvalidOperationException( - "Certificate.Password must be set when Certificate.UseFile is true.")) - ); - } - else - { - if (string.IsNullOrWhiteSpace(certSettings.FriendlyName) && - string.IsNullOrWhiteSpace(certSettings.Thumbprint)) - { - throw new InvalidOperationException( - "When Certificate.UseFile is false, set at least one of Certificate.FriendlyName or Certificate.Thumbprint."); - } - - services.AddSingleton( - new WindowsCertificateProvider( - certSettings.FriendlyName, - certSettings.Thumbprint)); - } + services.AddSingleton( + new FileCertificateProvider(certSettings.Path, certSettings.Password)); //Http Client services.AddSingleton(sp => diff --git a/src/MaxInventory.Infrastructure/Http/InventoryHttpClientFactory.cs b/src/MaxInventory.Infrastructure/Http/InventoryHttpClientFactory.cs index 8b9fbe6..47a9bab 100644 --- a/src/MaxInventory.Infrastructure/Http/InventoryHttpClientFactory.cs +++ b/src/MaxInventory.Infrastructure/Http/InventoryHttpClientFactory.cs @@ -34,7 +34,7 @@ namespace MaxInventory.Infrastructure.Http } //Ensure the base URL ends with a trailing slash for proper relative URL resolution - var baseUrl = apiSettings.BaseUrl.TrimEnd('/') + "/"; + var baseUrl = apiSettings.BaseUrl!.TrimEnd('/') + "/"; var client = new HttpClient(handler) { diff --git a/src/MaxInventory.Web/Components/Pages/ContentTypePage.razor.cs b/src/MaxInventory.Web/Components/Pages/ContentTypePage.razor.cs index a367bbb..acc599d 100644 --- a/src/MaxInventory.Web/Components/Pages/ContentTypePage.razor.cs +++ b/src/MaxInventory.Web/Components/Pages/ContentTypePage.razor.cs @@ -1,5 +1,6 @@ -namespace MaxInventory.Web.Components.Pages; +namespace MaxInventory.Web.Components.Pages; +using MaxInventory.Authentication.Abstractions; using MaxInventory.Core.Configuration; using MaxInventory.Core.Enums; using MaxInventory.Core.Models; @@ -28,10 +29,11 @@ public partial class ContentTypePage : ComponentBase [Inject] public required IInventoryApiClient ApiClient { get; set; } [Inject] public required IRelationshipRegistry RelationshipRegistry { get; set; } [Inject] public required IToastService ToastService { get; set; } + [Inject] public required IPrincipalAccessEvaluator PrincipalAccessEvaluator { get; set; } [Inject] public required AuthenticationStateProvider AuthenticationStateProvider { get; set; } - // ── Page State ─────────────────────────────────────────────────── + // -- Page State --------------------------------------------------- private ContentTypeDefinition? _definition; private ContentTypeDefinition? _formDefinition; private List _items = new(); @@ -54,7 +56,7 @@ public partial class ContentTypePage : ComponentBase /// private readonly Dictionary> _dataCache = new(); - // ── Lifecycle ──────────────────────────────────────────────────── + // -- Lifecycle ---------------------------------------------------- protected override async Task OnParametersSetAsync() { @@ -89,13 +91,13 @@ public partial class ContentTypePage : ComponentBase } if (EditId.HasValue) { - var editItem = _items.FirstOrDefault(i => i.Id == HighlightId.Value); + var editItem = _items.FirstOrDefault(i => i.Id == EditId.Value); if (editItem != null && _canEdit) ShowEditForm(editItem); } } - // ── Data Loading ───────────────────────────────────────────────── + // -- Data Loading ------------------------------------------------- private async Task LoadDataAsync() { @@ -157,7 +159,7 @@ public partial class ContentTypePage : ComponentBase } - // ── Row Selection ───────────────────────────────────────────────── + // -- Row Selection ------------------------------------------------- private void SelectItem(IInventoryEntity item) { @@ -202,7 +204,7 @@ public partial class ContentTypePage : ComponentBase } } - // ── CRUD ───────────────────────────────────────────────────────── + // -- CRUD --------------------------------------------------------- private void ShowAddForm() { @@ -338,7 +340,7 @@ public partial class ContentTypePage : ComponentBase } } - // ── Helpers ────────────────────────────────────────────────────── + // -- Helpers ------------------------------------------------------ private string GetDisplayName(IInventoryEntity entity) { @@ -378,13 +380,11 @@ public partial class ContentTypePage : ComponentBase private async Task ResolvePermissionsAsync() { var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - var user = state.User; - - _canView = user.Identity?.IsAuthenticated == true && - (user.IsInRole("View") || user.IsInRole("EditAdd") || user.IsInRole("Admin")); - _canEdit = user.Identity?.IsAuthenticated == true && - (user.IsInRole("EditAdd") || user.IsInRole("Admin")); - _canDelete = user.Identity?.IsAuthenticated == true && user.IsInRole("Admin"); + var access = await PrincipalAccessEvaluator.EvaluateAsync(state.User); + _canView = access.CanView; + _canEdit = access.CanEdit; + _canDelete = access.CanDelete; } -} \ No newline at end of file +} + diff --git a/src/MaxInventory.Web/MaxInventory.Web.csproj b/src/MaxInventory.Web/MaxInventory.Web.csproj index e75808e..ffbe2b2 100644 --- a/src/MaxInventory.Web/MaxInventory.Web.csproj +++ b/src/MaxInventory.Web/MaxInventory.Web.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/MaxInventory.Web/appsettings.json b/src/MaxInventory.Web/appsettings.json index 13e3502..61f90ad 100644 --- a/src/MaxInventory.Web/appsettings.json +++ b/src/MaxInventory.Web/appsettings.json @@ -7,14 +7,10 @@ }, "AllowedHosts": "*", "Api": { - "BaseUrl": "https://directauth.max.gov/inventory", "TimeoutSeconds": 30 }, "Certificate": { - "UseFile": false, - "FriendlyName": "alleman_j inventory.max.gov client", - "Thumbprint": "92e8b18ac60363bf2f761b7a0cd6d1228d707454", - "FilePath": null, + "Path": null, "Password": null }, "Proxy": {