From a67c292bfd134f3e7aaa19cbe3a234f282a23cc0 Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Mon, 9 Nov 2026 13:17:53 -0400 Subject: [PATCH 01/11] fix(FOUR-34260): store per-user random TOTP secrets for 2FA Stop deriving 2FA secrets from public uuid and username, which allowed anyone with those fields to forge valid codes. Generate secrets with TOTP::generate(), persist encrypted on users.two_factor_secret, and add regression tests for predictable-code rejection and normal 2FA flow. Existing Authenticator App enrollments must scan a new QR after deploy. https://processmaker.atlassian.net/browse/FOUR-34260 --- ProcessMaker/Models/User.php | 2 + ProcessMaker/TwoFactorAuthentication.php | 21 ++- ...0_add_two_factor_secret_to_users_table.php | 27 +++ .../Auth/TwoFactorPredictableSecretTest.php | 156 ++++++++++++++++++ .../TwoFactorAuthenticationTest.php | 108 ++++++++++++ 5 files changed, 308 insertions(+), 6 deletions(-) create mode 100644 database/migrations/2026_11_09_120000_add_two_factor_secret_to_users_table.php create mode 100644 tests/Feature/Auth/TwoFactorPredictableSecretTest.php create mode 100644 tests/unit/ProcessMaker/TwoFactorAuthenticationTest.php diff --git a/ProcessMaker/Models/User.php b/ProcessMaker/Models/User.php index 5f15ac4e36..a32ffdcc3f 100644 --- a/ProcessMaker/Models/User.php +++ b/ProcessMaker/Models/User.php @@ -144,6 +144,7 @@ class User extends Authenticatable implements HasMedia 'loggedin_at' => 'datetime', 'schedule' => 'array', 'preferences_2fa' => 'array', + 'two_factor_secret' => 'encrypted', ]; /** @@ -238,6 +239,7 @@ public static function passwordRules(self $existing = null) */ protected $hidden = [ 'password', + 'two_factor_secret', 'groupMembersFromMemberable', 'permissions', ]; diff --git a/ProcessMaker/TwoFactorAuthentication.php b/ProcessMaker/TwoFactorAuthentication.php index 4fe8f04807..24d070cf77 100644 --- a/ProcessMaker/TwoFactorAuthentication.php +++ b/ProcessMaker/TwoFactorAuthentication.php @@ -8,7 +8,6 @@ use BaconQrCode\Writer; use Illuminate\Support\Facades\Notification; use OTPHP\TOTP; -use ParagonIE\ConstantTime\Base32; use ProcessMaker\Models\User; use ProcessMaker\Notifications\TwoFactorAuthNotification; use Twilio\Exceptions\TwilioException; @@ -45,17 +44,27 @@ public function sendCode(User $user): void } } - private function generateSecret(User $user): string + private function getUserTotpSecret(User $user): string { - $base = $user->uuid . '_' . $user->username; + if (!empty($user->two_factor_secret)) { + return $user->two_factor_secret; + } + + $secret = TOTP::generate()->getSecret(); + + if ($user->exists) { + $user->two_factor_secret = $secret; + $user->saveQuietly(); + } else { + $user->setAttribute('two_factor_secret', $secret); + } - return trim(Base32::encodeUpper($base), '='); + return $secret; } private function createOtpInstance(User $user, bool $forGoogleAuthApp = false) { - // Get secret - $secret = $this->generateSecret($user); + $secret = $this->getUserTotpSecret($user); // Config OTP $otp = TOTP::createFromSecret($secret); diff --git a/database/migrations/2026_11_09_120000_add_two_factor_secret_to_users_table.php b/database/migrations/2026_11_09_120000_add_two_factor_secret_to_users_table.php new file mode 100644 index 0000000000..2a4c89736d --- /dev/null +++ b/database/migrations/2026_11_09_120000_add_two_factor_secret_to_users_table.php @@ -0,0 +1,27 @@ +text('two_factor_secret')->nullable()->after('preferences_2fa'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('two_factor_secret'); + }); + } +}; diff --git a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php new file mode 100644 index 0000000000..aa9b9714e1 --- /dev/null +++ b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php @@ -0,0 +1,156 @@ + true, + 'password-policies.2fa_method' => [TwoFactorAuthentication::AUTH_APP], + ]); + } + + private function codeFromPublicIdentity(string $uuid, string $username, string $mode = 'app'): string + { + $secret = trim(Base32::encodeUpper($uuid . '_' . $username), '='); + $otp = TOTP::createFromSecret($secret); + + if ($mode === 'email') { + $otp->setDigest('sha512'); + $otp->setDigits(8); + $otp->setPeriod(300); + } + + return $otp->now(); + } + + private function victimAtTwoFactorGate(): User + { + return User::factory()->create([ + 'password' => Hash::make('password'), + 'preferences_2fa' => [TwoFactorAuthentication::AUTH_APP], + ]); + } + + private function postTwoFactorCode(User $sessionUser, string $code) + { + return $this->actingAs($sessionUser, 'web') + ->post(route('2fa.validate'), ['code' => $code]); + } + + public function testProtectedRouteRedirectsToTwoFactorUntilValidated(): void + { + $victim = $this->victimAtTwoFactorGate(); + + $this->actingAs($victim, 'web') + ->get(route('about.index')) + ->assertRedirect(route('2fa')); + } + + public function testWrongCodeDoesNotClearTwoFactorGate(): void + { + $victim = $this->victimAtTwoFactorGate(); + + $this->postTwoFactorCode($victim, '000000') + ->assertRedirect(route('2fa')); + + $this->assertNotTrue(session()->get(TwoFactorAuthController::TFA_VALIDATED)); + + $this->actingAs($victim, 'web') + ->get(route('about.index')) + ->assertRedirect(route('2fa')); + } + + public function testCodeDerivedFromAnotherUsersPublicIdentityDoesNotValidateVictimSession(): void + { + $victim = $this->victimAtTwoFactorGate(); + $attacker = User::factory()->create(); + + $codeForAttacker = $this->codeFromPublicIdentity($attacker->uuid, $attacker->username); + + $this->postTwoFactorCode($victim, $codeForAttacker) + ->assertRedirect(route('2fa')); + + $this->assertNotTrue(session()->get(TwoFactorAuthController::TFA_VALIDATED)); + } + + public function testVictimSessionRejectsCodeComputedFromPublicUuidAndUsernameOnly(): void + { + $victim = $this->victimAtTwoFactorGate(); + $attacker = User::factory()->create(); + + $harvested = User::query() + ->whereKey($victim->id) + ->first(['uuid', 'username']); + + $this->assertNotSame($attacker->uuid, $harvested->uuid); + + $code = $this->codeFromPublicIdentity($harvested->uuid, $harvested->username); + + $this->postTwoFactorCode($victim, $code) + ->assertRedirect(route('2fa')); + + $this->assertNotTrue(session()->get(TwoFactorAuthController::TFA_VALIDATED)); + + $this->actingAs($victim, 'web') + ->get(route('about.index')) + ->assertRedirect(route('2fa')); + } + + public function testVictimSessionRejectsEightDigitEmailModeCodeFromPublicIdentity(): void + { + config([ + 'password-policies.2fa_method' => [TwoFactorAuthentication::EMAIL], + ]); + + $victim = User::factory()->create([ + 'password' => Hash::make('password'), + 'preferences_2fa' => [TwoFactorAuthentication::EMAIL], + ]); + + $code = $this->codeFromPublicIdentity($victim->uuid, $victim->username, 'email'); + + $this->postTwoFactorCode($victim, $code) + ->assertRedirect(route('2fa')); + + $this->assertNotTrue(session()->get(TwoFactorAuthController::TFA_VALIDATED)); + } + + public function testVictimSessionAcceptsCodeFromStoredSecret(): void + { + $secret = TOTP::generate()->getSecret(); + $victim = User::factory()->create([ + 'password' => Hash::make('password'), + 'preferences_2fa' => [TwoFactorAuthentication::AUTH_APP], + 'two_factor_secret' => $secret, + ]); + + $code = TOTP::createFromSecret($secret)->now(); + + $this->postTwoFactorCode($victim, $code) + ->assertRedirect(route('login')); + + $this->assertTrue(session()->get(TwoFactorAuthController::TFA_VALIDATED)); + + $this->actingAs($victim, 'web') + ->get(route('about.index')) + ->assertOk(); + } +} diff --git a/tests/unit/ProcessMaker/TwoFactorAuthenticationTest.php b/tests/unit/ProcessMaker/TwoFactorAuthenticationTest.php new file mode 100644 index 0000000000..4ae213f5d2 --- /dev/null +++ b/tests/unit/ProcessMaker/TwoFactorAuthenticationTest.php @@ -0,0 +1,108 @@ +getMethod('getUserTotpSecret'); + $method->setAccessible(true); + + return $method->invoke(new TwoFactorAuthentication(), $user); + } + + private function codeFromPublicIdentity(string $uuid, string $username, bool $emailMode = false): string + { + $secret = trim(Base32::encodeUpper($uuid . '_' . $username), '='); + $otp = TOTP::createFromSecret($secret); + + if ($emailMode) { + $otp->setDigest('sha512'); + $otp->setDigits(8); + $otp->setPeriod(300); + } + + return $otp->now(); + } + + public function testUserTotpSecretIsNotDerivedFromUuidAndUsername(): void + { + $user = User::factory()->create([ + 'uuid' => self::UUID, + 'username' => self::USERNAME, + ]); + + $secret = $this->invokeGetUserTotpSecret($user); + $predictable = trim(Base32::encodeUpper(self::UUID . '_' . self::USERNAME), '='); + + $this->assertNotSame($predictable, $secret); + + $user->refresh(); + $this->assertSame($secret, $user->two_factor_secret); + } + + public function testValidateCodeRejectsCodeDerivedFromPublicIdentityOnly(): void + { + $user = User::factory()->create([ + 'uuid' => self::UUID, + 'username' => self::USERNAME, + ]); + + $this->invokeGetUserTotpSecret($user); + + $this->assertFalse( + (new TwoFactorAuthentication())->validateCode( + $user, + $this->codeFromPublicIdentity(self::UUID, self::USERNAME) + ) + ); + $this->assertFalse( + (new TwoFactorAuthentication())->validateCode( + $user, + $this->codeFromPublicIdentity(self::UUID, self::USERNAME, true) + ) + ); + } + + public function testValidateCodeAcceptsCodeFromStoredSecret(): void + { + $secret = TOTP::generate()->getSecret(); + $user = User::factory()->create([ + 'two_factor_secret' => $secret, + ]); + + $appCode = TOTP::createFromSecret($secret)->now(); + $this->assertTrue((new TwoFactorAuthentication())->validateCode($user, $appCode)); + + $emailOtp = TOTP::createFromSecret($secret); + $emailOtp->setDigest('sha512'); + $emailOtp->setDigits(8); + $emailOtp->setPeriod(300); + $this->assertTrue((new TwoFactorAuthentication())->validateCode($user, $emailOtp->now())); + } + + public function testValidateCodeRejectsRandomCode(): void + { + $user = User::factory()->create(); + + $this->assertFalse((new TwoFactorAuthentication())->validateCode($user, '000000')); + } +} From 51235c12b69c7f0e039e337cb7d6b538f1e533ed Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Mon, 9 Nov 2026 19:27:20 -0400 Subject: [PATCH 02/11] fix(FOUR-34260): use per-user TOTP secrets and reprovision legacy 2FA users Stop deriving 2FA codes from public uuid and username. Persist CSPRNG secrets encrypted on users.two_factor_secret, add regression tests, and on login route legacy users without a secret to QR (auth app only) or force a fresh email/SMS code. Run the migration on deploy. Authenticator App users must scan a new QR. https://processmaker.atlassian.net/browse/FOUR-34260 --- .../Auth/TwoFactorAuthController.php | 16 +++++++++-- .../Auth/TwoFactorPredictableSecretTest.php | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php index ed50fda849..d9ca2bfbaa 100644 --- a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php +++ b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php @@ -40,11 +40,22 @@ public function displayTwoFactorAuthForm(Request $request) return redirect()->route('login'); } + $user->refresh(); + if (empty($user->two_factor_secret)) { + $methods = $user->getValid2FAPreferences(); + if (in_array(TwoFactorAuthentication::AUTH_APP, $methods, true) + && !in_array(TwoFactorAuthentication::EMAIL, $methods, true) + && !in_array(TwoFactorAuthentication::SMS, $methods, true) + ) { + return redirect()->route('2fa.auth_app_qr'); + } + } + // Send code if (!session()->has(self::TFA_ERROR) && !session()->has(self::TFA_MESSAGE)) { $this->twoFactorAuthentication->sendCode($user); } else { - if (!session()->has(self::TFA_MESSAGE)) { + if (!session()->has(self::TFA_MESSAGE) || empty($user->two_factor_secret)) { $this->twoFactorAuthentication->sendCode($user); } } @@ -153,7 +164,7 @@ public function testSettings(Request $request) if (count($enabled) === 0) { $message = [ 'status' => 'error', - 'message' => __('The two-step method must be selected.') + 'message' => __('The two-step method must be selected.'), ]; $status = 500; } @@ -237,6 +248,7 @@ public static function check2faByGroups() { try { $user = Auth::user(); + return $user->in2FAGroupOrIndependent(); } catch (Exception $e) { session()->put(self::TFA_ERROR, $e->getMessage()); diff --git a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php index aa9b9714e1..047373e94a 100644 --- a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php +++ b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php @@ -5,10 +5,12 @@ namespace Tests\Feature\Auth; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Notification; use OTPHP\TOTP; use ParagonIE\ConstantTime\Base32; use ProcessMaker\Http\Controllers\Auth\TwoFactorAuthController; use ProcessMaker\Models\User; +use ProcessMaker\Notifications\TwoFactorAuthNotification; use ProcessMaker\TwoFactorAuthentication; use Tests\TestCase; @@ -133,6 +135,31 @@ public function testVictimSessionRejectsEightDigitEmailModeCodeFromPublicIdentit $this->assertNotTrue(session()->get(TwoFactorAuthController::TFA_VALIDATED)); } + public function testLegacyUserProvisioningWhenSecretIsMissing(): void + { + Notification::fake(); + + $authOnly = User::factory()->create([ + 'password' => Hash::make('password'), + 'preferences_2fa' => [TwoFactorAuthentication::AUTH_APP], + 'two_factor_secret' => null, + ]); + $this->actingAs($authOnly, 'web') + ->get(route('2fa')) + ->assertRedirect(route('2fa.auth_app_qr')); + + config(['password-policies.2fa_method' => [TwoFactorAuthentication::EMAIL]]); + $emailUser = User::factory()->create([ + 'password' => Hash::make('password'), + 'preferences_2fa' => [TwoFactorAuthentication::EMAIL], + 'two_factor_secret' => null, + ]); + $this->actingAs($emailUser, 'web')->get(route('2fa'))->assertOk(); + + Notification::assertSentTo($emailUser, TwoFactorAuthNotification::class); + $this->assertNotEmpty($emailUser->fresh()->two_factor_secret); + } + public function testVictimSessionAcceptsCodeFromStoredSecret(): void { $secret = TOTP::generate()->getSecret(); From daade4e33baba279f319072a3f3f5bab9d75180c Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 11:34:59 -0400 Subject: [PATCH 03/11] fix(FOUR-34260): use __() for 2FA translations - TwoFactorAuthNotification mail subject and body line - TwoFactorAuthController invalid code errors Fixes sync mail send hanging on /2fa when subject used _(). https://processmaker.atlassian.net/browse/FOUR-34260 --- .../Http/Controllers/Auth/TwoFactorAuthController.php | 4 ++-- ProcessMaker/Notifications/TwoFactorAuthNotification.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php index d9ca2bfbaa..6f165289da 100644 --- a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php +++ b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php @@ -87,7 +87,7 @@ public function validateTwoFactorAuthCode(Request $request) // If empty code return error message if (empty($code)) { // Set error message - session()->put(self::TFA_ERROR, _('Invalid code.')); + session()->put(self::TFA_ERROR, __('Invalid code.')); // Return to 2fa page return redirect()->route('2fa'); @@ -109,7 +109,7 @@ public function validateTwoFactorAuthCode(Request $request) $route = 'login'; } else { // Set error message - session()->put(self::TFA_ERROR, _('Invalid code.')); + session()->put(self::TFA_ERROR, __('Invalid code.')); // Return to 2fa page $route = '2fa'; diff --git a/ProcessMaker/Notifications/TwoFactorAuthNotification.php b/ProcessMaker/Notifications/TwoFactorAuthNotification.php index 2b36e3953a..35204ba566 100644 --- a/ProcessMaker/Notifications/TwoFactorAuthNotification.php +++ b/ProcessMaker/Notifications/TwoFactorAuthNotification.php @@ -19,7 +19,7 @@ class TwoFactorAuthNotification extends Notification /** * Create a new notification instance. * - * @param \ProcessMaker\Models\User $user + * @param User $user * @param string $code */ public function __construct(User $user, string $code) @@ -43,12 +43,12 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { return (new MailMessage) - ->subject(_('Security Code')) + ->subject(__('Security Code')) ->greeting($this->user->username) ->line(__('This is your security code: :code', ['code' => $this->code])); } From a046a925c688edad8c71cc38b1b893d3c4bf5ef7 Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 12:18:30 -0400 Subject: [PATCH 04/11] fix(FOUR-34260): align 2FA screens with shared auth Decisions layout Refactor otp and auth_app_qr to extend auth.layouts.auth instead of legacy standalone HTML, background assets, and inline CSS. Use existing skip-language- selector and skip-auth-language-scripts sections so 2FA matches password-reset branding without changing the auth layout. https://processmaker.atlassian.net/browse/FOUR-34260 --- .../views/auth/2fa/auth_app_qr.blade.php | 145 +++------- resources/views/auth/2fa/otp.blade.php | 265 ++++++------------ 2 files changed, 121 insertions(+), 289 deletions(-) diff --git a/resources/views/auth/2fa/auth_app_qr.blade.php b/resources/views/auth/2fa/auth_app_qr.blade.php index 8e047304ed..da71c9f7b1 100644 --- a/resources/views/auth/2fa/auth_app_qr.blade.php +++ b/resources/views/auth/2fa/auth_app_qr.blade.php @@ -1,75 +1,42 @@ - - - - - - - - - - {{ __('Configure the authenticator app') }} - @vite('resources/sass/app.scss') - - - -
-
-
-
-
- @php - $isMobile = ( - isset($_SERVER['HTTP_USER_AGENT']) - && \ProcessMaker\Helpers\MobileHelper::isMobile($_SERVER['HTTP_USER_AGENT']) - ) ? true : false; - @endphp - @if (!$isMobile) -
- ProcessMaker - -
- @endif -
-
-
-
- @component('components.logo') - @endcomponent -

