diff options
Diffstat (limited to 'application/security/LoginManager.php')
-rw-r--r-- | application/security/LoginManager.php | 83 |
1 files changed, 36 insertions, 47 deletions
diff --git a/application/security/LoginManager.php b/application/security/LoginManager.php index 39ec9b2e..426e785e 100644 --- a/application/security/LoginManager.php +++ b/application/security/LoginManager.php | |||
@@ -2,6 +2,7 @@ | |||
2 | namespace Shaarli\Security; | 2 | namespace Shaarli\Security; |
3 | 3 | ||
4 | use Exception; | 4 | use Exception; |
5 | use Psr\Log\LoggerInterface; | ||
5 | use Shaarli\Config\ConfigManager; | 6 | use Shaarli\Config\ConfigManager; |
6 | 7 | ||
7 | /** | 8 | /** |
@@ -9,9 +10,6 @@ use Shaarli\Config\ConfigManager; | |||
9 | */ | 10 | */ |
10 | class LoginManager | 11 | class LoginManager |
11 | { | 12 | { |
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 */ | 13 | /** @var array A reference to the $_GLOBALS array */ |
16 | protected $globals = []; | 14 | protected $globals = []; |
17 | 15 | ||
@@ -32,24 +30,32 @@ class LoginManager | |||
32 | 30 | ||
33 | /** @var string User sign-in token depending on remote IP and credentials */ | 31 | /** @var string User sign-in token depending on remote IP and credentials */ |
34 | protected $staySignedInToken = ''; | 32 | protected $staySignedInToken = ''; |
33 | /** @var CookieManager */ | ||
34 | protected $cookieManager; | ||
35 | /** @var LoggerInterface */ | ||
36 | protected $logger; | ||
35 | 37 | ||
36 | /** | 38 | /** |
37 | * Constructor | 39 | * Constructor |
38 | * | 40 | * |
39 | * @param ConfigManager $configManager Configuration Manager instance | 41 | * @param ConfigManager $configManager Configuration Manager instance |
40 | * @param SessionManager $sessionManager SessionManager instance | 42 | * @param SessionManager $sessionManager SessionManager instance |
43 | * @param CookieManager $cookieManager CookieManager instance | ||
44 | * @param BanManager $banManager | ||
45 | * @param LoggerInterface $logger Used to log login attempts | ||
41 | */ | 46 | */ |
42 | public function __construct($configManager, $sessionManager) | 47 | public function __construct( |
43 | { | 48 | ConfigManager $configManager, |
49 | SessionManager $sessionManager, | ||
50 | CookieManager $cookieManager, | ||
51 | BanManager $banManager, | ||
52 | LoggerInterface $logger | ||
53 | ) { | ||
44 | $this->configManager = $configManager; | 54 | $this->configManager = $configManager; |
45 | $this->sessionManager = $sessionManager; | 55 | $this->sessionManager = $sessionManager; |
46 | $this->banManager = new BanManager( | 56 | $this->cookieManager = $cookieManager; |
47 | $this->configManager->get('security.trusted_proxies', []), | 57 | $this->banManager = $banManager; |
48 | $this->configManager->get('security.ban_after'), | 58 | $this->logger = $logger; |
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 | 59 | ||
54 | if ($this->configManager->get('security.open_shaarli') === true) { | 60 | if ($this->configManager->get('security.open_shaarli') === true) { |
55 | $this->openShaarli = true; | 61 | $this->openShaarli = true; |
@@ -86,10 +92,9 @@ class LoginManager | |||
86 | /** | 92 | /** |
87 | * Check user session state and validity (expiration) | 93 | * Check user session state and validity (expiration) |
88 | * | 94 | * |
89 | * @param array $cookie The $_COOKIE array | ||
90 | * @param string $clientIpId Client IP address identifier | 95 | * @param string $clientIpId Client IP address identifier |
91 | */ | 96 | */ |
92 | public function checkLoginState($cookie, $clientIpId) | 97 | public function checkLoginState($clientIpId) |
93 | { | 98 | { |
94 | if (! $this->configManager->exists('credentials.login')) { | 99 | if (! $this->configManager->exists('credentials.login')) { |
95 | // Shaarli is not configured yet | 100 | // Shaarli is not configured yet |
@@ -97,9 +102,7 @@ class LoginManager | |||
97 | return; | 102 | return; |
98 | } | 103 | } |
99 | 104 | ||
100 | if (isset($cookie[self::$STAY_SIGNED_IN_COOKIE]) | 105 | if ($this->staySignedInToken === $this->cookieManager->getCookieParameter(CookieManager::STAY_SIGNED_IN)) { |
101 | && $cookie[self::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken | ||
102 | ) { | ||
103 | // The user client has a valid stay-signed-in cookie | 106 | // The user client has a valid stay-signed-in cookie |
104 | // Session information is updated with the current client information | 107 | // Session information is updated with the current client information |
105 | $this->sessionManager->storeLoginInfo($clientIpId); | 108 | $this->sessionManager->storeLoginInfo($clientIpId); |
@@ -120,7 +123,7 @@ class LoginManager | |||
120 | * | 123 | * |
121 | * @return true when the user is logged in, false otherwise | 124 | * @return true when the user is logged in, false otherwise |
122 | */ | 125 | */ |
123 | public function isLoggedIn() | 126 | public function isLoggedIn(): bool |
124 | { | 127 | { |
125 | if ($this->openShaarli) { | 128 | if ($this->openShaarli) { |
126 | return true; | 129 | return true; |
@@ -131,48 +134,34 @@ class LoginManager | |||
131 | /** | 134 | /** |
132 | * Check user credentials are valid | 135 | * Check user credentials are valid |
133 | * | 136 | * |
134 | * @param string $remoteIp Remote client IP address | ||
135 | * @param string $clientIpId Client IP address identifier | 137 | * @param string $clientIpId Client IP address identifier |
136 | * @param string $login Username | 138 | * @param string $login Username |
137 | * @param string $password Password | 139 | * @param string $password Password |
138 | * | 140 | * |
139 | * @return bool true if the provided credentials are valid, false otherwise | 141 | * @return bool true if the provided credentials are valid, false otherwise |
140 | */ | 142 | */ |
141 | public function checkCredentials($remoteIp, $clientIpId, $login, $password) | 143 | public function checkCredentials($clientIpId, $login, $password) |
142 | { | 144 | { |
143 | // Check login matches config | ||
144 | if ($login !== $this->configManager->get('credentials.login')) { | ||
145 | return false; | ||
146 | } | ||
147 | |||
148 | // Check credentials | 145 | // Check credentials |
149 | try { | 146 | try { |
150 | $useLdapLogin = !empty($this->configManager->get('ldap.host')); | 147 | $useLdapLogin = !empty($this->configManager->get('ldap.host')); |
151 | if ((false === $useLdapLogin && $this->checkCredentialsFromLocalConfig($login, $password)) | 148 | if ($login === $this->configManager->get('credentials.login') |
152 | || (true === $useLdapLogin && $this->checkCredentialsFromLdap($login, $password)) | 149 | && ( |
150 | (false === $useLdapLogin && $this->checkCredentialsFromLocalConfig($login, $password)) | ||
151 | || (true === $useLdapLogin && $this->checkCredentialsFromLdap($login, $password)) | ||
152 | ) | ||
153 | ) { | 153 | ) { |
154 | $this->sessionManager->storeLoginInfo($clientIpId); | 154 | $this->sessionManager->storeLoginInfo($clientIpId); |
155 | logm( | 155 | $this->logger->info(format_log('Login successful', $clientIpId)); |
156 | $this->configManager->get('resource.log'), | 156 | |
157 | $remoteIp, | 157 | return true; |
158 | 'Login successful' | ||
159 | ); | ||
160 | return true; | ||
161 | } | 158 | } |
162 | } | 159 | } catch(Exception $exception) { |
163 | catch(Exception $exception) { | 160 | $this->logger->info(format_log('Exception while checking credentials: ' . $exception, $clientIpId)); |
164 | logm( | ||
165 | $this->configManager->get('resource.log'), | ||
166 | $remoteIp, | ||
167 | 'Exception while checking credentials: ' . $exception | ||
168 | ); | ||
169 | } | 161 | } |
170 | 162 | ||
171 | logm( | 163 | $this->logger->info(format_log('Login failed for user ' . $login, $clientIpId)); |
172 | $this->configManager->get('resource.log'), | 164 | |
173 | $remoteIp, | ||
174 | 'Login failed for user ' . $login | ||
175 | ); | ||
176 | return false; | 165 | return false; |
177 | } | 166 | } |
178 | 167 | ||