3 namespace BookStack\Auth\Access\Mfa;
5 use BookStack\Auth\User;
10 * Check if MFA is required for the given user.
12 public function isRequiredForUser(User $user): bool
14 // TODO - Test both these cases
15 return $user->mfaValues()->exists() || $this->userRoleEnforcesMfa($user);
19 * Check if the given user is pending MFA setup.
20 * (MFA required but not yet configured).
22 public function isPendingMfaSetup(User $user): bool
24 return $this->isRequiredForUser($user) && !$user->mfaValues()->exists();
28 * Check if a role of the given user enforces MFA.
30 protected function userRoleEnforcesMfa(User $user): bool
33 ->where('mfa_enforced', '=', true)
38 * Check if the current MFA session has already been verified for the given user.
40 public function isVerifiedForUser(User $user): bool
42 return session()->get($this->getMfaVerifiedSessionKey($user)) === 'true';
46 * Mark the current session as MFA-verified.
48 public function markVerifiedForUser(User $user): void
50 session()->put($this->getMfaVerifiedSessionKey($user), 'true');
54 * Get the session key in which the MFA verification status is stored.
56 protected function getMfaVerifiedSessionKey(User $user): string
58 return 'mfa-verification-passed:' . $user->id;