From 45863cc69cdfa2e30cdbcded8d6921ffbd8aaeba Mon Sep 17 00:00:00 2001 From: Martin Othamar Date: Thu, 9 Jan 2025 23:33:27 +0100 Subject: [PATCH] Some reorg, split IAuthenticationContext.cs into multiple files --- .../Controllers/AuthorizationController.cs | 1 + .../Controllers/PartiesController.cs | 1 + .../Controllers/ProfileController.cs | 2 +- .../Extensions/ServiceCollectionExtensions.cs | 1 + .../Auth/AuthenticationInfo.cs} | 118 +---------------- .../Features/Auth/IAuthenticationContext.cs | 121 ++++++++++++++++++ .../Authentication/AuthenticationClient.cs | 1 + .../Utils/TestAuthentication.cs | 2 +- 8 files changed, 128 insertions(+), 119 deletions(-) rename src/Altinn.App.Core/{Internal/Auth/IAuthenticationContext.cs => Features/Auth/AuthenticationInfo.cs} (82%) create mode 100644 src/Altinn.App.Core/Features/Auth/IAuthenticationContext.cs diff --git a/src/Altinn.App.Api/Controllers/AuthorizationController.cs b/src/Altinn.App.Api/Controllers/AuthorizationController.cs index 04cee2e82..c71e8d5b1 100644 --- a/src/Altinn.App.Api/Controllers/AuthorizationController.cs +++ b/src/Altinn.App.Api/Controllers/AuthorizationController.cs @@ -1,5 +1,6 @@ using System.Globalization; using Altinn.App.Core.Configuration; +using Altinn.App.Core.Features.Auth; using Altinn.App.Core.Internal.Auth; using Authorization.Platform.Authorization.Models; using Microsoft.AspNetCore.Authorization; diff --git a/src/Altinn.App.Api/Controllers/PartiesController.cs b/src/Altinn.App.Api/Controllers/PartiesController.cs index 183cdd938..12b3859c9 100644 --- a/src/Altinn.App.Api/Controllers/PartiesController.cs +++ b/src/Altinn.App.Api/Controllers/PartiesController.cs @@ -1,5 +1,6 @@ using System.Globalization; using Altinn.App.Core.Configuration; +using Altinn.App.Core.Features.Auth; using Altinn.App.Core.Helpers; using Altinn.App.Core.Internal.App; using Altinn.App.Core.Internal.Auth; diff --git a/src/Altinn.App.Api/Controllers/ProfileController.cs b/src/Altinn.App.Api/Controllers/ProfileController.cs index 23bbefce7..e7c5b6322 100644 --- a/src/Altinn.App.Api/Controllers/ProfileController.cs +++ b/src/Altinn.App.Api/Controllers/ProfileController.cs @@ -1,4 +1,4 @@ -using Altinn.App.Core.Internal.Auth; +using Altinn.App.Core.Features.Auth; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; diff --git a/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs b/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs index f3730f21a..43a328884 100644 --- a/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs @@ -1,6 +1,7 @@ using Altinn.App.Core.Configuration; using Altinn.App.Core.Features; using Altinn.App.Core.Features.Action; +using Altinn.App.Core.Features.Auth; using Altinn.App.Core.Features.DataLists; using Altinn.App.Core.Features.DataProcessing; using Altinn.App.Core.Features.ExternalApi; diff --git a/src/Altinn.App.Core/Internal/Auth/IAuthenticationContext.cs b/src/Altinn.App.Core/Features/Auth/AuthenticationInfo.cs similarity index 82% rename from src/Altinn.App.Core/Internal/Auth/IAuthenticationContext.cs rename to src/Altinn.App.Core/Features/Auth/AuthenticationInfo.cs index 46c16c79c..a396b7037 100644 --- a/src/Altinn.App.Core/Internal/Auth/IAuthenticationContext.cs +++ b/src/Altinn.App.Core/Features/Auth/AuthenticationInfo.cs @@ -1,11 +1,7 @@ using System.Globalization; using System.Text.Json; using System.Text.Json.Serialization; -using Altinn.App.Core.Configuration; using Altinn.App.Core.Helpers; -using Altinn.App.Core.Internal.App; -using Altinn.App.Core.Internal.Profile; -using Altinn.App.Core.Internal.Registers; using Altinn.App.Core.Models; using Altinn.Platform.Profile.Models; using Altinn.Platform.Register.Models; @@ -13,19 +9,8 @@ using AltinnCore.Authentication.Utils; using Authorization.Platform.Authorization.Models; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Options; -namespace Altinn.App.Core.Internal.Auth; - -internal static class AuthenticationContextDI -{ - internal static void AddAuthenticationContext(this IServiceCollection services) - { - services.TryAddSingleton(); - } -} +namespace Altinn.App.Core.Features.Auth; /// /// Contains information about the current logged in client/user. @@ -586,104 +571,3 @@ internal sealed record SystemUserOrg( [property: JsonPropertyName("ID")] string Id ); } - -/// -/// Provides access to the current authentication context. -/// -internal interface IAuthenticationContext -{ - /// - /// The current authentication info. - /// - AuthenticationInfo Current { get; } -} - -internal sealed class AuthenticationContext : IAuthenticationContext -{ - private const string ItemsKey = "Internal_AltinnAuthenticationInfo"; - private readonly IHttpContextAccessor _httpContextAccessor; - private readonly IOptionsMonitor _appSettings; - private readonly IOptionsMonitor _generalSettings; - private readonly IProfileClient _profileClient; - private readonly IAltinnPartyClient _altinnPartyClient; - private readonly IAuthorizationClient _authorizationClient; - private readonly IAppMetadata _appMetadata; - - public AuthenticationContext( - IHttpContextAccessor httpContextAccessor, - IOptionsMonitor appSettings, - IOptionsMonitor generalSettings, - IProfileClient profileClient, - IAltinnPartyClient altinnPartyClient, - IAuthorizationClient authorizationClient, - IAppMetadata appMetadata - ) - { - _httpContextAccessor = httpContextAccessor; - _appSettings = appSettings; - _generalSettings = generalSettings; - _profileClient = profileClient; - _altinnPartyClient = altinnPartyClient; - _authorizationClient = authorizationClient; - _appMetadata = appMetadata; - } - - // Currently we're coupling this to the HTTP context directly. - // In the future we might want to run work (e.g. service tasks) in the background, - // at which point we won't always have a HTTP context available. - // At that point we probably want to implement something like an `IExecutionContext`, `IExecutionContextAccessor` - // to decouple ourselves from the ASP.NET request context. - // TODO: consider removing dependcy on HTTP context - private HttpContext _httpContext => - _httpContextAccessor.HttpContext ?? throw new InvalidOperationException("No HTTP context available"); - - internal void ResolveCurrent(HttpContext httpContext) - { - var authInfo = AuthenticationInfo.From( - httpContext, - _appSettings.CurrentValue.RuntimeCookieName, - _generalSettings.CurrentValue.GetAltinnPartyCookieName, - _profileClient.GetUserProfile, - _altinnPartyClient.GetParty, - (string orgNr) => _altinnPartyClient.LookupParty(new PartyLookup { OrgNo = orgNr }), - _authorizationClient.GetPartyList, - _authorizationClient.ValidateSelectedParty, - _authorizationClient.GetUserRoles, - _appMetadata.GetApplicationMetadata - ); - httpContext.Items[ItemsKey] = authInfo; - } - - public AuthenticationInfo Current - { - get - { - var httpContext = _httpContext; - - AuthenticationInfo authInfo; - if (!httpContext.Items.TryGetValue(ItemsKey, out var authInfoObj)) - { - authInfo = AuthenticationInfo.From( - httpContext, - _appSettings.CurrentValue.RuntimeCookieName, - _generalSettings.CurrentValue.GetAltinnPartyCookieName, - _profileClient.GetUserProfile, - _altinnPartyClient.GetParty, - (string orgNr) => _altinnPartyClient.LookupParty(new PartyLookup { OrgNo = orgNr }), - _authorizationClient.GetPartyList, - _authorizationClient.ValidateSelectedParty, - _authorizationClient.GetUserRoles, - _appMetadata.GetApplicationMetadata - ); - httpContext.Items[ItemsKey] = authInfo; - } - else - { - authInfo = - authInfoObj as AuthenticationInfo - ?? throw new Exception("Unexpected type for authentication info in HTTP context"); - } - return authInfo; - } - } -} diff --git a/src/Altinn.App.Core/Features/Auth/IAuthenticationContext.cs b/src/Altinn.App.Core/Features/Auth/IAuthenticationContext.cs new file mode 100644 index 000000000..7fc9b066b --- /dev/null +++ b/src/Altinn.App.Core/Features/Auth/IAuthenticationContext.cs @@ -0,0 +1,121 @@ +using Altinn.App.Core.Configuration; +using Altinn.App.Core.Internal.App; +using Altinn.App.Core.Internal.Auth; +using Altinn.App.Core.Internal.Profile; +using Altinn.App.Core.Internal.Registers; +using Altinn.Platform.Register.Models; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Options; + +namespace Altinn.App.Core.Features.Auth; + +internal static class AuthenticationContextDI +{ + internal static void AddAuthenticationContext(this IServiceCollection services) + { + services.TryAddSingleton(); + } +} + +/// +/// Provides access to the current authentication context. +/// +internal interface IAuthenticationContext +{ + /// + /// The current authentication info. + /// + AuthenticationInfo Current { get; } +} + +internal sealed class AuthenticationContext : IAuthenticationContext +{ + private const string ItemsKey = "Internal_AltinnAuthenticationInfo"; + private readonly IHttpContextAccessor _httpContextAccessor; + private readonly IOptionsMonitor _appSettings; + private readonly IOptionsMonitor _generalSettings; + private readonly IProfileClient _profileClient; + private readonly IAltinnPartyClient _altinnPartyClient; + private readonly IAuthorizationClient _authorizationClient; + private readonly IAppMetadata _appMetadata; + + public AuthenticationContext( + IHttpContextAccessor httpContextAccessor, + IOptionsMonitor appSettings, + IOptionsMonitor generalSettings, + IProfileClient profileClient, + IAltinnPartyClient altinnPartyClient, + IAuthorizationClient authorizationClient, + IAppMetadata appMetadata + ) + { + _httpContextAccessor = httpContextAccessor; + _appSettings = appSettings; + _generalSettings = generalSettings; + _profileClient = profileClient; + _altinnPartyClient = altinnPartyClient; + _authorizationClient = authorizationClient; + _appMetadata = appMetadata; + } + + // Currently we're coupling this to the HTTP context directly. + // In the future we might want to run work (e.g. service tasks) in the background, + // at which point we won't always have a HTTP context available. + // At that point we probably want to implement something like an `IExecutionContext`, `IExecutionContextAccessor` + // to decouple ourselves from the ASP.NET request context. + // TODO: consider removing dependcy on HTTP context + private HttpContext _httpContext => + _httpContextAccessor.HttpContext ?? throw new InvalidOperationException("No HTTP context available"); + + internal void ResolveCurrent(HttpContext httpContext) + { + var authInfo = AuthenticationInfo.From( + httpContext, + _appSettings.CurrentValue.RuntimeCookieName, + _generalSettings.CurrentValue.GetAltinnPartyCookieName, + _profileClient.GetUserProfile, + _altinnPartyClient.GetParty, + (string orgNr) => _altinnPartyClient.LookupParty(new PartyLookup { OrgNo = orgNr }), + _authorizationClient.GetPartyList, + _authorizationClient.ValidateSelectedParty, + _authorizationClient.GetUserRoles, + _appMetadata.GetApplicationMetadata + ); + httpContext.Items[ItemsKey] = authInfo; + } + + public AuthenticationInfo Current + { + get + { + var httpContext = _httpContext; + + AuthenticationInfo authInfo; + if (!httpContext.Items.TryGetValue(ItemsKey, out var authInfoObj)) + { + authInfo = AuthenticationInfo.From( + httpContext, + _appSettings.CurrentValue.RuntimeCookieName, + _generalSettings.CurrentValue.GetAltinnPartyCookieName, + _profileClient.GetUserProfile, + _altinnPartyClient.GetParty, + (string orgNr) => _altinnPartyClient.LookupParty(new PartyLookup { OrgNo = orgNr }), + _authorizationClient.GetPartyList, + _authorizationClient.ValidateSelectedParty, + _authorizationClient.GetUserRoles, + _appMetadata.GetApplicationMetadata + ); + httpContext.Items[ItemsKey] = authInfo; + } + else + { + authInfo = + authInfoObj as AuthenticationInfo + ?? throw new Exception("Unexpected type for authentication info in HTTP context"); + } + return authInfo; + } + } +} diff --git a/src/Altinn.App.Core/Infrastructure/Clients/Authentication/AuthenticationClient.cs b/src/Altinn.App.Core/Infrastructure/Clients/Authentication/AuthenticationClient.cs index fac39f41f..8f851a471 100644 --- a/src/Altinn.App.Core/Infrastructure/Clients/Authentication/AuthenticationClient.cs +++ b/src/Altinn.App.Core/Infrastructure/Clients/Authentication/AuthenticationClient.cs @@ -2,6 +2,7 @@ using Altinn.App.Core.Configuration; using Altinn.App.Core.Constants; using Altinn.App.Core.Extensions; +using Altinn.App.Core.Features.Auth; using Altinn.App.Core.Internal.Auth; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/test/Altinn.App.Api.Tests/Utils/TestAuthentication.cs b/test/Altinn.App.Api.Tests/Utils/TestAuthentication.cs index 364a679f5..43b18db33 100644 --- a/test/Altinn.App.Api.Tests/Utils/TestAuthentication.cs +++ b/test/Altinn.App.Api.Tests/Utils/TestAuthentication.cs @@ -5,7 +5,7 @@ using Altinn.App.Core.Features.Maskinporten.Models; using Altinn.App.Core.Models; using AltinnCore.Authentication.Constants; -using static Altinn.App.Core.Internal.Auth.AuthenticationInfo; +using static Altinn.App.Core.Features.Auth.AuthenticationInfo; namespace Altinn.App.Api.Tests.Utils;