]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Mfa/MfaSession.php
72163b58ec4c2f63abce1fe3686f6ad245c41bed
[bookstack] / app / Auth / Access / Mfa / MfaSession.php
1 <?php
2
3 namespace BookStack\Auth\Access\Mfa;
4
5 use BookStack\Auth\User;
6
7 class MfaSession
8 {
9     /**
10      * Check if MFA is required for the given user.
11      */
12     public function isRequiredForUser(User $user): bool
13     {
14         // TODO - Test both these cases
15         return $user->mfaValues()->exists() || $this->userRoleEnforcesMfa($user);
16     }
17
18     /**
19      * Check if the given user is pending MFA setup.
20      * (MFA required but not yet configured).
21      */
22     public function isPendingMfaSetup(User $user): bool
23     {
24         return $this->isRequiredForUser($user) && !$user->mfaValues()->exists();
25     }
26
27     /**
28      * Check if a role of the given user enforces MFA.
29      */
30     protected function userRoleEnforcesMfa(User $user): bool
31     {
32         return $user->roles()
33             ->where('mfa_enforced', '=', true)
34             ->exists();
35     }
36
37     /**
38      * Check if the current MFA session has already been verified for the given user.
39      */
40     public function isVerifiedForUser(User $user): bool
41     {
42         return session()->get($this->getMfaVerifiedSessionKey($user)) === 'true';
43     }
44
45     /**
46      * Mark the current session as MFA-verified.
47      */
48     public function markVerifiedForUser(User $user): void
49     {
50         session()->put($this->getMfaVerifiedSessionKey($user), 'true');
51     }
52
53     /**
54      * Get the session key in which the MFA verification status is stored.
55      */
56     protected function getMfaVerifiedSessionKey(User $user): string
57     {
58         return 'mfa-verification-passed:' . $user->id;
59     }
60 }