-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some reorg, split IAuthenticationContext.cs into multiple files
- Loading branch information
1 parent
2790c76
commit 45863cc
Showing
8 changed files
with
128 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/Altinn.App.Core/Features/Auth/IAuthenticationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters