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;
20 protected $emailConfirmationService;
22 public function __construct(MfaSession $mfaSession, EmailConfirmationService $emailConfirmationService)
24 $this->mfaSession = $mfaSession;
25 $this->emailConfirmationService = $emailConfirmationService;
29 * Log the given user into the system.
30 * Will start a login of the given user but will prevent if there's
31 * a reason to (MFA or Unconfirmed Email).
32 * Returns a boolean to indicate the current login result.
33 * @throws StoppedAuthenticationException
35 public function login(User $user, string $method, bool $remember = false): void
37 if ($this->awaitingEmailConfirmation($user) || $this->needsMfaVerification($user)) {
38 $this->setLastLoginAttemptedForUser($user, $method, $remember);
39 throw new StoppedAuthenticationException($user, $this);
42 $this->clearLastLoginAttempted();
43 auth()->login($user, $remember);
44 Activity::add(ActivityType::AUTH_LOGIN, "{$method}; {$user->logDescriptor()}");
45 Theme::dispatch(ThemeEvents::AUTH_LOGIN, $method, $user);
47 // Authenticate on all session guards if a likely admin
48 if ($user->can('users-manage') && $user->can('user-roles-manage')) {
49 $guards = ['standard', 'ldap', 'saml2'];
50 foreach ($guards as $guard) {
51 auth($guard)->login($user);
57 * Reattempt a system login after a previous stopped attempt.
60 public function reattemptLoginFor(User $user)
62 if ($user->id !== ($this->getLastLoginAttemptUser()->id ?? null)) {
63 throw new Exception('Login reattempt user does align with current session state');
66 $lastLoginDetails = $this->getLastLoginAttemptDetails();
67 $this->login($user, $lastLoginDetails['method'], $lastLoginDetails['remember'] ?? false);
71 * Get the last user that was attempted to be logged in.
72 * Only exists if the last login attempt had correct credentials
73 * but had been prevented by a secondary factor.
75 public function getLastLoginAttemptUser(): ?User
77 $id = $this->getLastLoginAttemptDetails()['user_id'];
78 return User::query()->where('id', '=', $id)->first();
82 * Get the details of the last login attempt.
83 * Checks upon a ttl of about 1 hour since that last attempted login.
84 * @return array{user_id: ?string, method: ?string, remember: bool}
86 protected function getLastLoginAttemptDetails(): array
88 $value = session()->get(self::LAST_LOGIN_ATTEMPTED_SESSION_KEY);
90 return ['user_id' => null, 'method' => null];
93 [$id, $method, $remember, $time] = explode(':', $value);
94 $hourAgo = time() - (60*60);
95 if ($time < $hourAgo) {
96 $this->clearLastLoginAttempted();
97 return ['user_id' => null, 'method' => null];
100 return ['user_id' => $id, 'method' => $method, 'remember' => boolval($remember)];
104 * Set the last login attempted user.
105 * Must be only used when credentials are correct and a login could be
106 * achieved but a secondary factor has stopped the login.
108 protected function setLastLoginAttemptedForUser(User $user, string $method, bool $remember)
111 self::LAST_LOGIN_ATTEMPTED_SESSION_KEY,
112 implode(':', [$user->id, $method, $remember, time()])
117 * Clear the last login attempted session value.
119 protected function clearLastLoginAttempted(): void
121 session()->remove(self::LAST_LOGIN_ATTEMPTED_SESSION_KEY);
125 * Check if MFA verification is needed.
127 public function needsMfaVerification(User $user): bool
129 return !$this->mfaSession->isVerifiedForUser($user) && $this->mfaSession->isRequiredForUser($user);
133 * Check if the given user is awaiting email confirmation.
135 public function awaitingEmailConfirmation(User $user): bool
137 return $this->emailConfirmationService->confirmationRequired() && !$user->email_confirmed;
141 * Attempt the login of a user using the given credentials.
142 * Meant to mirror Laravel's default guard 'attempt' method
143 * but in a manner that always routes through our login system.
144 * May interrupt the flow if extra authentication requirements are imposed.
146 * @throws StoppedAuthenticationException
148 public function attempt(array $credentials, string $method, bool $remember = false): bool
150 $result = auth()->attempt($credentials, $remember);
152 $user = auth()->user();
154 $this->login($user, $method, $remember);