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'])
23 return view('mfa.setup', [
24 'userMethods' => $userMethods,
29 * Remove an MFA method for the current user.
32 public function remove(string $method)
34 if (in_array($method, MfaValue::allMethods())) {
35 $value = user()->mfaValues()->where('method', '=', $method)->first();
38 $this->logActivity(ActivityType::MFA_REMOVE_METHOD, $method);
42 return redirect('/mfa/setup');
46 * Show the page to start an MFA verification.
48 public function verify(Request $request)
50 $desiredMethod = $request->get('method');
51 $userMethods = $this->currentOrLastAttemptedUser()
53 ->get(['id', 'method'])
56 // Basic search for the default option for a user.
57 // (Prioritises totp over backup codes)
58 $method = $userMethods->has($desiredMethod) ? $desiredMethod : $userMethods->keys()->sort()->reverse()->first();
59 $otherMethods = $userMethods->keys()->filter(function($userMethod) use ($method) {
60 return $method !== $userMethod;
63 return view('mfa.verify', [
64 'userMethods' => $userMethods,
66 'otherMethods' => $otherMethods,