Move Cookie to own class (with tests)

Move Authentication to App namespace
This commit is contained in:
nupplaPhil 2019-12-08 22:45:34 +01:00
parent c45b6d309b
commit 54392fab81
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
16 changed files with 393 additions and 112 deletions

View file

@ -6,10 +6,12 @@
namespace Friendica\Core;
use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Session\CacheSessionHandler;
use Friendica\Core\Session\DatabaseSessionHandler;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\User;
use Friendica\Util\Strings;
/**
@ -171,73 +173,15 @@ class Session
return $_SESSION['authenticated'];
}
/**
* @brief Calculate the hash that is needed for the "Friendica" cookie
*
* @param array $user Record from "user" table
*
* @return string Hashed data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function getCookieHashForUser($user)
{
return hash_hmac(
"sha256",
hash_hmac("sha256", $user["password"], $user["prvkey"]),
Config::get("system", "site_prvkey")
);
}
/**
* @brief Set the "Friendica" cookie
*
* @param int $time
* @param array $user Record from "user" table
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function setCookie($time, $user = [])
{
if ($time != 0) {
$time = $time + time();
}
if ($user) {
$value = json_encode([
"uid" => $user["uid"],
"hash" => self::getCookieHashForUser($user),
"ip" => ($_SERVER['REMOTE_ADDR'] ?? '') ?: '0.0.0.0'
]);
} else {
$value = "";
}
setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL), true);
}
/**
* @brief Checks if the "Friendica" cookie is set
*
* @param string $hash
* @param array $user Record from "user" table
*
* @return boolean True, if the cookie is set
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function checkCookie(string $hash, array $user)
{
return hash_equals(
self::getCookieHashForUser($user),
$hash
);
}
/**
* @brief Kills the "Friendica" cookie and all session data
*/
public static function delete()
{
self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
/** @var User\Cookie $cookie */
$cookie = BaseObject::getClass(User\Cookie::class);
$cookie->clear();
$_SESSION = [];
session_unset();
session_destroy();
}