]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/security/LoginManager.php
ldap authentication, fixes shaarli/Shaarli#1343
[github/shaarli/Shaarli.git] / application / security / LoginManager.php
1 <?php
2 namespace Shaarli\Security;
3
4 use Exception;
5 use Shaarli\Config\ConfigManager;
6
7 /**
8 * User login management
9 */
10 class LoginManager
11 {
12 /** @var string Name of the cookie set after logging in **/
13 public static $STAY_SIGNED_IN_COOKIE = 'shaarli_staySignedIn';
14
15 /** @var array A reference to the $_GLOBALS array */
16 protected $globals = [];
17
18 /** @var ConfigManager Configuration Manager instance **/
19 protected $configManager = null;
20
21 /** @var SessionManager Session Manager instance **/
22 protected $sessionManager = null;
23
24 /** @var BanManager Ban Manager instance **/
25 protected $banManager;
26
27 /** @var bool Whether the user is logged in **/
28 protected $isLoggedIn = false;
29
30 /** @var bool Whether the Shaarli instance is open to public edition **/
31 protected $openShaarli = false;
32
33 /** @var string User sign-in token depending on remote IP and credentials */
34 protected $staySignedInToken = '';
35
36 /**
37 * Constructor
38 *
39 * @param ConfigManager $configManager Configuration Manager instance
40 * @param SessionManager $sessionManager SessionManager instance
41 */
42 public function __construct($configManager, $sessionManager)
43 {
44 $this->configManager = $configManager;
45 $this->sessionManager = $sessionManager;
46 $this->banManager = new BanManager(
47 $this->configManager->get('security.trusted_proxies', []),
48 $this->configManager->get('security.ban_after'),
49 $this->configManager->get('security.ban_duration'),
50 $this->configManager->get('resource.ban_file', 'data/ipbans.php'),
51 $this->configManager->get('resource.log')
52 );
53
54 if ($this->configManager->get('security.open_shaarli') === true) {
55 $this->openShaarli = true;
56 }
57 }
58
59 /**
60 * Generate a token depending on deployment salt, user password and client IP
61 *
62 * @param string $clientIpAddress The remote client IP address
63 */
64 public function generateStaySignedInToken($clientIpAddress)
65 {
66 if ($this->configManager->get('security.session_protection_disabled') === true) {
67 $clientIpAddress = '';
68 }
69 $this->staySignedInToken = sha1(
70 $this->configManager->get('credentials.hash')
71 . $clientIpAddress
72 . $this->configManager->get('credentials.salt')
73 );
74 }
75
76 /**
77 * Return the user's client stay-signed-in token
78 *
79 * @return string User's client stay-signed-in token
80 */
81 public function getStaySignedInToken()
82 {
83 return $this->staySignedInToken;
84 }
85
86 /**
87 * Check user session state and validity (expiration)
88 *
89 * @param array $cookie The $_COOKIE array
90 * @param string $clientIpId Client IP address identifier
91 */
92 public function checkLoginState($cookie, $clientIpId)
93 {
94 if (! $this->configManager->exists('credentials.login')) {
95 // Shaarli is not configured yet
96 $this->isLoggedIn = false;
97 return;
98 }
99
100 if (isset($cookie[self::$STAY_SIGNED_IN_COOKIE])
101 && $cookie[self::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken
102 ) {
103 // The user client has a valid stay-signed-in cookie
104 // Session information is updated with the current client information
105 $this->sessionManager->storeLoginInfo($clientIpId);
106 } elseif ($this->sessionManager->hasSessionExpired()
107 || $this->sessionManager->hasClientIpChanged($clientIpId)
108 ) {
109 $this->sessionManager->logout();
110 $this->isLoggedIn = false;
111 return;
112 }
113
114 $this->isLoggedIn = true;
115 $this->sessionManager->extendSession();
116 }
117
118 /**
119 * Return whether the user is currently logged in
120 *
121 * @return true when the user is logged in, false otherwise
122 */
123 public function isLoggedIn()
124 {
125 if ($this->openShaarli) {
126 return true;
127 }
128 return $this->isLoggedIn;
129 }
130
131 /**
132 * Check user credentials are valid
133 *
134 * @param string $remoteIp Remote client IP address
135 * @param string $clientIpId Client IP address identifier
136 * @param string $login Username
137 * @param string $password Password
138 *
139 * @return bool true if the provided credentials are valid, false otherwise
140 */
141 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
142 {
143 // Check login matches config
144 if ($login != $this->configManager->get('credentials.login')) {
145 return false;
146 }
147
148 // Check credentials
149 try {
150 if (($this->configManager->get('ldap.host') != "" && $this->checkCredentialsFromLdap($login, $password))
151 || ($this->configManager->get('ldap.host') == "" && $this->checkCredentialsFromLocalConfig($login, $password))) {
152 $this->sessionManager->storeLoginInfo($clientIpId);
153 logm(
154 $this->configManager->get('resource.log'),
155 $remoteIp,
156 'Login successful'
157 );
158 return true;
159 }
160 }
161 catch(Exception $exception) {
162 logm(
163 $this->configManager->get('resource.log'),
164 $remoteIp,
165 'Exception while checking credentials: ' . $exception
166 );
167 }
168
169 logm(
170 $this->configManager->get('resource.log'),
171 $remoteIp,
172 'Login failed for user ' . $login
173 );
174 return false;
175 }
176
177
178 /**
179 * Check user credentials from local config
180 *
181 * @param string $login Username
182 * @param string $password Password
183 *
184 * @return bool true if the provided credentials are valid, false otherwise
185 */
186 public function checkCredentialsFromLocalConfig($login, $password) {
187 $hash = sha1($password . $login . $this->configManager->get('credentials.salt'));
188
189 return $login == $this->configManager->get('credentials.login')
190 && $hash == $this->configManager->get('credentials.hash');
191 }
192
193 /**
194 * Check user credentials are valid through LDAP bind
195 *
196 * @param string $remoteIp Remote client IP address
197 * @param string $clientIpId Client IP address identifier
198 * @param string $login Username
199 * @param string $password Password
200 *
201 * @return bool true if the provided credentials are valid, false otherwise
202 */
203 public function checkCredentialsFromLdap($login, $password, $connect = null, $bind = null)
204 {
205 $connect = $connect ?? function($host) { return ldap_connect($host); };
206 $bind = $bind ?? function($handle, $dn, $password) { return ldap_bind($handle, $dn, $password); };
207 return $bind($connect($this->configManager->get('ldap.host')), sprintf($this->configManager->get('ldap.dn'), $login), $password);
208 }
209
210 /**
211 * Handle a failed login and ban the IP after too many failed attempts
212 *
213 * @param array $server The $_SERVER array
214 */
215 public function handleFailedLogin($server)
216 {
217 $this->banManager->handleFailedAttempt($server);
218 }
219
220 /**
221 * Handle a successful login
222 *
223 * @param array $server The $_SERVER array
224 */
225 public function handleSuccessfulLogin($server)
226 {
227 $this->banManager->clearFailures($server);
228 }
229
230 /**
231 * Check if the user can login from this IP
232 *
233 * @param array $server The $_SERVER array
234 *
235 * @return bool true if the user is allowed to login
236 */
237 public function canLogin($server)
238 {
239 return ! $this->banManager->isBanned($server);
240 }
241 }