]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/MfaController.php
Added force option for update-url command
[bookstack] / app / Http / Controllers / Auth / MfaController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\Mfa\MfaValue;
7 use BookStack\Http\Controllers\Controller;
8 use Illuminate\Http\Request;
9
10 class MfaController extends Controller
11 {
12     use HandlesPartialLogins;
13
14     /**
15      * Show the view to setup MFA for the current user.
16      */
17     public function setup()
18     {
19         $userMethods = $this->currentOrLastAttemptedUser()
20             ->mfaValues()
21             ->get(['id', 'method'])
22             ->groupBy('method');
23
24         $this->setPageTitle(trans('auth.mfa_setup'));
25
26         return view('mfa.setup', [
27             'userMethods' => $userMethods,
28         ]);
29     }
30
31     /**
32      * Remove an MFA method for the current user.
33      *
34      * @throws \Exception
35      */
36     public function remove(string $method)
37     {
38         if (in_array($method, MfaValue::allMethods())) {
39             $value = user()->mfaValues()->where('method', '=', $method)->first();
40             if ($value) {
41                 $value->delete();
42                 $this->logActivity(ActivityType::MFA_REMOVE_METHOD, $method);
43             }
44         }
45
46         return redirect('/mfa/setup');
47     }
48
49     /**
50      * Show the page to start an MFA verification.
51      */
52     public function verify(Request $request)
53     {
54         $desiredMethod = $request->get('method');
55         $userMethods = $this->currentOrLastAttemptedUser()
56             ->mfaValues()
57             ->get(['id', 'method'])
58             ->groupBy('method');
59
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;
65         })->all();
66
67         return view('mfa.verify', [
68             'userMethods'  => $userMethods,
69             'method'       => $method,
70             'otherMethods' => $otherMethods,
71         ]);
72     }
73 }