Repository navigation
feat(mfa): add IRecoveryCodeService::disableTwoFactor #169
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
4
commits into
feat/mfa-admin-enforcement-and-reset-commands
from
feat/mfa-disable-two-factor-service
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c3f0def
feat(mfa): add IRecoveryCodeService::disableTwoFactor
romanetar 38d6f9e
docs(mfa): document passwordless self-service in disableTwoFactor
romanetar be0d1c4
refactor(mfa): use revokeAllActiveForUser and audit one device_revoke…
romanetar 94e505c
test(mfa): expect one device_revoked audit event per device in Disabl…
romanetar 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
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,103 @@ | ||
| <?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); | ||
| } | ||
|
|
||
| // one device_revoked event per revoked device | ||
| $this->assertSame(2, DB::table('two_factor_audit_log') | ||
| ->where('user_id', $this->user->getId()) | ||
| ->where('event_type', TwoFactorAuditLog::EventDeviceRevoked) | ||
| ->count()); | ||
| // 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 #186 adds a second
RecoveryCodeService::disableTwoFactor()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-self-serviceleaves both methods inRecoveryCodeService.php(lines 238 and 300 of the merged file), 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.The two versions also differ in behaviour:
lockUserRow()and clearsrecovery_codes_acknowledged_at; this one does neither.revokeAllForUser(), which this PR and #160 remove. Merging feat(2fa): disableTwoFactor service method with enrollment_changed audit and group guard #186 with feat(2fa): trusted devices management UI and revoke endpoints #160 leaves that call in place (RecoveryCodeService.php:275).enrollment_changedrow inside the service, while this contract leaves the event to the caller andidp:reset-2fa(#171) writessettings_changed. A reset would end up writing both.Suggested fix: keep one method before either PR merges. This one is what #171 consumes and what SDS rev 2.6 describes (per-device
device_revoked, fntechgit/ftn-docsnsklz#179). Port #186's group guard, row lock, acknowledgment reset and passwordless window into it instead of adding a second definition, and decide whether the enrollment event comes from the service or the caller. Then runphp -land phpunit on the combined tree.