]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/LoginManager.php
Refactor user credential validation at login time
[github/shaarli/Shaarli.git] / application / LoginManager.php
CommitLineData
44acf706
V
1<?php
2namespace Shaarli;
3
4/**
5 * User login management
6 */
7class LoginManager
8{
9 protected $globals = [];
10 protected $configManager = null;
63ea23c2 11 protected $sessionManager = null;
44acf706 12 protected $banFile = '';
63ea23c2
V
13 protected $isLoggedIn = false;
14 protected $openShaarli = false;
44acf706
V
15
16 /**
17 * Constructor
18 *
63ea23c2
V
19 * @param array $globals The $GLOBALS array (reference)
20 * @param ConfigManager $configManager Configuration Manager instance
21 * @param SessionManager $sessionManager SessionManager instance
44acf706 22 */
63ea23c2 23 public function __construct(& $globals, $configManager, $sessionManager)
44acf706
V
24 {
25 $this->globals = &$globals;
26 $this->configManager = $configManager;
63ea23c2 27 $this->sessionManager = $sessionManager;
44acf706
V
28 $this->banFile = $this->configManager->get('resource.ban_file', 'data/ipbans.php');
29 $this->readBanFile();
63ea23c2
V
30 if ($this->configManager->get('security.open_shaarli')) {
31 $this->openShaarli = true;
32 }
33 }
34
35 /**
36 * Check user session state and validity (expiration)
37 *
38 * @param array $server The $_SERVER array
39 * @param array $session The $_SESSION array (reference)
40 * @param array $cookie The $_COOKIE array
41 * @param string $webPath Path on the server in which the cookie will be available on
42 * @param string $token Session token
43 *
44 * @return bool true if the user session is valid, false otherwise
45 */
46 public function checkLoginState($server, & $session, $cookie, $webPath, $token)
47 {
48 if (! $this->configManager->exists('credentials.login')) {
49 // Shaarli is not configured yet
50 $this->isLoggedIn = false;
51 return;
52 }
53
54 if (isset($cookie[SessionManager::$LOGGED_IN_COOKIE])
55 && $cookie[SessionManager::$LOGGED_IN_COOKIE] === $token
56 ) {
57 $this->sessionManager->storeLoginInfo($server);
58 $this->isLoggedIn = true;
59 }
60
61 // Logout when:
62 // - the session does not exist on the server side
63 // - the session has expired
64 // - the client IP address has changed
65 if (empty($session['uid'])
66 || ($this->configManager->get('security.session_protection_disabled') === false
67 && $session['ip'] != client_ip_id($server))
68 || time() >= $session['expires_on']
69 ) {
70 $this->sessionManager->logout($webPath);
71 $this->isLoggedIn = false;
72 return;
73 }
74
75 // Extend session validity
76 if (! empty($session['longlastingsession'])) {
77 // "Stay signed in" is enabled
78 $session['expires_on'] = time() + $session['longlastingsession'];
79 } else {
80 $session['expires_on'] = time() + SessionManager::$INACTIVITY_TIMEOUT;
81 }
82 }
83
84 /**
85 * Return whether the user is currently logged in
86 *
87 * @return true when the user is logged in, false otherwise
88 */
89 public function isLoggedIn()
90 {
91 if ($this->openShaarli) {
92 return true;
93 }
94 return $this->isLoggedIn;
95 }
96
97 /**
98 * Check user credentials are valid
99 *
100 * @param array $server The $_SERVER array
101 * @param string $login Username
102 * @param string $password Password
103 *
104 * @return bool true if the provided credentials are valid, false otherwise
105 */
106 public function checkCredentials($server, $login, $password)
107 {
108 $hash = sha1($password . $login . $this->configManager->get('credentials.salt'));
109
110 if ($login != $this->configManager->get('credentials.login')
111 || $hash != $this->configManager->get('credentials.hash')
112 ) {
113 logm(
114 $this->configManager->get('resource.log'),
115 $server['REMOTE_ADDR'],
116 'Login failed for user ' . $login
117 );
118 return false;
119 }
120
121 $this->sessionManager->storeLoginInfo($server);
122 logm(
123 $this->configManager->get('resource.log'),
124 $server['REMOTE_ADDR'],
125 'Login successful'
126 );
127 return true;
44acf706
V
128 }
129
130 /**
131 * Read a file containing banned IPs
132 */
133 protected function readBanFile()
134 {
135 if (! file_exists($this->banFile)) {
136 return;
137 }
138 include $this->banFile;
139 }
140
141 /**
142 * Write the banned IPs to a file
143 */
144 protected function writeBanFile()
145 {
146 if (! array_key_exists('IPBANS', $this->globals)) {
147 return;
148 }
149 file_put_contents(
150 $this->banFile,
151 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals['IPBANS'], true) . ";\n?>"
152 );
153 }
154
155 /**
156 * Handle a failed login and ban the IP after too many failed attempts
157 *
158 * @param array $server The $_SERVER array
159 */
160 public function handleFailedLogin($server)
161 {
162 $ip = $server['REMOTE_ADDR'];
163 $trusted = $this->configManager->get('security.trusted_proxies', []);
164
165 if (in_array($ip, $trusted)) {
166 $ip = getIpAddressFromProxy($server, $trusted);
167 if (! $ip) {
168 // the IP is behind a trusted forward proxy, but is not forwarded
169 // in the HTTP headers, so we do nothing
170 return;
171 }
172 }
173
174 // increment the fail count for this IP
175 if (isset($this->globals['IPBANS']['FAILURES'][$ip])) {
176 $this->globals['IPBANS']['FAILURES'][$ip]++;
177 } else {
178 $this->globals['IPBANS']['FAILURES'][$ip] = 1;
179 }
180
181 if ($this->globals['IPBANS']['FAILURES'][$ip] >= $this->configManager->get('security.ban_after')) {
182 $this->globals['IPBANS']['BANS'][$ip] = time() + $this->configManager->get('security.ban_duration', 1800);
183 logm(
184 $this->configManager->get('resource.log'),
185 $server['REMOTE_ADDR'],
186 'IP address banned from login'
187 );
188 }
189 $this->writeBanFile();
190 }
191
192 /**
193 * Handle a successful login
194 *
195 * @param array $server The $_SERVER array
196 */
197 public function handleSuccessfulLogin($server)
198 {
199 $ip = $server['REMOTE_ADDR'];
200 // FIXME unban when behind a trusted proxy?
201
202 unset($this->globals['IPBANS']['FAILURES'][$ip]);
203 unset($this->globals['IPBANS']['BANS'][$ip]);
204
205 $this->writeBanFile();
206 }
207
208 /**
209 * Check if the user can login from this IP
210 *
211 * @param array $server The $_SERVER array
212 *
213 * @return bool true if the user is allowed to login
214 */
215 public function canLogin($server)
216 {
217 $ip = $server['REMOTE_ADDR'];
218
219 if (! isset($this->globals['IPBANS']['BANS'][$ip])) {
220 // the user is not banned
221 return true;
222 }
223
224 if ($this->globals['IPBANS']['BANS'][$ip] > time()) {
225 // the user is still banned
226 return false;
227 }
228
229 // the ban has expired, the user can attempt to log in again
230 logm($this->configManager->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
231 unset($this->globals['IPBANS']['FAILURES'][$ip]);
232 unset($this->globals['IPBANS']['BANS'][$ip]);
233
234 $this->writeBanFile();
235 return true;
236 }
237}