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

removepartId and disable auth #445

Merged
merged 2 commits into from
Nov 6, 2024
Merged

removepartId and disable auth #445

merged 2 commits into from
Nov 6, 2024

Conversation

Andreass2
Copy link
Collaborator

@Andreass2 Andreass2 commented Nov 6, 2024

Description

Related Issue(s)

  • #{issue number}

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)

Summary by CodeRabbit

  • New Features

    • Simplified the GetCorrespondenceOverview method by removing unnecessary parameters, enhancing user experience.
    • Streamlined correspondence handling by directly using identifiers instead of request objects.
  • Bug Fixes

    • Improved error handling for resource access, providing clearer feedback to users.
  • Chores

    • Removed the LegacyGetCorrespondenceOverviewRequest class, reducing complexity in the correspondence overview process.

Copy link
Contributor

coderabbitai bot commented Nov 6, 2024

📝 Walkthrough

Walkthrough

The changes primarily involve the LegacyCorrespondenceController and LegacyGetCorrespondenceOverviewHandler classes. The GetCorrespondenceOverview method in the controller has been simplified by removing the onBehalfOfPartyId parameter and adjusting the method's processing logic. Correspondingly, the handler now directly uses a Guid for correspondence identification instead of a request object. These modifications streamline the method signatures and internal logic, enhancing clarity and reducing complexity.

Changes

File Change Summary
src/Altinn.Correspondence.API/Controllers/LegacyCorrespondenceController.cs Updated GetCorrespondenceOverview method: removed onBehalfOfPartyId parameter, eliminated LegacyGetCorrespondenceOverviewRequest object, and adjusted handler.Process method to accept correspondenceId and cancellationToken.
src/Altinn.Correspondence.Application/GetCorrespondenceOverview/LegacyGetCorrespondenceOverviewHandler.cs Changed class to implement IHandler<Guid, LegacyGetCorrespondenceOverviewResponse>, updated Process method to accept Guid correspondenceId, and modified internal logic for user claims and error handling.

Possibly related PRs

Suggested labels

kind/bug, ignore-for-release

Suggested reviewers

  • CelineTrammi

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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@Andreass2 Andreass2 added ignore-for-release pull request wont be included in release notes kind/chore changes that do not relate to a fix or feature and don't modify src or test files labels Nov 6, 2024
@Andreass2 Andreass2 merged commit de7e8fa into main Nov 6, 2024
5 checks passed
@Andreass2 Andreass2 deleted the legacy/overview-party-id branch November 6, 2024 12:25
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 (3)
src/Altinn.Correspondence.API/Controllers/LegacyCorrespondenceController.cs (1)

Line range hint 59-59: Address TODO comment regarding response type.

The comment questions whether LegacyGetCorrespondenceHistoryResponse should be LegacyCorrespondenceHistoryExt. This should be resolved to maintain API consistency.

Would you like me to help analyze the response types and suggest the most appropriate one to use?

src/Altinn.Correspondence.Application/GetCorrespondenceOverview/LegacyGetCorrespondenceOverviewHandler.cs (2)

19-19: Ensure _userClaimsHelper Is Properly Initialized

A new private field _userClaimsHelper is introduced. Consider adding a null check in the constructor to prevent a potential NullReferenceException if userClaimsHelper is not provided.

Apply this diff to add a null check:

public LegacyGetCorrespondenceOverviewHandler(
    IAltinnAccessManagementService altinnAccessManagementService,
    IAltinnAuthorizationService altinnAuthorizationService,
    IAltinnRegisterService altinnRegisterService,
    ICorrespondenceRepository correspondenceRepository,
    ICorrespondenceStatusRepository correspondenceStatusRepository,
    UserClaimsHelper userClaimsHelper,
    ILogger<LegacyGetCorrespondenceOverviewHandler> logger)
{
    _altinnAccessManagementService = altinnAccessManagementService;
    _altinnAuthorizationService = altinnAuthorizationService;
    _altinnRegisterService = altinnRegisterService;
    _correspondenceRepository = correspondenceRepository;
    _correspondenceStatusRepository = correspondenceStatusRepository;
    _logger = logger;
-   _userClaimsHelper = userClaimsHelper;
+   _userClaimsHelper = userClaimsHelper ?? throw new ArgumentNullException(nameof(userClaimsHelper));
}

33-37: Improve Error Handling When Retrieving Party ID

When _userClaimsHelper.GetPartyId() fails to retrieve a valid partyId, the method returns Errors.NoAccessToResource. Consider adding logging to capture this event for easier debugging and to provide more context in logs.

Apply this diff to add a warning log:

