3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\Mfa\MfaValue;
7 use BookStack\Http\Controllers\Controller;
8 use Illuminate\Http\Request;
10 class MfaController extends Controller
12 use HandlesPartialLogins;
15 * Show the view to setup MFA for the current user.
17 public function setup()
19 $userMethods = $this->currentOrLastAttemptedUser()
21 ->get(['id', 'method'])
24 $this->setPageTitle(trans('auth.mfa_setup'));
26 return view('mfa.setup', [
27 'userMethods' => $userMethods,
32 * Remove an MFA method for the current user.
36 public function remove(string $method)
38 if (in_array($method, MfaValue::allMethods())) {
39 $value = user()->mfaValues()->where('method', '=', $method)->first();
42 $this->logActivity(ActivityType::MFA_REMOVE_METHOD, $method);
46 return redirect('/mfa/setup');
50 * Show the page to start an MFA verification.
52 public function verify(Request $request)
54 $desiredMethod = $request->get('method');
55 $userMethods = $this->currentOrLastAttemptedUser()
57 ->get(['id', 'method'])
60 // Basic search for the default option for a user.
61 // (Prioritises totp over backup codes)
62 $method = $userMethods->has($desiredMethod) ? $desiredMethod : $userMethods->keys()->sort()->reverse()->first();
63 $otherMethods = $userMethods->keys()->filter(function ($userMethod) use ($method) {
64 return $method !== $userMethod;
67 return view('mfa.verify', [
68 'userMethods' => $userMethods,
70 'otherMethods' => $otherMethods,