Skip to content

Commit

Permalink
Some reorg, split IAuthenticationContext.cs into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
martinothamar committed Jan 9, 2025
1 parent 2790c76 commit 45863cc
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 119 deletions.
1 change: 1 addition & 0 deletions src/Altinn.App.Api/Controllers/AuthorizationController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Altinn.App.Api/Controllers/PartiesController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Altinn.App.Api/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.App.Core.Internal.Auth;
using Altinn.App.Core.Features.Auth;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
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;
using AltinnCore.Authentication.Constants;
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<IAuthenticationContext, AuthenticationContext>();
}
}
namespace Altinn.App.Core.Features.Auth;

/// <summary>
/// Contains information about the current logged in client/user.
Expand Down Expand Up @@ -586,104 +571,3 @@ internal sealed record SystemUserOrg(
[property: JsonPropertyName("ID")] string Id
);
}

/// <summary>
/// Provides access to the current authentication context.
/// </summary>
internal interface IAuthenticationContext
{
/// <summary>
/// The current authentication info.
/// </summary>
AuthenticationInfo Current { get; }
}

internal sealed class AuthenticationContext : IAuthenticationContext
{
private const string ItemsKey = "Internal_AltinnAuthenticationInfo";
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IOptionsMonitor<AppSettings> _appSettings;
private readonly IOptionsMonitor<GeneralSettings> _generalSettings;
private readonly IProfileClient _profileClient;
private readonly IAltinnPartyClient _altinnPartyClient;
private readonly IAuthorizationClient _authorizationClient;
private readonly IAppMetadata _appMetadata;

public AuthenticationContext(
IHttpContextAccessor httpContextAccessor,
IOptionsMonitor<AppSettings> appSettings,
IOptionsMonitor<GeneralSettings> 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;
}
}
}
121 changes: 121 additions & 0 deletions src/Altinn.App.Core/Features/Auth/IAuthenticationContext.cs
Original file line number Diff line number Diff line change
@@ -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<IAuthenticationContext, AuthenticationContext>();
}
}

/// <summary>
/// Provides access to the current authentication context.
/// </summary>
internal interface IAuthenticationContext
{
/// <summary>
/// The current authentication info.
/// </summary>
AuthenticationInfo Current { get; }
}

internal sealed class AuthenticationContext : IAuthenticationContext
{
private const string ItemsKey = "Internal_AltinnAuthenticationInfo";
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IOptionsMonitor<AppSettings> _appSettings;
private readonly IOptionsMonitor<GeneralSettings> _generalSettings;
private readonly IProfileClient _profileClient;
private readonly IAltinnPartyClient _altinnPartyClient;
private readonly IAuthorizationClient _authorizationClient;
private readonly IAppMetadata _appMetadata;

public AuthenticationContext(
IHttpContextAccessor httpContextAccessor,
IOptionsMonitor<AppSettings> appSettings,
IOptionsMonitor<GeneralSettings> 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

Check warning on line 68 in src/Altinn.App.Core/Features/Auth/IAuthenticationContext.cs

View workflow job for this annotation

GitHub Actions / Static code analysis

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/Altinn.App.Api.Tests/Utils/TestAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 45863cc

Please sign in to comment.