Repository navigation
feat(2fa): disableTwoFactor service method with enrollment_changed audit and group guard #186
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
Open
romanetar
wants to merge
2
commits into
feat/mfa-recovery-code-enrollment
Choose a base branch
from
feat/mfa-disable-two-factor-self-service
base: feat/mfa-recovery-code-enrollment
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| <?php namespace Tests; | ||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use App\libs\Auth\Models\TwoFactorAuditLog; | ||
| use App\Services\Auth\IDeviceTrustService; | ||
| use App\Services\Auth\IRecoveryCodeService; | ||
| use Auth\User; | ||
| use Illuminate\Support\Facades\DB; | ||
| use LaravelDoctrine\ORM\Facades\EntityManager; | ||
|
|
||
| /** | ||
| * Class DisableTwoFactorIntegrationTest | ||
| * IRecoveryCodeService::disableTwoFactor against the real database. | ||
| * @package Tests | ||
| */ | ||
| final class DisableTwoFactorIntegrationTest extends BrowserKitTestCase | ||
| { | ||
| private IRecoveryCodeService $service; | ||
| private User $user; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->service = $this->app->make(IRecoveryCodeService::class); | ||
|
|
||
| $user = new User(); | ||
| $user->setEmail('disable-2fa@nomail.com'); | ||
| $user->setFirstName('Disable'); | ||
| $user->setLastName('TwoFactor'); | ||
| $user->setIdentifier('disable-2fa@nomail.com'); | ||
| $user->setPassword('P@sswordS3cret'); | ||
| $user->verifyEmail(false); | ||
| EntityManager::persist($user); | ||
| EntityManager::flush(); | ||
| $this->user = $user; | ||
|
|
||
| // enrolled, with a code batch and two trusted devices | ||
| $this->service->enableTwoFactorAndGenerateCodes($user, User::MFAMethod_OTP); | ||
| $devices = $this->app->make(IDeviceTrustService::class); | ||
| $devices->trustDevice($user, 'agent-a', '127.0.0.1'); | ||
| $devices->trustDevice($user, 'agent-b', '127.0.0.1'); | ||
| } | ||
|
|
||
| private function userRow(): object | ||
| { | ||
| return DB::table('users')->where('id', $this->user->getId())->first(); | ||
| } | ||
|
|
||
| public function testDisableClearsEverything(): void | ||
| { | ||
| $this->assertSame(1, (int)$this->userRow()->two_factor_enabled); | ||
| $this->assertSame(10, DB::table('user_recovery_codes')->where('user_id', $this->user->getId())->count()); | ||
|
|
||
| $this->service->disableTwoFactor($this->user, null, null); | ||
|
|
||
| $row = $this->userRow(); | ||
| $this->assertSame(0, (int)$row->two_factor_enabled); | ||
| $this->assertNull($row->two_factor_enforced_at); | ||
| $this->assertSame(0, DB::table('user_recovery_codes')->where('user_id', $this->user->getId())->count()); | ||
|
|
||
| $devices = DB::table('user_trusted_devices')->where('user_id', $this->user->getId())->get(); | ||
| $this->assertCount(2, $devices); | ||
| foreach ($devices as $device) { | ||
| $this->assertSame(1, (int)$device->is_revoked); | ||
| } | ||
|
|
||
| $audit = DB::table('two_factor_audit_log') | ||
| ->where('user_id', $this->user->getId()) | ||
| ->where('event_type', TwoFactorAuditLog::EventEnrollmentChanged) | ||
| ->orderByDesc('id') | ||
| ->first(); | ||
| $this->assertNotNull($audit); | ||
| $metadata = json_decode($audit->metadata, true); | ||
| $this->assertFalse($metadata['enabled']); | ||
| $this->assertNull($metadata['actor_id']); | ||
| $this->assertSame('console', $metadata['reason']); | ||
| // settings_changed is the caller's responsibility | ||
| $this->assertSame(0, DB::table('two_factor_audit_log') | ||
| ->where('user_id', $this->user->getId()) | ||
| ->where('event_type', TwoFactorAuditLog::EventSettingsChanged) | ||
| ->count()); | ||
| } | ||
|
|
||
| public function testSelfServiceWithWrongPasswordLeavesStateUntouched(): void | ||
| { | ||
| try { | ||
| $this->service->disableTwoFactor($this->user, 'wrong', $this->user); | ||
| $this->fail('ValidationException expected'); | ||
| } catch (\models\exceptions\ValidationException $ex) { | ||
| } | ||
|
|
||
| $this->assertSame(1, (int)$this->userRow()->two_factor_enabled); | ||
| $this->assertSame(10, DB::table('user_recovery_codes')->where('user_id', $this->user->getId())->count()); | ||
| $this->assertSame(0, DB::table('user_trusted_devices') | ||
| ->where('user_id', $this->user->getId())->where('is_revoked', 1)->count()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@romanetar This adds a second
RecoveryCodeService::disableTwoFactor(): #169 already defines one with the same signature, and git merges this file without a conflict (onlyIRecoveryCodeService.phpand the two test files conflict).git merge-treeof this branch andfeat/mfa-disable-two-factor-serviceleaves both methods inRecoveryCodeService.php, andphp -lfails withCannot redeclare App\Services\Auth\RecoveryCodeService::disableTwoFactor(). The container resolves this class in theUserControllerandUserApiControllerconstructors, so a tree merged that way breaks those routes, login included. Details in the matching comment on #169.Separately, line 275 calls
revokeAllForUser(), which #160 and #169 replace withrevokeAllActiveForUser(User): UserTrustedDevice[].git merge-treeof this branch and #160 also merges this file without a conflict and keeps that call, so disabling 2FA would fail with "Call to undefined method" at runtime.The two implementations also disagree on audit semantics: this one writes a single
enrollment_changedrow and nodevice_revoked, while #169 writes onedevice_revoked(withdevice_id) per revoked device and leaves the enrollment/settings event to the caller (idp:reset-2fain #171 writessettings_changed). SDS rev 2.6 (fntechgit/ftn-docsnsklz#179) specifies the per-devicedevice_revokedfordisableTwoFactor.Suggested fix: keep a single method. Move the group guard,
lockUserRow(), therecovery_codes_acknowledged_atreset and the passwordless window into #169'sdisableTwoFactor()instead of adding a second definition here, switch the revocation torevokeAllActiveForUser()with onedevice_revokedper returned device, and decide whetherenrollment_changedis written by the service or by the caller so a reset does not record it twice. Then runphp -land phpunit on the combined tree.