Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial IAuthenticationContext abstraction #880

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

martinothamar
Copy link
Contributor

@martinothamar martinothamar commented Nov 4, 2024

Description

Initial proposal for #763

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)
  • All tests run green

Documentation

  • User documentation is updated with a separate linked PR in altinn-studio-docs. (if applicable)

@martinothamar martinothamar added the feature Label Pull requests with new features. Used when generation releasenotes label Nov 4, 2024
@martinothamar martinothamar self-assigned this Nov 4, 2024
@martinothamar martinothamar force-pushed the feat/authn-abstraction branch from 3935f27 to 9a48939 Compare November 8, 2024 09:16
@martinothamar martinothamar changed the title Initial IClientContext abstraction Initial IAuthenticationContext abstraction Nov 8, 2024
@@ -334,7 +334,8 @@
var mockHttpClientFactory = fixture.HttpClientFactoryMock;
var mockMaskinportenClient = fixture.MaskinportenClientMock;
var mockHttpClient = new Mock<HttpClient>();
var altinnTokenResponse = PrincipalUtil.GetOrgToken("ttd");
var correspondencePayload = PayloadFactory.Send(authorisation: CorrespondenceAuthorisation.Maskinporten);

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning test

This assignment to
correspondencePayload
is useless, since its value is never read.
Copy link
Member

@ivarne ivarne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fin opprydding i mange av testene og autorisasjon :).

Har kommentert litt ved gjennomlesning

{
_authenticationClient = authenticationClient;
_settings = settings.Value;
_authenticationContext = serviceProvider.GetRequiredService<IAuthenticationContext>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment about why you inject IServiceProvider instead of IAuthenticationContext?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in code or just here? It's because the interface is internal for now

/// <returns>Party id for selected party. If invalid, partyId for logged in user is returned.</returns>
[Authorize]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[HttpGet("{org}/{app}/api/[controller]/current")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why /[controller]/ instead of authentication (with a lower case a)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to somehow annotate for swagger to document the possible response(s).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might remove or comment this out. I was orginally intending to replace 3 separate frontend requests with this endpoint, but put that on the backburner as it seemed difficult to refactor the frontend at the time. Wether a union is a good idea I'm not sure. I did have the start of a TS implementation that used the discriminator to figure which type it was, and a .NET client would be fine with it, but yeah it's not the most common thing

[JsonDerivedType(typeof(OrgResponse), typeDiscriminator: "Org")]
[JsonDerivedType(typeof(ServiceOwnerResponse), typeDiscriminator: "ServiceOwner")]
[JsonDerivedType(typeof(SystemUserResponse), typeDiscriminator: "SystemUser")]
private abstract record CurrentAuthenticationBaseResponse { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I like the idea of exposing a discriminated union in the api. Does OpenAPI generation support this nicely?

At least we need some docs on the properties

{
public required UserProfile Profile { get; init; }

public required Party Party { get; init; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the party of the authenticated user or the party he currently intends to represent as specified by the AltinnPartyId cookie (which is kind of useless because it will be wrong for one of the tabs if you have multiple instances for multiple reportees open at the same time )?

/// This means that the user has authenticated with a username/password, which can happen using
/// * Altinn "self registered users"
/// * IDporten through Ansattporten ("low"), MinID self registered eID
/// These have limited access to Altinn and can only represent themselves.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? At least if you come through Ansattporten, you likely represent the org you're employed at?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you end up as a user, and not with authentication_level = 0? I'll check...

{
var authenticationContext = (AuthenticationContext)
context.RequestServices.GetRequiredService<IAuthenticationContext>();
authenticationContext.ResolveCurrent(context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need a middleware here? Don't we already have a middleware that sets IHttpContextAccessor.Current?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember if there is a good reason anymore, I'll try to retrieve it lazily instead, without middleware

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has too many classes, records and interfaces. Would be nice to split it up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

@martinothamar martinothamar force-pushed the feat/authn-abstraction branch from df48e10 to 45863cc Compare January 9, 2025 22:34
Copy link
Contributor

@HauklandJ HauklandJ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm


var lookupPartyTask =
SelectedPartyId == userProfile.PartyId
? Task.FromResult((Party?)userProfile.Party)

Check warning

Code scanning / CodeQL

Cast to same type Warning

This cast is redundant because the expression already has type Party.
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
6.5% Coverage on New Code (required ≥ 65%)
C Reliability Rating on New Code (required ≥ A)
1.14% Condition Coverage on New Code (required ≥ 65%)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Label Pull requests with new features. Used when generation releasenotes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants