]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Auth/MfaTotpController.php
Fixes for CodeStyle vol.2
[bookstack] / app / Http / Controllers / Auth / MfaTotpController.php
index 18f08e7093f65180a1d82bbd4708e24175d6a119..5a932d6e9ae80f00a0096a6d306d8633874688b5 100644 (file)
@@ -3,15 +3,20 @@
 namespace BookStack\Http\Controllers\Auth;
 
 use BookStack\Actions\ActivityType;
+use BookStack\Auth\Access\LoginService;
+use BookStack\Auth\Access\Mfa\MfaSession;
 use BookStack\Auth\Access\Mfa\MfaValue;
 use BookStack\Auth\Access\Mfa\TotpService;
 use BookStack\Auth\Access\Mfa\TotpValidationRule;
+use BookStack\Exceptions\NotFoundException;
 use BookStack\Http\Controllers\Controller;
 use Illuminate\Http\Request;
 use Illuminate\Validation\ValidationException;
 
 class MfaTotpController extends Controller
 {
+    use HandlesPartialLogins;
+
     protected const SETUP_SECRET_SESSION_KEY = 'mfa-setup-totp-secret';
 
     /**
@@ -31,14 +36,16 @@ class MfaTotpController extends Controller
 
         return view('mfa.totp-generate', [
             'secret' => $totpSecret,
-            'svg' => $svg,
+            'svg'    => $svg,
         ]);
     }
 
     /**
      * Confirm the setup of TOTP and save the auth method secret
      * against the current user.
+     *
      * @throws ValidationException
+     * @throws NotFoundException
      */
     public function confirm(Request $request)
     {
@@ -48,13 +55,43 @@ class MfaTotpController extends Controller
                 'required',
                 'max:12', 'min:4',
                 new TotpValidationRule($totpSecret),
-            ]
+            ],
         ]);
 
-        MfaValue::upsertWithValue(user(), MfaValue::METHOD_TOTP, $totpSecret);
+        MfaValue::upsertWithValue($this->currentOrLastAttemptedUser(), MfaValue::METHOD_TOTP, $totpSecret);
         session()->remove(static::SETUP_SECRET_SESSION_KEY);
         $this->logActivity(ActivityType::MFA_SETUP_METHOD, 'totp');
 
+        if (!auth()->check()) {
+            $this->showSuccessNotification(trans('auth.mfa_setup_login_notification'));
+
+            return redirect('/login');
+        }
+
         return redirect('/mfa/setup');
     }
+
+    /**
+     * Verify the MFA method submission on check.
+     *
+     * @throws NotFoundException
+     */
+    public function verify(Request $request, LoginService $loginService, MfaSession $mfaSession)
+    {
+        $user = $this->currentOrLastAttemptedUser();
+        $totpSecret = MfaValue::getValueForUser($user, MfaValue::METHOD_TOTP);
+
+        $this->validate($request, [
+            'code' => [
+                'required',
+                'max:12', 'min:4',
+                new TotpValidationRule($totpSecret),
+            ],
+        ]);
+
+        $mfaSession->markVerifiedForUser($user);
+        $loginService->reattemptLoginFor($user);
+
+        return redirect()->intended();
+    }
 }