8000 [PM-18238] Add RequireTwoFactorPolicyRequirement by r-tome · Pull Request #5840 · bitwarden/server · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[PM-18238] Add RequireTwoFactorPolicyRequirement #5840

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

Merged
merged 37 commits into from
May 29, 2025

Conversation

r-tome
Copy link
Contributor
@r-tome r-tome commented May 20, 2025

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-18238

📔 Objective

Add a new RequireTwoFactorPolicyRequirement to be used for checking if a user complies with the 2FA policy on various services (UserService, RestoreOrganizationUserCommand, ConfirmOrganizationUserCommand, and AcceptOrgUserCommand)

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

r-tome added 3 commits May 8, 2025 15:29
* Implemented RequireTwoFactorPolicyRequirement to enforce two-factor authentication policies.
* Created RequireTwoFactorPolicyRequirementFactory to generate policy requirements based on user status.
* Added unit tests for the factory to validate behavior with various user statuses and policy details.
…ement

# Conflicts:
#	src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/AcceptOrgUserCommand.cs
Copy link
Contributor
github-actions bot commented May 20, 2025

Logo
Checkmarx One – Scan Summary & Details2d68b0dd-1875-4c4f-8f9e-94ad78390e87

Great job, no security vulnerabilities found in this Pull Request

r-tome added 4 commits May 20, 2025 11:19
…oFactorPolicyRequirement; update tests to reflect new logic for two-factor authentication policy handling.
…licy based on feature flag; update validation logic and tests accordingly.
…ication policy based on feature flag; refactor validation logic and update related tests for improved policy handling.
Copy link
codecov bot commented May 20, 2025

Codecov Report

Attention: Patch coverage is 95.23810% with 5 lines in your changes missing coverage. Please review.

Project coverage is 47.59%. Comparing base (c7b0c30) to head (0e208c3).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/Core/Services/Implementations/UserService.cs 84.00% 3 Missing and 1 partial ⚠️
...yRequirements/RequireTwoFactorPolicyRequirement.cs 93.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5840      +/-   ##
==========================================
- Coverage   50.82%   47.59%   -3.23%     
==========================================
  Files        1662     1663       +1     
  Lines       75231    75260      +29     
  Branches     6762     6764       +2     
==========================================
- Hits        38235    35819    -2416     
- Misses      35472    37976    +2504     
+ Partials     1524     1465      -59     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

r-tome added 7 commits May 20, 2025 17:09
…o determine user restoration eligibility based on two-factor authentication status; add corresponding unit tests for various scenarios.
…for two-factor authentication policies checks
…rPolicyRequirement and corresponding unit tests for policy retrieval based on user status
…rty in RequireTwoFactorPolicyRequirement to clarify its purpose and return value.
@r-tome r-tome requested a review from Copilot May 21, 2025 14:52
@@ -1782,72 +1775,6 @@ public async Task<List<Tuple<OrganizationUser, string>>> RevokeUsersAsync(Guid o
return result;
}

private async Task CheckPoliciesBeforeRestoreAsync(OrganizationUser orgUser, bool userHasTwoFactorEnabled)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unused method, leftover when lifting and shifting into command

Copy link
Contributor Author

Choose a reason for hiding this comment

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

vNext tests were redundant

Copilot

This comment was marked as outdated.

…Async method in ConfirmOrganizationUserCommand to clarify error handling for users without two-step login enabled.
@r-tome r-tome requested a review from Copilot May 21, 2025 15:46
Copy link
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces the RequireTwoFactorPolicyRequirement to enforce two‐factor authentication policies consistently across various user management commands. Key changes include updating tests and services (UserService, RestoreOrganizationUserCommand, ConfirmOrganizationUserCommand, and AcceptOrgUserCommand) to leverage the new policy requirement for two‐factor authentication, and refactoring dependency injections to support policy queries.

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/Core.Test/Services/UserServiceTests.cs Added dependency on IPolicyRequirementQuery and updated tests to verify two‐factor policy revocations.
test/Core.Test/AdminConsole/OrganizationFeatures/Policies/PolicyRequirements/RequireTwoFactorPolicyRequirementFactoryTests.cs Introduced and validated the policy requirement creation and behavior.
test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/RestoreUser/RestoreOrganizationUserCommandTests.cs Updated tests to assert behavior changes with policy requirements for 2FA.
test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/ConfirmOrganizationUserCommandTests.cs Adjusted tests to enforce policy checks during confirmation.
test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/AcceptOrgUserCommandTests.cs Modified tests to validate error and success paths with policy requirements.
src/Core/Services/Implementations/UserService.cs Integrated IPolicyRequirementQuery into two‐factor removal checks.
src/Core/AdminConsole/Services/Implementations/OrganizationService.cs Removed unused two‐factor query dependency as policies are now enforced via policy requirements.
src/Core/AdminConsole/OrganizationFeatures/Policies/PolicyRequirements/RequireTwoFactorPolicyRequirement.cs Added new policy requirement class and its factory for two‐factor authentication.
src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/RestoreUser/v1/RestoreOrganizationUserCommand.cs Updated restore logic to factor in the new two‐factor policy requirements.
src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/ConfirmOrganizationUserCommand.cs Introduced enhanced policy validation for user confirmation.
src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/AcceptOrgUserCommand.cs Incorporated policy-based two‐factor authentication validation during acceptance of organization users.
Comments suppressed due to low confidence (2)

