*/
class LoginManager
{
+ /** @var string Name of the cookie set after logging in **/
+ public static $STAY_SIGNED_IN_COOKIE = 'shaarli_staySignedIn';
+
/** @var array A reference to the $_GLOBALS array */
protected $globals = [];
/** @var bool Whether the Shaarli instance is open to public edition **/
protected $openShaarli = false;
+ /** @var string User sign-in token depending on remote IP and credentials */
+ protected $staySignedInToken = '';
+
/**
* Constructor
*
}
}
+ /**
+ * Generate a token depending on deployment salt, user password and client IP
+ *
+ * @param string $clientIpAddress The remote client IP address
+ */
+ public function generateStaySignedInToken($clientIpAddress)
+ {
+ $this->staySignedInToken = sha1(
+ $this->configManager->get('credentials.hash')
+ . $clientIpAddress
+ . $this->configManager->get('credentials.salt')
+ );
+ }
+
+ /**
+ * Return the user's client stay-signed-in token
+ *
+ * @return string User's client stay-signed-in token
+ */
+ public function getStaySignedInToken()
+ {
+ return $this->staySignedInToken;
+ }
+
/**
* Check user session state and validity (expiration)
*
* @param array $cookie The $_COOKIE array
* @param string $clientIpId Client IP address identifier
- * @param string $token Session token
*
* @return bool true if the user session is valid, false otherwise
*/
- public function checkLoginState($cookie, $clientIpId, $token)
+ public function checkLoginState($cookie, $clientIpId)
{
if (! $this->configManager->exists('credentials.login')) {
// Shaarli is not configured yet
return;
}
- if (isset($cookie[SessionManager::$LOGGED_IN_COOKIE])
- && $cookie[SessionManager::$LOGGED_IN_COOKIE] === $token
+ if (isset($cookie[self::$STAY_SIGNED_IN_COOKIE])
+ && $cookie[self::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken
) {
$this->sessionManager->storeLoginInfo($clientIpId);
$this->isLoggedIn = true;
/** @var int Session expiration timeout, in seconds */
public static $LONG_TIMEOUT = 31536000; // 1 year
- /** @var string Name of the cookie set after logging in **/
- public static $LOGGED_IN_COOKIE = 'shaarli_staySignedIn';
-
/** @var array Local reference to the global $_SESSION array */
protected $session = [];
$conf = new ConfigManager();
$sessionManager = new SessionManager($_SESSION, $conf);
$loginManager = new LoginManager($GLOBALS, $conf, $sessionManager);
+$loginManager->generateStaySignedInToken($_SERVER['REMOTE_ADDR']);
$clientIpId = client_ip_id($_SERVER);
// LC_MESSAGES isn't defined without php-intl, in this case use LC_COLLATE locale instead.
install($conf, $sessionManager);
}
-// a token depending of deployment salt, user password, and the current ip
-define('STAY_SIGNED_IN_TOKEN', sha1($conf->get('credentials.hash') . $_SERVER['REMOTE_ADDR'] . $conf->get('credentials.salt')));
-
-$loginManager->checkLoginState($_COOKIE, $clientIpId, STAY_SIGNED_IN_TOKEN);
+$loginManager->checkLoginState($_COOKIE, $clientIpId);
/**
* Adapter function to ensure compatibility with third-party templates
$expirationTime = $sessionManager->extendSession();
setcookie(
- $sessionManager::$LOGGED_IN_COOKIE,
- STAY_SIGNED_IN_TOKEN,
+ $loginManager::$STAY_SIGNED_IN_COOKIE,
+ $loginManager->getStaySignedInToken(),
$expirationTime,
WEB_PATH
);
{
invalidateCaches($conf->get('resource.page_cache'));
$sessionManager->logout();
- setcookie(SessionManager::$LOGGED_IN_COOKIE, 'false', 0, WEB_PATH);
+ setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, WEB_PATH);
header('Location: ?');
exit;
}
protected $server = [];
protected $trustedProxy = '10.1.1.100';
+ /** @var string User login */
+ protected $login = 'johndoe';
+
+ /** @var string User password */
+ protected $password = 'IC4nHazL0g1n?';
+
+ /** @var string Hash of the salted user password */
+ protected $passwordHash = '';
+
+ /** @var string Salt used by hash functions */
+ protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2';
+
/**
* Prepare or reset test resources
*/
unlink($this->banFile);
}
+ $this->passwordHash = sha1($this->password . $this->login . $this->salt);
+
$this->configManager = new \FakeConfigManager([
+ 'credentials.login' => $this->login,
+ 'credentials.hash' => $this->passwordHash,
+ 'credentials.salt' => $this->salt,
'resource.ban_file' => $this->banFile,
'resource.log' => $this->logFile,
'security.ban_after' => 4,
$this->globals['IPBANS']['BANS'][$this->ipAddr] = time() - 3600;
$this->assertTrue($this->loginManager->canLogin($this->server));
}
+
+ /**
+ * Generate a token depending on the user credentials and client IP
+ */
+ public function testGenerateStaySignedInToken()
+ {
+ $ipAddress = '10.1.47.179';
+ $this->loginManager->generateStaySignedInToken($ipAddress);
+
+ $this->assertEquals(
+ sha1($this->passwordHash . $ipAddress . $this->salt),
+ $this->loginManager->getStaySignedInToken()
+ );
+ }
}