if (_userClaimsHelper.GetPartyId() is not int partyId)
{
+   _logger.LogWarning("Failed to retrieve valid Party ID from user claims.");
    return Errors.NoAccessToResource;
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e0de97f and aea514c.

📒 Files selected for processing (2)
  • src/Altinn.Correspondence.API/Controllers/LegacyCorrespondenceController.cs (1 hunks)
  • src/Altinn.Correspondence.Application/GetCorrespondenceOverview/LegacyGetCorrespondenceOverviewHandler.cs (2 hunks)
🔇 Additional comments (3)
src/Altinn.Correspondence.API/Controllers/LegacyCorrespondenceController.cs (2)

Line range hint 18-21: Verify if authorization should be disabled.

The PR title mentions "disable auth", but the controller still has the [Authorize(Policy = AuthorizationConstants.Legacy)] attribute. Please clarify if the authorization should be removed or maintained.

Let's check for any pending authorization changes:


Line range hint 48-52: LGTM! Method signature and processing flow have been simplified.

The removal of onBehalfOfPartyId parameter and direct passing of correspondenceId to the handler makes the code cleaner and more straightforward.

Let's verify the authorization implications of this change:

src/Altinn.Correspondence.Application/GetCorrespondenceOverview/LegacyGetCorrespondenceOverviewHandler.cs (1)

11-11: ⚠️ Potential issue

Update Interface Implementation Carefully

The class now implements IHandler<Guid, LegacyGetCorrespondenceOverviewResponse> instead of IHandler<LegacyGetCorrespondenceOverviewRequest, LegacyGetCorrespondenceOverviewResponse>. Ensure that all references to this handler and its interface are updated throughout the codebase to prevent interface mismatches or runtime exceptions.

Run the following script to identify any references to the old interface:

✅ Verification successful

Interface Change is Safe and Properly Implemented

The change from IHandler<LegacyGetCorrespondenceOverviewRequest, LegacyGetCorrespondenceOverviewResponse> to IHandler<Guid, LegacyGetCorrespondenceOverviewResponse> is safe and properly implemented across the codebase:

  • The handler is correctly registered in DependencyInjection.cs
  • The controller correctly injects and uses the handler with a Guid parameter
  • No references to the old interface signature were found
  • The change aligns with similar handlers in the codebase that also use Guid as input parameter (e.g., GetCorrespondenceOverviewHandler, GetCorrespondenceDetailsHandler)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all references to the old interface implementation.

# Test: Search for usages of the old interface. Expect: No matches.
rg 'IHandler<LegacyGetCorrespondenceOverviewRequest, LegacyGetCorrespondenceOverviewResponse>'

Length of output: 96


Script:

#!/bin/bash
# Let's try a broader search approach to find any references to this handler class and its usage

# Search for any references to the handler class
rg "LegacyGetCorrespondenceOverviewHandler" -A 3 -B 3

# Search for any dependency injection registrations
rg "AddScoped|AddTransient|AddSingleton.*LegacyGetCorrespondenceOverviewHandler"

# Search for any references to the request type
rg "LegacyGetCorrespondenceOverviewRequest"

# Search for IHandler interface usage with any type parameters
rg "IHandler<.*>" -A 1 -B 1

Length of output: 22035

Comment on lines +49 to +54
/*var minimumAuthLevel = await _altinnAuthorizationService.CheckUserAccessAndGetMinimumAuthLevel(correspondence.ResourceId, new List<ResourceAccessLevel> { ResourceAccessLevel.Read }, cancellationToken);
if (minimumAuthLevel == null)
{
return Errors.LegacyNoAccessToCorrespondence;
}
} */
var minimumAuthLevel = 2;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Security Concern: Hardcoded Authorization Level Bypasses Checks

The original authorization check using _altinnAuthorizationService.CheckUserAccessAndGetMinimumAuthLevel is commented out and replaced with var minimumAuthLevel = 2;. This hardcodes the authorization level, potentially allowing unauthorized access and bypassing security measures.

Apply this diff to reinstate the authorization check:

-/*var minimumAuthLevel = await _altinnAuthorizationService.CheckUserAccessAndGetMinimumAuthLevel(correspondence.ResourceId, new List<ResourceAccessLevel> { ResourceAccessLevel.Read }, cancellationToken);
- if (minimumAuthLevel == null)
- {
-     return Errors.LegacyNoAccessToCorrespondence;
- } */
+var minimumAuthLevel = await _altinnAuthorizationService.CheckUserAccessAndGetMinimumAuthLevel(
+    correspondence.ResourceId,
+    new List<ResourceAccessLevel> { ResourceAccessLevel.Read },
+    cancellationToken);
+if (minimumAuthLevel == null)
+{
+    return Errors.LegacyNoAccessToCorrespondence;
+}

Do you need assistance in properly re-implementing the authorization logic?

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/*var minimumAuthLevel = await _altinnAuthorizationService.CheckUserAccessAndGetMinimumAuthLevel(correspondence.ResourceId, new List<ResourceAccessLevel> { ResourceAccessLevel.Read }, cancellationToken);
if (minimumAuthLevel == null)
{
return Errors.LegacyNoAccessToCorrespondence;
}
} */
var minimumAuthLevel = 2;
var minimumAuthLevel = await _altinnAuthorizationService.CheckUserAccessAndGetMinimumAuthLevel(
correspondence.ResourceId,
new List<ResourceAccessLevel> { ResourceAccessLevel.Read },
cancellationToken);
if (minimumAuthLevel == null)
{
return Errors.LegacyNoAccessToCorrespondence;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ignore-for-release pull request wont be included in release notes kind/chore changes that do not relate to a fix or feature and don't modify src or test files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants