]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/MfaController.php
Added Backup code verification logic
[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         return view('mfa.setup', [
24             'userMethods' => $userMethods,
25         ]);
26     }
27
28     /**
29      * Remove an MFA method for the current user.
30      * @throws \Exception
31      */
32     public function remove(string $method)
33     {
34         if (in_array($method, MfaValue::allMethods())) {
35             $value = user()->mfaValues()->where('method', '=', $method)->first();
36             if ($value) {
37                 $value->delete();
38                 $this->logActivity(ActivityType::MFA_REMOVE_METHOD, $method);
39             }
40         }
41
42         return redirect('/mfa/setup');
43     }
44
45     /**
46      * Show the page to start an MFA verification.
47      */
48     public function verify(Request $request)
49     {
50         $desiredMethod = $request->get('method');
51         $userMethods = $this->currentOrLastAttemptedUser()
52             ->mfaValues()
53             ->get(['id', 'method'])
54             ->groupBy('method');
55
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;
61         })->all();
62
63         return view('mfa.verify', [
64             'userMethods' => $userMethods,
65             'method' => $method,
66             'otherMethods' => $otherMethods,
67         ]);
68     }
69 }