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