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

fix: Fix ID-porten acr claim parsing #1299

Merged
merged 1 commit into from
Oct 16, 2024
Merged

Conversation

elsand
Copy link
Member

@elsand elsand commented Oct 15, 2024

Description

This fixes acr-parsing (authentication level) for real ID-porten tokens

Related Issue(s)

N/A

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)

Note

There is a bug in the token generator in https://github.com/Altinn/AltinnTestTools, which is still producing the old "Level3" and "Level4" acr-values.

Summary by CodeRabbit

  • New Features

    • Enhanced logic for determining authentication levels, improving efficiency and clarity.
    • Improved handling of authorization details for better data management.
  • Tests

    • Introduced unit tests for the authentication level parsing, ensuring accuracy and reliability of the new logic.

@elsand elsand requested a review from a team as a code owner October 15, 2024 13:01
Copy link
Contributor

coderabbitai bot commented Oct 15, 2024

📝 Walkthrough

Walkthrough

The pull request introduces modifications to the ClaimsPrincipalExtensions class, specifically the TryGetAuthenticationLevel method, streamlining its logic for determining authentication levels from claims. It also enhances the TryGetAuthorizationDetailsClaimValue method for better deserialization of authorization details. Additionally, a new test file is added to validate the behavior of the modified method, ensuring that authentication levels are correctly parsed from claims.

Changes

File Change Summary
src/Digdir.Domain.Dialogporten.Application/Common/Extensions/ClaimsPrincipalExtensions.cs Updated TryGetAuthenticationLevel method logic for streamlined authentication level determination; modified TryGetAuthorizationDetailsClaimValue for better deserialization.
tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/Features/V1/Common/Extensions/ClaimsPrincipalExtensionsTests.cs Added unit tests for TryGetAuthenticationLevel method, including three test cases for parsing authentication levels.

Possibly related PRs

Suggested reviewers

  • arealmaas
  • oskogstad

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@elsand elsand changed the title Fix ID-porten acr claim parsing fix: Fix ID-porten acr claim parsing Oct 15, 2024
Copy link

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (4)
tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/Features/V1/Common/Extensions/ClaimsPrincipalExtensionsTests.cs (2)

42-58: LGTM: Well-structured test for Altinn authentication level prioritization

The test is well-implemented, following the Arrange-Act-Assert pattern. It correctly verifies that the "urn:altinn:authlevel" claim takes precedence over the "acr" claim when both are present.

Consider adding an additional test case where the "urn:altinn:authlevel" claim has a lower value than the "acr" claim to ensure the prioritization works correctly in all scenarios.


6-59: Consider adding more test cases for comprehensive coverage

The current test suite covers the main scenarios well. To further improve the test coverage, consider adding the following test cases:

  1. Test with an invalid "acr" claim value to ensure proper error handling.
  2. Test with a missing "acr" claim to verify the behavior when the claim is not present.
  3. Test with an "acr" claim value that doesn't match any known level to ensure proper handling of unknown values.
  4. Test with a non-numeric "urn:altinn:authlevel" claim value to verify error handling.

These additional test cases will help ensure the robustness of the TryGetAuthenticationLevel method across various scenarios.

src/Digdir.Domain.Dialogporten.Application/Common/Extensions/ClaimsPrincipalExtensions.cs (2)

183-195: Define acr claim values as constants

For maintainability and to prevent potential typos, consider defining the acr claim values as constants. This practice promotes reuse and makes it easier to manage these values across the codebase.

You can add constants at the beginning of the class:

private const string IdportenLoaSubstantial = "idporten-loa-substantial";
private const string IdportenLoaHigh = "idporten-loa-high";

And update the switch statement:

authenticationLevel = claimValue switch
{
-    "idporten-loa-substantial" => 3,
-    "idporten-loa-high" => 4,
+    IdportenLoaSubstantial => 3,
+    IdportenLoaHigh => 4,
    _ => null
};

183-195: Handle unexpected acr claim values explicitly

If an unrecognized acr claim value is encountered, it might be helpful to log a warning for diagnostic purposes. This can aid in identifying misconfigurations or updates to the authentication system.

Would you like assistance in adding logging to handle unexpected acr claim values?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0deef0f and 9f7ec91.

📒 Files selected for processing (2)
  • src/Digdir.Domain.Dialogporten.Application/Common/Extensions/ClaimsPrincipalExtensions.cs (1 hunks)
  • tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/Features/V1/Common/Extensions/ClaimsPrincipalExtensionsTests.cs (1 hunks)
🧰 Additional context used
🔇 Additional comments (4)
tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/Features/V1/Common/Extensions/ClaimsPrincipalExtensionsTests.cs (3)

8-23: LGTM: Well-structured test for Level 3 authentication

The test is well-implemented, following the Arrange-Act-Assert pattern. It correctly verifies that the "idporten-loa-substantial" claim is parsed as authentication level 3.


25-40: LGTM: Well-structured test for Level 4 authentication

The test is properly implemented, adhering to the Arrange-Act-Assert pattern. It accurately verifies that the "idporten-loa-high" claim is parsed as authentication level 4.


1-59: Overall, well-implemented test suite for authentication level parsing

The test suite provides good coverage for the main scenarios of ID-porten and Altinn authentication level parsing. The tests are well-structured, following the Arrange-Act-Assert pattern, and effectively validate the behavior of the TryGetAuthenticationLevel method.

The implementation aligns well with the PR objectives of fixing ID-porten acr claim parsing. The tests cover both ID-porten levels (3 and 4) and the prioritization of Altinn authentication levels.

To further enhance the robustness of the test suite, consider implementing the additional test cases suggested in the previous comments. This will ensure comprehensive coverage of edge cases and error scenarios.

Great job on implementing these tests! They will help maintain the reliability of the authentication level parsing functionality.

src/Digdir.Domain.Dialogporten.Application/Common/Extensions/ClaimsPrincipalExtensions.cs (1)

177-180: Proper handling of AltinnAuthLevelClaim

The code correctly attempts to retrieve the AltinnAuthLevelClaim and parses it into an integer. This ensures that authentication levels from Altinn are appropriately handled.

@elsand elsand merged commit 8b8862f into main Oct 16, 2024
23 of 24 checks passed
@elsand elsand deleted the fix/parse-idporten-acr-header branch October 16, 2024 23:20
arealmaas pushed a commit that referenced this pull request Oct 17, 2024
🤖 I have created a release *beep* *boop*
---


##
[1.25.0](v1.24.0...v1.25.0)
(2024-10-17)


### Features

* **applications:** add scalers for cpu and memory
([#1295](#1295))
([eb0f19b](eb0f19b))
* **infrastructure:** create new yt01 app environment
([#1291](#1291))
([1a1ccc0](1a1ccc0))
* **service:** add permissions for service-bus
([#1305](#1305))
([7bf4177](7bf4177))
* **service:** deploy application in container apps
([#1303](#1303))
([a309044](a309044))


### Bug Fixes

* **applications:** add missing property for scale configuration
([3ffb724](3ffb724))
* **applications:** use correct scale configuration
([#1311](#1311))
([b8fb3cc](b8fb3cc))
* Fix ID-porten acr claim parsing
([#1299](#1299))
([8b8862f](8b8862f))
* **service:** ensure default credentials work
([#1306](#1306))
([b1e6a14](b1e6a14))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants