]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | namespace Shaarli\Security; | |
3 | ||
4 | use Shaarli\Config\ConfigManager; | |
5 | ||
6 | /** | |
7 | * User login management | |
8 | */ | |
9 | class LoginManager | |
10 | { | |
11 | /** @var string Name of the cookie set after logging in **/ | |
12 | public static $STAY_SIGNED_IN_COOKIE = 'shaarli_staySignedIn'; | |
13 | ||
14 | /** @var array A reference to the $_GLOBALS array */ | |
15 | protected $globals = []; | |
16 | ||
17 | /** @var ConfigManager Configuration Manager instance **/ | |
18 | protected $configManager = null; | |
19 | ||
20 | /** @var SessionManager Session Manager instance **/ | |
21 | protected $sessionManager = null; | |
22 | ||
23 | /** @var string Path to the file containing IP bans */ | |
24 | protected $banFile = ''; | |
25 | ||
26 | /** @var bool Whether the user is logged in **/ | |
27 | protected $isLoggedIn = false; | |
28 | ||
29 | /** @var bool Whether the Shaarli instance is open to public edition **/ | |
30 | protected $openShaarli = false; | |
31 | ||
32 | /** @var string User sign-in token depending on remote IP and credentials */ | |
33 | protected $staySignedInToken = ''; | |
34 | ||
35 | /** | |
36 | * Constructor | |
37 | * | |
38 | * @param array $globals The $GLOBALS array (reference) | |
39 | * @param ConfigManager $configManager Configuration Manager instance | |
40 | * @param SessionManager $sessionManager SessionManager instance | |
41 | */ | |
42 | public function __construct(& $globals, $configManager, $sessionManager) | |
43 | { | |
44 | $this->globals = &$globals; | |
45 | $this->configManager = $configManager; | |
46 | $this->sessionManager = $sessionManager; | |
47 | $this->banFile = $this->configManager->get('resource.ban_file', 'data/ipbans.php'); | |
48 | $this->readBanFile(); | |
49 | if ($this->configManager->get('security.open_shaarli') === true) { | |
50 | $this->openShaarli = true; | |
51 | } | |
52 | } | |
53 | ||
54 | /** | |
55 | * Generate a token depending on deployment salt, user password and client IP | |
56 | * | |
57 | * @param string $clientIpAddress The remote client IP address | |
58 | */ | |
59 | public function generateStaySignedInToken($clientIpAddress) | |
60 | { | |
61 | if ($this->configManager->get('security.session_protection_disabled') === true) { | |
62 | $clientIpAddress = ''; | |
63 | } | |
64 | $this->staySignedInToken = sha1( | |
65 | $this->configManager->get('credentials.hash') | |
66 | . $clientIpAddress | |
67 | . $this->configManager->get('credentials.salt') | |
68 | ); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Return the user's client stay-signed-in token | |
73 | * | |
74 | * @return string User's client stay-signed-in token | |
75 | */ | |
76 | public function getStaySignedInToken() | |
77 | { | |
78 | return $this->staySignedInToken; | |
79 | } | |
80 | ||
81 | /** | |
82 | * Check user session state and validity (expiration) | |
83 | * | |
84 | * @param array $cookie The $_COOKIE array | |
85 | * @param string $clientIpId Client IP address identifier | |
86 | */ | |
87 | public function checkLoginState($cookie, $clientIpId) | |
88 | { | |
89 | if (! $this->configManager->exists('credentials.login')) { | |
90 | // Shaarli is not configured yet | |
91 | $this->isLoggedIn = false; | |
92 | return; | |
93 | } | |
94 | ||
95 | if (isset($cookie[self::$STAY_SIGNED_IN_COOKIE]) | |
96 | && $cookie[self::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken | |
97 | ) { | |
98 | // The user client has a valid stay-signed-in cookie | |
99 | // Session information is updated with the current client information | |
100 | $this->sessionManager->storeLoginInfo($clientIpId); | |
101 | ||
102 | } elseif ($this->sessionManager->hasSessionExpired() | |
103 | || $this->sessionManager->hasClientIpChanged($clientIpId) | |
104 | ) { | |
105 | $this->sessionManager->logout(); | |
106 | $this->isLoggedIn = false; | |
107 | return; | |
108 | } | |
109 | ||
110 | $this->isLoggedIn = true; | |
111 | $this->sessionManager->extendSession(); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Return whether the user is currently logged in | |
116 | * | |
117 | * @return true when the user is logged in, false otherwise | |
118 | */ | |
119 | public function isLoggedIn() | |
120 | { | |
121 | if ($this->openShaarli) { | |
122 | return true; | |
123 | } | |
124 | return $this->isLoggedIn; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Check user credentials are valid | |
129 | * | |
130 | * @param string $remoteIp Remote client IP address | |
131 | * @param string $clientIpId Client IP address identifier | |
132 | * @param string $login Username | |
133 | * @param string $password Password | |
134 | * | |
135 | * @return bool true if the provided credentials are valid, false otherwise | |
136 | */ | |
137 | public function checkCredentials($remoteIp, $clientIpId, $login, $password) | |
138 | { | |
139 | $hash = sha1($password . $login . $this->configManager->get('credentials.salt')); | |
140 | ||
141 | if ($login != $this->configManager->get('credentials.login') | |
142 | || $hash != $this->configManager->get('credentials.hash') | |
143 | ) { | |
144 | logm( | |
145 | $this->configManager->get('resource.log'), | |
146 | $remoteIp, | |
147 | 'Login failed for user ' . $login | |
148 | ); | |
149 | return false; | |
150 | } | |
151 | ||
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 | /** | |
162 | * Read a file containing banned IPs | |
163 | */ | |
164 | protected function readBanFile() | |
165 | { | |
166 | if (! file_exists($this->banFile)) { | |
167 | return; | |
168 | } | |
169 | include $this->banFile; | |
170 | } | |
171 | ||
172 | /** | |
173 | * Write the banned IPs to a file | |
174 | */ | |
175 | protected function writeBanFile() | |
176 | { | |
177 | if (! array_key_exists('IPBANS', $this->globals)) { | |
178 | return; | |
179 | } | |
180 | file_put_contents( | |
181 | $this->banFile, | |
182 | "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals['IPBANS'], true) . ";\n?>" | |
183 | ); | |
184 | } | |
185 | ||
186 | /** | |
187 | * Handle a failed login and ban the IP after too many failed attempts | |
188 | * | |
189 | * @param array $server The $_SERVER array | |
190 | */ | |
191 | public function handleFailedLogin($server) | |
192 | { | |
193 | $ip = $server['REMOTE_ADDR']; | |
194 | $trusted = $this->configManager->get('security.trusted_proxies', []); | |
195 | ||
196 | if (in_array($ip, $trusted)) { | |
197 | $ip = getIpAddressFromProxy($server, $trusted); | |
198 | if (! $ip) { | |
199 | // the IP is behind a trusted forward proxy, but is not forwarded | |
200 | // in the HTTP headers, so we do nothing | |
201 | return; | |
202 | } | |
203 | } | |
204 | ||
205 | // increment the fail count for this IP | |
206 | if (isset($this->globals['IPBANS']['FAILURES'][$ip])) { | |
207 | $this->globals['IPBANS']['FAILURES'][$ip]++; | |
208 | } else { | |
209 | $this->globals['IPBANS']['FAILURES'][$ip] = 1; | |
210 | } | |
211 | ||
212 | if ($this->globals['IPBANS']['FAILURES'][$ip] >= $this->configManager->get('security.ban_after')) { | |
213 | $this->globals['IPBANS']['BANS'][$ip] = time() + $this->configManager->get('security.ban_duration', 1800); | |
214 | logm( | |
215 | $this->configManager->get('resource.log'), | |
216 | $server['REMOTE_ADDR'], | |
217 | 'IP address banned from login' | |
218 | ); | |
219 | } | |
220 | $this->writeBanFile(); | |
221 | } | |
222 | ||
223 | /** | |
224 | * Handle a successful login | |
225 | * | |
226 | * @param array $server The $_SERVER array | |
227 | */ | |
228 | public function handleSuccessfulLogin($server) | |
229 | { | |
230 | $ip = $server['REMOTE_ADDR']; | |
231 | // FIXME unban when behind a trusted proxy? | |
232 | ||
233 | unset($this->globals['IPBANS']['FAILURES'][$ip]); | |
234 | unset($this->globals['IPBANS']['BANS'][$ip]); | |
235 | ||
236 | $this->writeBanFile(); | |
237 | } | |
238 | ||
239 | /** | |
240 | * Check if the user can login from this IP | |
241 | * | |
242 | * @param array $server The $_SERVER array | |
243 | * | |
244 | * @return bool true if the user is allowed to login | |
245 | */ | |
246 | public function canLogin($server) | |
247 | { | |
248 | $ip = $server['REMOTE_ADDR']; | |
249 | ||
250 | if (! isset($this->globals['IPBANS']['BANS'][$ip])) { | |
251 | // the user is not banned | |
252 | return true; | |
253 | } | |
254 | ||
255 | if ($this->globals['IPBANS']['BANS'][$ip] > time()) { | |
256 | // the user is still banned | |
257 | return false; | |
258 | } | |
259 | ||
260 | // the ban has expired, the user can attempt to log in again | |
261 | logm($this->configManager->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.'); | |
262 | unset($this->globals['IPBANS']['FAILURES'][$ip]); | |
263 | unset($this->globals['IPBANS']['BANS'][$ip]); | |
264 | ||
265 | $this->writeBanFile(); | |
266 | return true; | |
267 | } | |
268 | } |