src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/ConfirmOrganizationUserCommand.cs:206

  • [nitpick] Consider unifying the error message terminology to consistently refer to 'two-factor authentication' rather than 'two-step login', aligning with the rest of the codebase.
throw new BadRequestException("User does not have two-step login enabled.");

src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/AcceptOrgUserCommand.cs:261

  • [nitpick] Consider standardizing error messages across commands by replacing 'two-step login' with 'two-factor authentication' for consistency.
throw new BadRequestException("You cannot join this organization until you enable two-step login on your user account.");

@r-tome r-tome marked this pull request as ready for review May 22, 2025 10:14
@r-tome r-tome requested a review from a team as a code owner May 22, 2025 10:14
@r-tome r-tome requested a review from BTreston May 22, 2025 10:14
BTreston
BTreston previously approved these changes May 22, 2025
Copy link
Contributor
@BTreston BTreston left a comment

Choose a reason for hiding this comment

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

Looks good, just some non-blocking thoughts that might be worth investigating

Copy link
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new RequireTwoFactorPolicyRequirement along with the matching factory to enforce two‐factor authentication policies across multiple user management commands. Key changes include:

  • Implementation of the new 2FA policy requirement and its corresponding factory.
  • Updates to user management commands (UserService, Restore, Confirm, and Accept commands) to incorporate the 2FA policy checks based on a feature flag.
  • Addition of extensive unit tests for validating the new 2FA policy behaviors in various user scenarios.

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/Core.Test/Services/UserServiceTests.cs Updated tests to validate behavior when revoking 2FA providers using the new 2FA policy requirement.
test/Core.Test/AdminConsole/OrganizationFeatures/Policies/PolicyRequirements/RequireTwoFactorPolicyRequirementFactoryTests.cs Added unit tests for the new RequireTwoFactorPolicyRequirement factory.
test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/* Enhanced tests across Restore, Confirm, and Accept commands to check for correct 2FA policy enforcement.
src/Core/Services/Implementations/UserService.cs Injected and used IPolicyRequirementQuery for enforcing 2FA policies during provider removal.
src/Core/AdminConsole/OrganizationFeatures/Policies/* Introduced the new policy requirement implementation and factory for two-factor authentication.
src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/* Adjusted restore, confirm, and accept commands to conditionally enforce the new 2FA requirement when the feature flag is enabled.
Comments suppressed due to low confidence (1)

src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/ConfirmOrganizationUserCommand.cs:129

  • [nitpick] Consider ensuring consistency in naming the two-factor enablement flag across methods. In some areas it is referenced as 'twoFactorEnabled' while here it is renamed to 'userTwoFactorEnabled'.
var userTwoFactorEnabled = usersTwoFactorEnabled.FirstOrDefault(tuple => tuple.userId == user.Id).twoFactorIsEnabled;

r-tome and others added 2 commits May 23, 2025 14:19
…izationId, OrganizationUserId) for active memberships requiring two-factor authentication. Update UserService and related tests to reflect this change.
@r-tome r-tome requested review from BTreston and eliykat May 23, 2025 13:42
@r-tome r-tome requested a review from eliykat May 26, 2025 11:24
…to ensure correct validation of organization user policies.

var revokeOrgUserTasks = requirement.OrganizationsRequiringTwoFactor.Select(async o =>
{
var organization = await _organizationRepository.GetByIdAsync(o.OrganizationId);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can use the bulk method for this instead and store in a dict keyed by organizationId

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea!

Copy link
Member
@eliykat eliykat left a comment

Choose a reason for hiding this comment

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

Other than @BTreston 's suggested change, looks good to me!

…izing organization retrieval and logging when no organizations require two-factor authentication.
@r-tome r-tome requested review from eliykat and BTreston May 28, 2025 09:34
Copy link
Contributor
@BTreston BTreston left a comment

Choose a reason for hiding this comment

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

Looks good 👍

Copy link

@r-tome r-tome merged commit 829ce86 into main May 29, 2025
52 of 53 checks passed
@r-tome r-tome deleted the ac/pm-18238/add-requiretwofactorpolicyrequirement branch May 29, 2025 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3C46
Development

Successfully merging this pull request may close these issues.

3 participants
0