]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/MfaController.php
Bump composer/composer from 2.1.8 to 2.1.9
[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         return view('mfa.setup', [
25             'userMethods' => $userMethods,
26         ]);
27     }
28
29     /**
30      * Remove an MFA method for the current user.
31      *
32      * @throws \Exception
33      */
34     public function remove(string $method)
35     {
36         if (in_array($method, MfaValue::allMethods())) {
37             $value = user()->mfaValues()->where('method', '=', $method)->first();
38             if ($value) {
39                 $value->delete();
40                 $this->logActivity(ActivityType::MFA_REMOVE_METHOD, $method);
41             }
42         }
43
44         return redirect('/mfa/setup');
45     }
46
47     /**
48      * Show the page to start an MFA verification.
49      */
50     public function verify(Request $request)
51     {
52         $desiredMethod = $request->get('method');
53         $userMethods = $this->currentOrLastAttemptedUser()
54             ->mfaValues()
55             ->get(['id', 'method'])
56             ->groupBy('method');
57
58         // Basic search for the default option for a user.
59         // (Prioritises totp over backup codes)
60         $method = $userMethods->has($desiredMethod) ? $desiredMethod : $userMethods->keys()->sort()->reverse()->first();
61         $otherMethods = $userMethods->keys()->filter(function ($userMethod) use ($method) {
62             return $method !== $userMethod;
63         })->all();
64
65         return view('mfa.verify', [
66             'userMethods'  => $userMethods,
67             'method'       => $method,
68             'otherMethods' => $otherMethods,
69         ]);
70     }
71 }