]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Guards/LdapSessionGuard.php
Move logFailedAccess into Activity
[bookstack] / app / Auth / Access / Guards / LdapSessionGuard.php
1 <?php
2
3 namespace BookStack\Auth\Access\Guards;
4
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;
17
18 class LdapSessionGuard extends ExternalBaseSessionGuard
19 {
20
21     protected $ldapService;
22
23     /**
24      * LdapSessionGuard constructor.
25      */
26     public function __construct($name,
27         UserProvider $provider,
28         Session $session,
29         LdapService $ldapService,
30         RegistrationService $registrationService
31     )
32     {
33         $this->ldapService = $ldapService;
34         parent::__construct($name, $provider, $session, $registrationService);
35     }
36
37     /**
38      * Validate a user's credentials.
39      *
40      * @param array $credentials
41      * @return bool
42      * @throws LdapException
43      */
44     public function validate(array $credentials = [])
45     {
46         $userDetails = $this->ldapService->getUserDetails($credentials['username']);
47         $this->lastAttempted = $this->provider->retrieveByCredentials([
48             'external_auth_id' => $userDetails['uid']
49         ]);
50
51         return $this->ldapService->validateUserCredentials($userDetails, $credentials['username'], $credentials['password']);
52     }
53
54     /**
55      * Attempt to authenticate a user using the given credentials.
56      *
57      * @param array $credentials
58      * @param bool $remember
59      * @return bool
60      * @throws LoginAttemptEmailNeededException
61      * @throws LoginAttemptException
62      * @throws LdapException
63      * @throws UserRegistrationException
64      */
65     public function attempt(array $credentials = [], $remember = false)
66     {
67         $username = $credentials['username'];
68         $userDetails = $this->ldapService->getUserDetails($username);
69         $this->lastAttempted = $user = $this->provider->retrieveByCredentials([
70             'external_auth_id' => $userDetails['uid']
71         ]);
72
73         if (!$this->ldapService->validateUserCredentials($userDetails, $username, $credentials['password'])) {
74             return false;
75         }
76
77         if (is_null($user)) {
78             $user = $this->createNewFromLdapAndCreds($userDetails, $credentials);
79         }
80
81         // Sync LDAP groups if required
82         if ($this->ldapService->shouldSyncGroups()) {
83             $this->ldapService->syncGroups($user, $username);
84         }
85
86         $this->login($user, $remember);
87         return true;
88     }
89
90     /**
91      * Create a new user from the given ldap credentials and login credentials
92      * @throws LoginAttemptEmailNeededException
93      * @throws LoginAttemptException
94      * @throws UserRegistrationException
95      */
96     protected function createNewFromLdapAndCreds(array $ldapUserDetails, array $credentials): User
97     {
98         $email = trim($ldapUserDetails['email'] ?: ($credentials['email'] ?? ''));
99
100         if (empty($email)) {
101             throw new LoginAttemptEmailNeededException();
102         }
103
104         $details = [
105             'name' => $ldapUserDetails['name'],
106             'email' => $ldapUserDetails['email'] ?: $credentials['email'],
107             'external_auth_id' => $ldapUserDetails['uid'],
108             'password' => Str::random(32),
109         ];
110
111         return $this->registrationService->registerUser($details, null, false);
112     }
113
114 }