]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Mfa/MfaSession.php
dabd568f7e80b1941289f178c997b5909dae3bc4
[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 a role of the given user enforces MFA.
20      */
21     protected function userRoleEnforcesMfa(User $user): bool
22     {
23         return $user->roles()
24             ->where('mfa_enforced', '=', true)
25             ->exists();
26     }
27
28     /**
29      * Check if the current MFA session has already been verified for the given user.
30      */
31     public function isVerifiedForUser(User $user): bool
32     {
33         return session()->get($this->getMfaVerifiedSessionKey($user)) === 'true';
34     }
35
36     /**
37      * Mark the current session as MFA-verified.
38      */
39     public function markVerifiedForUser(User $user): void
40     {
41         session()->put($this->getMfaVerifiedSessionKey($user), 'true');
42     }
43
44     /**
45      * Get the session key in which the MFA verification status is stored.
46      */
47     protected function getMfaVerifiedSessionKey(User $user): string
48     {
49         return 'mfa-verification-passed:' . $user->id;
50     }
51
52 }