3 namespace BookStack\Auth\Access\Guards;
5 use BookStack\Auth\Access\LdapService;
6 use BookStack\Auth\Access\RegistrationService;
7 use BookStack\Auth\User;
8 use BookStack\Auth\UserRepo;
9 use BookStack\Exceptions\LdapException;
10 use BookStack\Exceptions\LoginAttemptException;
11 use BookStack\Exceptions\LoginAttemptEmailNeededException;
12 use BookStack\Exceptions\UserRegistrationException;
13 use Illuminate\Contracts\Auth\UserProvider;
14 use Illuminate\Contracts\Session\Session;
15 use Illuminate\Support\Facades\Hash;
16 use Illuminate\Support\Str;
18 class LdapSessionGuard extends ExternalBaseSessionGuard
21 protected $ldapService;
24 * LdapSessionGuard constructor.
26 public function __construct($name,
27 UserProvider $provider,
29 LdapService $ldapService,
30 RegistrationService $registrationService
33 $this->ldapService = $ldapService;
34 parent::__construct($name, $provider, $session, $registrationService);
38 * Validate a user's credentials.
40 * @param array $credentials
42 * @throws LdapException
44 public function validate(array $credentials = [])
46 $userDetails = $this->ldapService->getUserDetails($credentials['username']);
48 if (isset($userDetails['uid'])) {
49 $this->lastAttempted = $this->provider->retrieveByCredentials([
50 'external_auth_id' => $userDetails['uid']
54 return $this->ldapService->validateUserCredentials($userDetails, $credentials['password']);
58 * Attempt to authenticate a user using the given credentials.
60 * @param array $credentials
61 * @param bool $remember
63 * @throws LoginAttemptEmailNeededException
64 * @throws LoginAttemptException
65 * @throws LdapException
66 * @throws UserRegistrationException
68 public function attempt(array $credentials = [], $remember = false)
70 $username = $credentials['username'];
71 $userDetails = $this->ldapService->getUserDetails($username);
74 if (isset($userDetails['uid'])) {
75 $this->lastAttempted = $user = $this->provider->retrieveByCredentials([
76 'external_auth_id' => $userDetails['uid']
80 if (!$this->ldapService->validateUserCredentials($userDetails, $credentials['password'])) {
85 $user = $this->createNewFromLdapAndCreds($userDetails, $credentials);
88 // Sync LDAP groups if required
89 if ($this->ldapService->shouldSyncGroups()) {
90 $this->ldapService->syncGroups($user, $username);
93 $this->login($user, $remember);
98 * Create a new user from the given ldap credentials and login credentials
99 * @throws LoginAttemptEmailNeededException
100 * @throws LoginAttemptException
101 * @throws UserRegistrationException
103 protected function createNewFromLdapAndCreds(array $ldapUserDetails, array $credentials): User
105 $email = trim($ldapUserDetails['email'] ?: ($credentials['email'] ?? ''));
108 throw new LoginAttemptEmailNeededException();
112 'name' => $ldapUserDetails['name'],
113 'email' => $ldapUserDetails['email'] ?: $credentials['email'],
114 'external_auth_id' => $ldapUserDetails['uid'],
115 'password' => Str::random(32),
118 return $this->registrationService->registerUser($details, null, false);