]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/security/LoginManager.php
Refactor session and cookie timeout control
[github/shaarli/Shaarli.git] / application / security / LoginManager.php
CommitLineData
44acf706 1<?php
fab87c26 2namespace Shaarli\Security;
44acf706 3
c7721487
V
4use Shaarli\Config\ConfigManager;
5
44acf706
V
6/**
7 * User login management
8 */
9class LoginManager
10{
1b28c66c 11 /** @var array A reference to the $_GLOBALS array */
44acf706 12 protected $globals = [];
1b28c66c
V
13
14 /** @var ConfigManager Configuration Manager instance **/
44acf706 15 protected $configManager = null;
1b28c66c
V
16
17 /** @var SessionManager Session Manager instance **/
63ea23c2 18 protected $sessionManager = null;
1b28c66c
V
19
20 /** @var string Path to the file containing IP bans */
44acf706 21 protected $banFile = '';
1b28c66c
V
22
23 /** @var bool Whether the user is logged in **/
63ea23c2 24 protected $isLoggedIn = false;
1b28c66c
V
25
26 /** @var bool Whether the Shaarli instance is open to public edition **/
63ea23c2 27 protected $openShaarli = false;
44acf706
V
28
29 /**
30 * Constructor
31 *
63ea23c2
V
32 * @param array $globals The $GLOBALS array (reference)
33 * @param ConfigManager $configManager Configuration Manager instance
34 * @param SessionManager $sessionManager SessionManager instance
44acf706 35 */
63ea23c2 36 public function __construct(& $globals, $configManager, $sessionManager)
44acf706
V
37 {
38 $this->globals = &$globals;
39 $this->configManager = $configManager;
63ea23c2 40 $this->sessionManager = $sessionManager;
44acf706
V
41 $this->banFile = $this->configManager->get('resource.ban_file', 'data/ipbans.php');
42 $this->readBanFile();
63ea23c2
V
43 if ($this->configManager->get('security.open_shaarli')) {
44 $this->openShaarli = true;
45 }
46 }
47
48 /**
49 * Check user session state and validity (expiration)
50 *
84742084 51 * @param array $cookie The $_COOKIE array
84742084
V
52 * @param string $clientIpId Client IP address identifier
53 * @param string $token Session token
63ea23c2
V
54 *
55 * @return bool true if the user session is valid, false otherwise
56 */
51f0128c 57 public function checkLoginState($cookie, $clientIpId, $token)
63ea23c2
V
58 {
59 if (! $this->configManager->exists('credentials.login')) {
60 // Shaarli is not configured yet
61 $this->isLoggedIn = false;
62 return;
63 }
64
65 if (isset($cookie[SessionManager::$LOGGED_IN_COOKIE])
66 && $cookie[SessionManager::$LOGGED_IN_COOKIE] === $token
67 ) {
c7721487 68 $this->sessionManager->storeLoginInfo($clientIpId);
63ea23c2
V
69 $this->isLoggedIn = true;
70 }
71
c7721487
V
72 if ($this->sessionManager->hasSessionExpired()
73 || $this->sessionManager->hasClientIpChanged($clientIpId)
63ea23c2 74 ) {
51f0128c 75 $this->sessionManager->logout();
63ea23c2
V
76 $this->isLoggedIn = false;
77 return;
78 }
79
c7721487 80 $this->sessionManager->extendSession();
63ea23c2
V
81 }
82
83 /**
84 * Return whether the user is currently logged in
85 *
86 * @return true when the user is logged in, false otherwise
87 */
88 public function isLoggedIn()
89 {
90 if ($this->openShaarli) {
91 return true;
92 }
93 return $this->isLoggedIn;
94 }
95
96 /**
97 * Check user credentials are valid
98 *
84742084
V
99 * @param string $remoteIp Remote client IP address
100 * @param string $clientIpId Client IP address identifier
101 * @param string $login Username
102 * @param string $password Password
63ea23c2
V
103 *
104 * @return bool true if the provided credentials are valid, false otherwise
105 */
84742084 106 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
63ea23c2
V
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'),
84742084 115 $remoteIp,
63ea23c2
V
116 'Login failed for user ' . $login
117 );
118 return false;
119 }
120
c7721487 121 $this->sessionManager->storeLoginInfo($clientIpId);
63ea23c2
V
122 logm(
123 $this->configManager->get('resource.log'),
84742084 124 $remoteIp,
63ea23c2
V
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}