-

-
- {{__('Configure the authenticator app')}} -
-
- {{__('1.- Download the Google Authenticator App')}} -
-
- {{__('2.- On the Google Authenticator app click on the + icon')}} -
-
- {{__('3.- Select "Scan QR code" option')}} -
-
- QR -
- -
- -
-
-
-
-
+@extends('auth.layouts.auth') + +@section('skip-language-selector', 'skip') +@section('skip-auth-language-scripts', 'skip') + +@section('title') +{{ __('Configure the authenticator app') }} +@endsection + +@section('content') +
+
+ {{__('Configure the authenticator app')}}
+
+ {{__('1.- Download the Google Authenticator App')}} +
+
+ {{__('2.- On the Google Authenticator app click on the + icon')}} +
+
+ {{__('3.- Select "Scan QR code" option')}} +
+
+QR + +
+
- +@endsection + +@push('scripts') - - +@endpush diff --git a/resources/views/auth/2fa/otp.blade.php b/resources/views/auth/2fa/otp.blade.php index aba8a68b26..eb3ddecf34 100644 --- a/resources/views/auth/2fa/otp.blade.php +++ b/resources/views/auth/2fa/otp.blade.php @@ -1,105 +1,88 @@ - - - - - - - - - - {{ __('Enter Security Code') }} - @vite('resources/sass/app.scss') - - - -
-
-
-
-
- @php - $isMobile = ( - isset($_SERVER['HTTP_USER_AGENT']) - && \ProcessMaker\Helpers\MobileHelper::isMobile($_SERVER['HTTP_USER_AGENT']) - ) ? true : false; - @endphp - @if (!$isMobile) -
- ProcessMaker - -
- @endif -
-
-
-
- @component('components.logo') - @endcomponent -
-
- @if (session()->has('2fa-message') && !session()->has('2fa-error')) -
{{ session()->get('2fa-message')}}
- @endif - @if (session()->has('2fa-error')) -
{{ session()->get('2fa-error')}}
- @endif +@extends('auth.layouts.auth') -
- -
- - - @if ($errors->has('code')) - - {{ $errors->first('code') }} - - @endif -
-
-
- -
- @if (in_array(\ProcessMaker\TwoFactorAuthentication::AUTH_APP, - config('password-policies.2fa_method', []))) - - {{ __('Authenticator app') }} - -
- @endif - - {{ __('Log in as another user') }} - -
-
-
- -
-
-
-
-
+@section('skip-language-selector', 'skip') +@section('skip-auth-language-scripts', 'skip') + +@section('title') +{{ __('Enter Security Code') }} +@endsection + +@section('css') + +@endsection + +@section('content') +
+ @if (session()->has('2fa-message') && !session()->has('2fa-error')) +
{{ session()->get('2fa-message')}}
+ @endif + @if (session()->has('2fa-error')) +
{{ session()->get('2fa-error')}}
+ @endif + +
+ +
+ + + @if ($errors->has('code')) + + {{ $errors->first('code') }} + + @endif +
+
+
+ +
+ @if (in_array(\ProcessMaker\TwoFactorAuthentication::AUTH_APP, + config('password-policies.2fa_method', []))) + + {{ __('Authenticator app') }} + +
+ @endif + + {{ __('Log in as another user') }} +
-
- +
+ +
+ +@endsection + +@push('scripts') - - +@endpush From 4a3f991da53037a62f27d92570db0d34bacc9e85 Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 12:23:18 -0400 Subject: [PATCH 05/11] fix(FOUR-34260): reload 2FA user without flushing permission caches Use fresh() instead of User::refresh() on /2fa display so two_factor_secret is read from the database without invalidating permissions cache. https://processmaker.atlassian.net/browse/FOUR-34260 --- ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php index 6f165289da..06e43f6b76 100644 --- a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php +++ b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php @@ -40,7 +40,8 @@ public function displayTwoFactorAuthForm(Request $request) return redirect()->route('login'); } - $user->refresh(); + // Reload DB state without User::refresh() (clears permission caches). + $user = $user->fresh() ?? $user; if (empty($user->two_factor_secret)) { $methods = $user->getValid2FAPreferences(); if (in_array(TwoFactorAuthentication::AUTH_APP, $methods, true) From f922d9697d400e4e5a8d89f4f28b085b140ac7ff Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 12:58:08 -0400 Subject: [PATCH 06/11] fix(FOUR-34260): split 2FA secrets and harden authenticator QR enrollment Store email/SMS TOTP in two_factor_delivery_secret and authenticator TOTP in two_factor_secret. Gate /2fa/auth_app_qr on enabled Authenticator App and empty enrollment secret; reject six-digit codes unless the method is allowed. Extend the users migration, tighten OTP link visibility, and add unit/feature coverage for predictable-secret bypass and QR route access. https://processmaker.atlassian.net/browse/FOUR-34260 --- .../Auth/TwoFactorAuthController.php | 20 ++++- ProcessMaker/Models/User.php | 2 + ProcessMaker/TwoFactorAuthentication.php | 46 +++++++++- ...0_add_two_factor_secret_to_users_table.php | 3 +- resources/views/auth/2fa/otp.blade.php | 7 +- .../Auth/TwoFactorPredictableSecretTest.php | 31 ++++++- .../TwoFactorAuthenticationTest.php | 89 +++++++++++++++---- 7 files changed, 172 insertions(+), 26 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php index 06e43f6b76..b70cf657c3 100644 --- a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php +++ b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php @@ -56,7 +56,12 @@ public function displayTwoFactorAuthForm(Request $request) if (!session()->has(self::TFA_ERROR) && !session()->has(self::TFA_MESSAGE)) { $this->twoFactorAuthentication->sendCode($user); } else { - if (!session()->has(self::TFA_MESSAGE) || empty($user->two_factor_secret)) { + $methods = $user->getValid2FAPreferences(); + $needsProvisioning = in_array(TwoFactorAuthentication::EMAIL, $methods, true) + || in_array(TwoFactorAuthentication::SMS, $methods, true) + ? empty($user->two_factor_delivery_secret) + : empty($user->two_factor_secret); + if (!session()->has(self::TFA_MESSAGE) || $needsProvisioning) { $this->twoFactorAuthentication->sendCode($user); } } @@ -141,10 +146,21 @@ public function displayAuthAppQr(Request $request) $user = $request->user(); // If not user not authenticated, redirect to login page - if (empty($user)) { + if (empty($user) || !config('password-policies.2fa_enabled', false)) { return redirect()->route('login'); } + $user = $user->fresh() ?? $user; + $methods = $user->getValid2FAPreferences(); + + if (!in_array(TwoFactorAuthentication::AUTH_APP, $methods, true)) { + return redirect()->route('2fa'); + } + + if (!empty($user->two_factor_secret)) { + return redirect()->route('2fa'); + } + // Generate QR code $qrCode = $this->twoFactorAuthentication->generateQr($user); diff --git a/ProcessMaker/Models/User.php b/ProcessMaker/Models/User.php index a32ffdcc3f..68fe61ef04 100644 --- a/ProcessMaker/Models/User.php +++ b/ProcessMaker/Models/User.php @@ -145,6 +145,7 @@ class User extends Authenticatable implements HasMedia 'schedule' => 'array', 'preferences_2fa' => 'array', 'two_factor_secret' => 'encrypted', + 'two_factor_delivery_secret' => 'encrypted', ]; /** @@ -240,6 +241,7 @@ public static function passwordRules(self $existing = null) protected $hidden = [ 'password', 'two_factor_secret', + 'two_factor_delivery_secret', 'groupMembersFromMemberable', 'permissions', ]; diff --git a/ProcessMaker/TwoFactorAuthentication.php b/ProcessMaker/TwoFactorAuthentication.php index 24d070cf77..86e4f1fc5a 100644 --- a/ProcessMaker/TwoFactorAuthentication.php +++ b/ProcessMaker/TwoFactorAuthentication.php @@ -62,9 +62,29 @@ private function getUserTotpSecret(User $user): string return $secret; } + private function getUserDeliveryTotpSecret(User $user): string + { + if (!empty($user->two_factor_delivery_secret)) { + return $user->two_factor_delivery_secret; + } + + $secret = TOTP::generate()->getSecret(); + + if ($user->exists) { + $user->two_factor_delivery_secret = $secret; + $user->saveQuietly(); + } else { + $user->setAttribute('two_factor_delivery_secret', $secret); + } + + return $secret; + } + private function createOtpInstance(User $user, bool $forGoogleAuthApp = false) { - $secret = $this->getUserTotpSecret($user); + $secret = $forGoogleAuthApp + ? $this->getUserTotpSecret($user) + : $this->getUserDeliveryTotpSecret($user); // Config OTP $otp = TOTP::createFromSecret($secret); @@ -91,11 +111,31 @@ private function getCodeForEmailSms(User $user): string public function validateCode(User $user, string $code) { + $methods = $user->getValid2FAPreferences(); + // The code is for Google Authenticator app? - $forGoogleAuthApp = strlen($code) === 6; + if (strlen($code) === 6) { + if (!in_array(self::AUTH_APP, $methods, true) || empty($user->two_factor_secret)) { + return false; + } + + // Create OTP instance + $otp = $this->createOtpInstance($user, true); + + // Validate code + return $otp->verify($code); + } + + if (!in_array(self::EMAIL, $methods, true) && !in_array(self::SMS, $methods, true)) { + return false; + } + + if (empty($user->two_factor_delivery_secret)) { + return false; + } // Create OTP instance - $otp = $this->createOtpInstance($user, $forGoogleAuthApp); + $otp = $this->createOtpInstance($user, false); // Validate code return $otp->verify($code); diff --git a/database/migrations/2026_11_09_120000_add_two_factor_secret_to_users_table.php b/database/migrations/2026_11_09_120000_add_two_factor_secret_to_users_table.php index 2a4c89736d..87ef9ce8cf 100644 --- a/database/migrations/2026_11_09_120000_add_two_factor_secret_to_users_table.php +++ b/database/migrations/2026_11_09_120000_add_two_factor_secret_to_users_table.php @@ -12,6 +12,7 @@ public function up(): void { Schema::table('users', function (Blueprint $table) { $table->text('two_factor_secret')->nullable()->after('preferences_2fa'); + $table->text('two_factor_delivery_secret')->nullable()->after('two_factor_secret'); }); } @@ -21,7 +22,7 @@ public function up(): void public function down(): void { Schema::table('users', function (Blueprint $table) { - $table->dropColumn('two_factor_secret'); + $table->dropColumn(['two_factor_secret', 'two_factor_delivery_secret']); }); } }; diff --git a/resources/views/auth/2fa/otp.blade.php b/resources/views/auth/2fa/otp.blade.php index eb3ddecf34..190c804b79 100644 --- a/resources/views/auth/2fa/otp.blade.php +++ b/resources/views/auth/2fa/otp.blade.php @@ -57,8 +57,11 @@ class="form-control{{ $errors->has('code') ? ' is-invalid' : '' }}"
- @if (in_array(\ProcessMaker\TwoFactorAuthentication::AUTH_APP, - config('password-policies.2fa_method', []))) + @php + $twoFactorMethods = auth()->user()->getValid2FAPreferences(); + @endphp + @if (in_array(\ProcessMaker\TwoFactorAuthentication::AUTH_APP, $twoFactorMethods, true) + && empty(auth()->user()->two_factor_secret)) {{ __('Authenticator app') }} diff --git a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php index 047373e94a..1672d1d6b2 100644 --- a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php +++ b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php @@ -157,7 +157,36 @@ public function testLegacyUserProvisioningWhenSecretIsMissing(): void $this->actingAs($emailUser, 'web')->get(route('2fa'))->assertOk(); Notification::assertSentTo($emailUser, TwoFactorAuthNotification::class); - $this->assertNotEmpty($emailUser->fresh()->two_factor_secret); + $emailUser->refresh(); + $this->assertNotEmpty($emailUser->two_factor_delivery_secret); + $this->assertEmpty($emailUser->two_factor_secret); + } + + public function testAuthAppQrRouteRequiresAuthenticatorEnrollment(): void + { + config(['password-policies.2fa_method' => [TwoFactorAuthentication::EMAIL]]); + + $emailUser = User::factory()->create([ + 'password' => Hash::make('password'), + 'preferences_2fa' => [TwoFactorAuthentication::EMAIL], + ]); + + $this->actingAs($emailUser, 'web') + ->get(route('2fa.auth_app_qr')) + ->assertRedirect(route('2fa')); + + $secret = TOTP::generate()->getSecret(); + $authUser = User::factory()->create([ + 'password' => Hash::make('password'), + 'preferences_2fa' => [TwoFactorAuthentication::AUTH_APP], + 'two_factor_secret' => $secret, + ]); + + config(['password-policies.2fa_method' => [TwoFactorAuthentication::AUTH_APP]]); + + $this->actingAs($authUser, 'web') + ->get(route('2fa.auth_app_qr')) + ->assertRedirect(route('2fa')); } public function testVictimSessionAcceptsCodeFromStoredSecret(): void diff --git a/tests/unit/ProcessMaker/TwoFactorAuthenticationTest.php b/tests/unit/ProcessMaker/TwoFactorAuthenticationTest.php index 4ae213f5d2..57ae6a16d7 100644 --- a/tests/unit/ProcessMaker/TwoFactorAuthenticationTest.php +++ b/tests/unit/ProcessMaker/TwoFactorAuthenticationTest.php @@ -29,6 +29,15 @@ private function invokeGetUserTotpSecret(User $user): string return $method->invoke(new TwoFactorAuthentication(), $user); } + private function invokeGetUserDeliveryTotpSecret(User $user): string + { + $class = new ReflectionClass(TwoFactorAuthentication::class); + $method = $class->getMethod('getUserDeliveryTotpSecret'); + $method->setAccessible(true); + + return $method->invoke(new TwoFactorAuthentication(), $user); + } + private function codeFromPublicIdentity(string $uuid, string $username, bool $emailMode = false): string { $secret = trim(Base32::encodeUpper($uuid . '_' . $username), '='); @@ -59,49 +68,95 @@ public function testUserTotpSecretIsNotDerivedFromUuidAndUsername(): void $this->assertSame($secret, $user->two_factor_secret); } + public function testUserDeliveryTotpSecretIsNotDerivedFromUuidAndUsername(): void + { + $user = User::factory()->create([ + 'uuid' => self::UUID, + 'username' => self::USERNAME, + ]); + + $secret = $this->invokeGetUserDeliveryTotpSecret($user); + $predictable = trim(Base32::encodeUpper(self::UUID . '_' . self::USERNAME), '='); + + $this->assertNotSame($predictable, $secret); + + $user->refresh(); + $this->assertSame($secret, $user->two_factor_delivery_secret); + $this->assertEmpty($user->two_factor_secret); + } + public function testValidateCodeRejectsCodeDerivedFromPublicIdentityOnly(): void { $user = User::factory()->create([ 'uuid' => self::UUID, 'username' => self::USERNAME, + 'preferences_2fa' => [TwoFactorAuthentication::EMAIL], ]); - $this->invokeGetUserTotpSecret($user); + $this->invokeGetUserDeliveryTotpSecret($user); + $service = new TwoFactorAuthentication(); $this->assertFalse( - (new TwoFactorAuthentication())->validateCode( - $user, - $this->codeFromPublicIdentity(self::UUID, self::USERNAME) - ) + $service->validateCode($user, $this->codeFromPublicIdentity(self::UUID, self::USERNAME)) ); $this->assertFalse( - (new TwoFactorAuthentication())->validateCode( - $user, - $this->codeFromPublicIdentity(self::UUID, self::USERNAME, true) - ) + $service->validateCode($user, $this->codeFromPublicIdentity(self::UUID, self::USERNAME, true)) ); } - public function testValidateCodeAcceptsCodeFromStoredSecret(): void + public function testValidateCodeAcceptsCodeFromStoredSecrets(): void { - $secret = TOTP::generate()->getSecret(); + config([ + 'password-policies.2fa_method' => [ + TwoFactorAuthentication::AUTH_APP, + TwoFactorAuthentication::EMAIL, + ], + ]); + + $authSecret = TOTP::generate()->getSecret(); + $deliverySecret = TOTP::generate()->getSecret(); $user = User::factory()->create([ - 'two_factor_secret' => $secret, + 'preferences_2fa' => [ + TwoFactorAuthentication::AUTH_APP, + TwoFactorAuthentication::EMAIL, + ], + 'two_factor_secret' => $authSecret, + 'two_factor_delivery_secret' => $deliverySecret, ]); - $appCode = TOTP::createFromSecret($secret)->now(); - $this->assertTrue((new TwoFactorAuthentication())->validateCode($user, $appCode)); + $service = new TwoFactorAuthentication(); - $emailOtp = TOTP::createFromSecret($secret); + $appCode = TOTP::createFromSecret($authSecret)->now(); + $this->assertTrue($service->validateCode($user, $appCode)); + + $emailOtp = TOTP::createFromSecret($deliverySecret); $emailOtp->setDigest('sha512'); $emailOtp->setDigits(8); $emailOtp->setPeriod(300); - $this->assertTrue((new TwoFactorAuthentication())->validateCode($user, $emailOtp->now())); + $this->assertTrue($service->validateCode($user, $emailOtp->now())); + } + + public function testValidateCodeRejectsSixDigitForEmailOnlyUser(): void + { + config(['password-policies.2fa_method' => [TwoFactorAuthentication::EMAIL]]); + + $deliverySecret = TOTP::generate()->getSecret(); + $authSecret = TOTP::generate()->getSecret(); + $user = User::factory()->create([ + 'preferences_2fa' => [TwoFactorAuthentication::EMAIL], + 'two_factor_secret' => $authSecret, + 'two_factor_delivery_secret' => $deliverySecret, + ]); + + $sixDigitFromAuthSecret = TOTP::createFromSecret($authSecret)->now(); + $this->assertFalse((new TwoFactorAuthentication())->validateCode($user, $sixDigitFromAuthSecret)); } public function testValidateCodeRejectsRandomCode(): void { - $user = User::factory()->create(); + $user = User::factory()->create([ + 'preferences_2fa' => [TwoFactorAuthentication::EMAIL], + ]); $this->assertFalse((new TwoFactorAuthentication())->validateCode($user, '000000')); } From ffea1a0ec9e06fb2c9db8a02a14e551ae0f430c1 Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 14:53:16 -0400 Subject: [PATCH 07/11] fix(FOUR-34260): reduce returns in displayAuthAppQr for Sonar S1142 Combine authenticator-method and existing-secret checks into one redirect to /2fa so displayAuthAppQr stays within the return-statement limit. https://processmaker.atlassian.net/browse/FOUR-34260 --- .../Http/Controllers/Auth/TwoFactorAuthController.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php index b70cf657c3..d6149b6b70 100644 --- a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php +++ b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php @@ -153,11 +153,9 @@ public function displayAuthAppQr(Request $request) $user = $user->fresh() ?? $user; $methods = $user->getValid2FAPreferences(); - if (!in_array(TwoFactorAuthentication::AUTH_APP, $methods, true)) { - return redirect()->route('2fa'); - } - - if (!empty($user->two_factor_secret)) { + if (!in_array(TwoFactorAuthentication::AUTH_APP, $methods, true) + || !empty($user->two_factor_secret) + ) { return redirect()->route('2fa'); } From 9b7085830aa891b87dd80e580d1ed061b3dbcfc2 Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 14:55:51 -0400 Subject: [PATCH 08/11] fix(FOUR-34260): lower cognitive complexity in displayTwoFactorAuthForm Extract authenticator QR redirect and send-code provisioning into private helpers so Sonar S3776 stays within the limit. https://processmaker.atlassian.net/browse/FOUR-34260 --- .../Auth/TwoFactorAuthController.php | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php index d6149b6b70..bd29b95331 100644 --- a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php +++ b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php @@ -42,29 +42,12 @@ public function displayTwoFactorAuthForm(Request $request) // Reload DB state without User::refresh() (clears permission caches). $user = $user->fresh() ?? $user; - if (empty($user->two_factor_secret)) { - $methods = $user->getValid2FAPreferences(); - if (in_array(TwoFactorAuthentication::AUTH_APP, $methods, true) - && !in_array(TwoFactorAuthentication::EMAIL, $methods, true) - && !in_array(TwoFactorAuthentication::SMS, $methods, true) - ) { - return redirect()->route('2fa.auth_app_qr'); - } + if ($this->shouldRedirectToAuthenticatorQrEnrollment($user)) { + return redirect()->route('2fa.auth_app_qr'); } // Send code - if (!session()->has(self::TFA_ERROR) && !session()->has(self::TFA_MESSAGE)) { - $this->twoFactorAuthentication->sendCode($user); - } else { - $methods = $user->getValid2FAPreferences(); - $needsProvisioning = in_array(TwoFactorAuthentication::EMAIL, $methods, true) - || in_array(TwoFactorAuthentication::SMS, $methods, true) - ? empty($user->two_factor_delivery_secret) - : empty($user->two_factor_secret); - if (!session()->has(self::TFA_MESSAGE) || $needsProvisioning) { - $this->twoFactorAuthentication->sendCode($user); - } - } + $this->sendTwoFactorCodeForDisplay($user); // Set informative message $methodsNames = $this->twoFactorAuthentication->friendlyMethodsNames($user); @@ -166,6 +149,38 @@ public function displayAuthAppQr(Request $request) return view('auth.2fa.auth_app_qr', compact('qrCode')); } + private function shouldRedirectToAuthenticatorQrEnrollment(User $user): bool + { + if (!empty($user->two_factor_secret)) { + return false; + } + + $methods = $user->getValid2FAPreferences(); + + return in_array(TwoFactorAuthentication::AUTH_APP, $methods, true) + && !in_array(TwoFactorAuthentication::EMAIL, $methods, true) + && !in_array(TwoFactorAuthentication::SMS, $methods, true); + } + + private function sendTwoFactorCodeForDisplay(User $user): void + { + if (!session()->has(self::TFA_ERROR) && !session()->has(self::TFA_MESSAGE)) { + $this->twoFactorAuthentication->sendCode($user); + + return; + } + + $methods = $user->getValid2FAPreferences(); + $needsProvisioning = in_array(TwoFactorAuthentication::EMAIL, $methods, true) + || in_array(TwoFactorAuthentication::SMS, $methods, true) + ? empty($user->two_factor_delivery_secret) + : empty($user->two_factor_secret); + + if (!session()->has(self::TFA_MESSAGE) || $needsProvisioning) { + $this->twoFactorAuthentication->sendCode($user); + } + } + public function testSettings(Request $request) { $enabled = $request->json('enabled'); From f8319357b2550a66ab16ea9bce6612012651683e Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 15:00:43 -0400 Subject: [PATCH 09/11] fix(FOUR-34260): use single return in validateCode for Sonar S1142 Accumulate validation in a $valid flag and return once so validateCode stays within the return-statement limit without changing 2FA verification behavior. https://processmaker.atlassian.net/browse/FOUR-34260 --- ProcessMaker/TwoFactorAuthentication.php | 32 +++++++++++------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/ProcessMaker/TwoFactorAuthentication.php b/ProcessMaker/TwoFactorAuthentication.php index 86e4f1fc5a..b4a2546278 100644 --- a/ProcessMaker/TwoFactorAuthentication.php +++ b/ProcessMaker/TwoFactorAuthentication.php @@ -112,33 +112,29 @@ private function getCodeForEmailSms(User $user): string public function validateCode(User $user, string $code) { $methods = $user->getValid2FAPreferences(); + $valid = false; // The code is for Google Authenticator app? if (strlen($code) === 6) { - if (!in_array(self::AUTH_APP, $methods, true) || empty($user->two_factor_secret)) { - return false; - } + if (in_array(self::AUTH_APP, $methods, true) && !empty($user->two_factor_secret)) { + // Create OTP instance + $otp = $this->createOtpInstance($user, true); + // Validate code + $valid = $otp->verify($code); + } + } elseif ( + (in_array(self::EMAIL, $methods, true) || in_array(self::SMS, $methods, true)) + && !empty($user->two_factor_delivery_secret) + ) { // Create OTP instance - $otp = $this->createOtpInstance($user, true); + $otp = $this->createOtpInstance($user, false); // Validate code - return $otp->verify($code); - } - - if (!in_array(self::EMAIL, $methods, true) && !in_array(self::SMS, $methods, true)) { - return false; + $valid = $otp->verify($code); } - if (empty($user->two_factor_delivery_secret)) { - return false; - } - - // Create OTP instance - $otp = $this->createOtpInstance($user, false); - - // Validate code - return $otp->verify($code); + return $valid; } /** From 0a72e9cbaf1df864c26dbfeca80930f3a2014658 Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 15:07:27 -0400 Subject: [PATCH 10/11] fix(FOUR-34260): keep authenticator secret in session until first valid code Defer persisting two_factor_secret until a six-digit code verifies. Store the QR provisioning secret in session so enrollment can be retried without locking users out of the authenticator link or QR page. https://processmaker.atlassian.net/browse/FOUR-34260 --- ProcessMaker/TwoFactorAuthentication.php | 47 +++++++++++++++---- resources/views/auth/2fa/otp.blade.php | 3 +- .../Auth/TwoFactorPredictableSecretTest.php | 7 +++ 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/ProcessMaker/TwoFactorAuthentication.php b/ProcessMaker/TwoFactorAuthentication.php index b4a2546278..7e9cca41f6 100644 --- a/ProcessMaker/TwoFactorAuthentication.php +++ b/ProcessMaker/TwoFactorAuthentication.php @@ -23,6 +23,8 @@ class TwoFactorAuthentication const ERROR_INVALID_TO_NUMBER = 21211; + public const PENDING_AUTHENTICATOR_SECRET = '2fa-pending-authenticator-secret'; + public function sendCode(User $user): void { // Get methods to send the code @@ -80,14 +82,19 @@ private function getUserDeliveryTotpSecret(User $user): string return $secret; } - private function createOtpInstance(User $user, bool $forGoogleAuthApp = false) + private function resolveAuthenticatorSecret(User $user): ?string { - $secret = $forGoogleAuthApp - ? $this->getUserTotpSecret($user) - : $this->getUserDeliveryTotpSecret($user); + if (!empty($user->two_factor_secret)) { + return $user->two_factor_secret; + } - // Config OTP - $otp = TOTP::createFromSecret($secret); + $pending = session()->get(self::PENDING_AUTHENTICATOR_SECRET); + + return !empty($pending) ? $pending : null; + } + + private function configureOtp(TOTP $otp, User $user, bool $forGoogleAuthApp): TOTP + { $otp->setIssuer('ProcessMaker'); $otp->setLabel($user->username); @@ -100,6 +107,18 @@ private function createOtpInstance(User $user, bool $forGoogleAuthApp = false) return $otp; } + private function createOtpInstance(User $user, bool $forGoogleAuthApp = false) + { + $secret = $forGoogleAuthApp + ? $this->resolveAuthenticatorSecret($user) + : $this->getUserDeliveryTotpSecret($user); + + // Config OTP + $otp = $this->configureOtp(TOTP::createFromSecret($secret), $user, $forGoogleAuthApp); + + return $otp; + } + private function getCodeForEmailSms(User $user): string { // Create OTP instance @@ -116,12 +135,18 @@ public function validateCode(User $user, string $code) // The code is for Google Authenticator app? if (strlen($code) === 6) { - if (in_array(self::AUTH_APP, $methods, true) && !empty($user->two_factor_secret)) { + $authSecret = $this->resolveAuthenticatorSecret($user); + if (in_array(self::AUTH_APP, $methods, true) && $authSecret !== null) { // Create OTP instance $otp = $this->createOtpInstance($user, true); // Validate code $valid = $otp->verify($code); + if ($valid && empty($user->two_factor_secret)) { + $user->two_factor_secret = $authSecret; + $user->saveQuietly(); + session()->forget(self::PENDING_AUTHENTICATOR_SECRET); + } } } elseif ( (in_array(self::EMAIL, $methods, true) || in_array(self::SMS, $methods, true)) @@ -199,7 +224,13 @@ private function sendSms(User $user, string $code) public function generateQr(User $user) { - $otp = $this->createOtpInstance($user, true); + $secret = session()->get(self::PENDING_AUTHENTICATOR_SECRET); + if (empty($secret)) { + $secret = TOTP::generate()->getSecret(); + session()->put(self::PENDING_AUTHENTICATOR_SECRET, $secret); + } + + $otp = $this->configureOtp(TOTP::createFromSecret($secret), $user, true); $g2faUrl = $otp->getProvisioningUri(); diff --git a/resources/views/auth/2fa/otp.blade.php b/resources/views/auth/2fa/otp.blade.php index 190c804b79..d3d26e42a6 100644 --- a/resources/views/auth/2fa/otp.blade.php +++ b/resources/views/auth/2fa/otp.blade.php @@ -61,7 +61,8 @@ class="form-control{{ $errors->has('code') ? ' is-invalid' : '' }}" $twoFactorMethods = auth()->user()->getValid2FAPreferences(); @endphp @if (in_array(\ProcessMaker\TwoFactorAuthentication::AUTH_APP, $twoFactorMethods, true) - && empty(auth()->user()->two_factor_secret)) + && (empty(auth()->user()->two_factor_secret) + || session(\ProcessMaker\TwoFactorAuthentication::PENDING_AUTHENTICATOR_SECRET))) {{ __('Authenticator app') }} diff --git a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php index 1672d1d6b2..051bcc82aa 100644 --- a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php +++ b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php @@ -148,6 +148,13 @@ public function testLegacyUserProvisioningWhenSecretIsMissing(): void ->get(route('2fa')) ->assertRedirect(route('2fa.auth_app_qr')); + $this->actingAs($authOnly, 'web') + ->get(route('2fa.auth_app_qr')) + ->assertOk(); + + $this->assertEmpty($authOnly->fresh()->two_factor_secret); + $this->assertNotEmpty(session(TwoFactorAuthentication::PENDING_AUTHENTICATOR_SECRET)); + config(['password-policies.2fa_method' => [TwoFactorAuthentication::EMAIL]]); $emailUser = User::factory()->create([ 'password' => Hash::make('password'), From 93531427b20fda8e5f91e6798dd593d6060b3a23 Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Tue, 10 Nov 2026 16:07:30 -0400 Subject: [PATCH 11/11] fix(FOUR-34260): show OTP after QR when authenticator secret is pending Do not redirect authenticator-only users back to the QR page when PENDING_AUTHENTICATOR_SECRET is in session, so they can enter a code after scanning. Add a feature test that GET /2fa returns the OTP view after QR enrollment. https://processmaker.atlassian.net/browse/FOUR-34260 --- .../Http/Controllers/Auth/TwoFactorAuthController.php | 4 ++++ tests/Feature/Auth/TwoFactorPredictableSecretTest.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php index bd29b95331..e0d0c8c478 100644 --- a/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php +++ b/ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php @@ -155,6 +155,10 @@ private function shouldRedirectToAuthenticatorQrEnrollment(User $user): bool return false; } + if (session()->has(TwoFactorAuthentication::PENDING_AUTHENTICATOR_SECRET)) { + return false; + } + $methods = $user->getValid2FAPreferences(); return in_array(TwoFactorAuthentication::AUTH_APP, $methods, true) diff --git a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php index 051bcc82aa..37959e69a3 100644 --- a/tests/Feature/Auth/TwoFactorPredictableSecretTest.php +++ b/tests/Feature/Auth/TwoFactorPredictableSecretTest.php @@ -155,6 +155,11 @@ public function testLegacyUserProvisioningWhenSecretIsMissing(): void $this->assertEmpty($authOnly->fresh()->two_factor_secret); $this->assertNotEmpty(session(TwoFactorAuthentication::PENDING_AUTHENTICATOR_SECRET)); + $this->actingAs($authOnly, 'web') + ->get(route('2fa')) + ->assertOk() + ->assertViewIs('auth.2fa.otp'); + config(['password-policies.2fa_method' => [TwoFactorAuthentication::EMAIL]]); $emailUser = User::factory()->create([ 'password' => Hash::make('password'),