3 namespace BookStack\Auth\Access;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\Mfa\MfaSession;
7 use BookStack\Auth\User;
8 use BookStack\Exceptions\StoppedAuthenticationException;
9 use BookStack\Facades\Activity;
10 use BookStack\Facades\Theme;
11 use BookStack\Theming\ThemeEvents;
17 protected const LAST_LOGIN_ATTEMPTED_SESSION_KEY = 'auth-login-last-attempted';
19 protected $mfaSession;
21 public function __construct(MfaSession $mfaSession)
23 $this->mfaSession = $mfaSession;
27 * Log the given user into the system.
28 * Will start a login of the given user but will prevent if there's
29 * a reason to (MFA or Unconfirmed Email).
30 * Returns a boolean to indicate the current login result.
31 * @throws StoppedAuthenticationException
33 public function login(User $user, string $method): void
35 if ($this->awaitingEmailConfirmation($user) || $this->needsMfaVerification($user)) {
36 $this->setLastLoginAttemptedForUser($user);
37 throw new StoppedAuthenticationException($user, $this);
38 // TODO - Does 'remember' still work? Probably not right now.
40 // TODO - Need to clear MFA sessions out upon logout
42 // Old MFA middleware todos:
44 // TODO - Need to redirect to setup if not configured AND ONLY IF NO OPTIONS CONFIGURED
45 // Might need to change up such routes to start with /configure/ for such identification.
46 // (Can't allow access to those if already configured)
47 // Or, More likely, Need to add defence to those to prevent access unless
48 // logged in or during partial auth.
50 // TODO - Handle email confirmation handling
51 // Left BookStack\Http\Middleware\Authenticate@emailConfirmationErrorResponse in which needs
52 // be removed as an example of old behaviour.
55 $this->clearLastLoginAttempted();
57 Activity::add(ActivityType::AUTH_LOGIN, "{$method}; {$user->logDescriptor()}");
58 Theme::dispatch(ThemeEvents::AUTH_LOGIN, $method, $user);
60 // Authenticate on all session guards if a likely admin
61 if ($user->can('users-manage') && $user->can('user-roles-manage')) {
62 $guards = ['standard', 'ldap', 'saml2'];
63 foreach ($guards as $guard) {
64 auth($guard)->login($user);
70 * Reattempt a system login after a previous stopped attempt.
73 public function reattemptLoginFor(User $user, string $method)
75 if ($user->id !== ($this->getLastLoginAttemptUser()->id ?? null)) {
76 throw new Exception('Login reattempt user does align with current session state');
79 $this->login($user, $method);
83 * Get the last user that was attempted to be logged in.
84 * Only exists if the last login attempt had correct credentials
85 * but had been prevented by a secondary factor.
87 public function getLastLoginAttemptUser(): ?User
89 $id = session()->get(self::LAST_LOGIN_ATTEMPTED_SESSION_KEY);
94 return User::query()->where('id', '=', $id)->first();
98 * Set the last login attempted user.
99 * Must be only used when credentials are correct and a login could be
100 * achieved but a secondary factor has stopped the login.
102 protected function setLastLoginAttemptedForUser(User $user)
104 session()->put(self::LAST_LOGIN_ATTEMPTED_SESSION_KEY, $user->id);
108 * Clear the last login attempted session value.
110 protected function clearLastLoginAttempted(): void
112 session()->remove(self::LAST_LOGIN_ATTEMPTED_SESSION_KEY);
116 * Check if MFA verification is needed.
118 public function needsMfaVerification(User $user): bool
120 return !$this->mfaSession->isVerifiedForUser($user) && $this->mfaSession->isRequiredForUser($user);
124 * Check if the given user is awaiting email confirmation.
126 public function awaitingEmailConfirmation(User $user): bool
128 $requireConfirmation = (setting('registration-confirmation') || setting('registration-restrict'));
129 return $requireConfirmation && !$user->email_confirmed;
133 * Attempt the login of a user using the given credentials.
134 * Meant to mirror Laravel's default guard 'attempt' method
135 * but in a manner that always routes through our login system.
136 * May interrupt the flow if extra authentication requirements are imposed.
138 * @throws StoppedAuthenticationException
140 public function attempt(array $credentials, string $method, bool $remember = false): bool
142 $result = auth()->attempt($credentials, $remember);
144 $user = auth()->user();
146 $this->login($user, $method);