From: Andrey Kutejko Date: Sun, 7 Apr 2019 10:12:25 +0000 (+0200) Subject: no globals in session X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=4e58f91d72c7042fa2faff9292399072ce28c9ed;p=ipf.git no globals in session --- diff --git a/ipf/crypto.php b/ipf/crypto.php deleted file mode 100644 index f4624b7..0000000 --- a/ipf/crypto.php +++ /dev/null @@ -1,45 +0,0 @@ -get('secret_key'); + if (!$secret_key) { + throw new Exception('Security error: "secret_key" is not set in the configuration file.'); + } + $this->backends = [ - new CookieSessionBackend(), - new DBSessionBackend($container, $container['settings']->get('secret_key')), + new CookieSessionBackend($secret_key), + new DBSessionBackend($container, $secret_key), ]; } @@ -78,13 +83,21 @@ interface SessionBackend class CookieSessionBackend implements SessionBackend { + /** @var string */ + private $secret_key; + + function __construct($secret_key) + { + $this->secret_key = $secret_key; + } + public function getData($key) { $key = explode('|', $key, 2); if (count($key) !== 2) return null; list($data, $sign) = $key; - if (IPF_Crypto::sign($data) !== $sign) + if ($this->sign($data) !== $sign) return null; return unserialize(base64_decode($data)); @@ -93,13 +106,19 @@ class CookieSessionBackend implements SessionBackend public function save($key, $data) { $encoded = base64_encode(serialize($data)); - return $encoded . '|' . IPF_Crypto::sign($encoded); + return $encoded . '|' . $this->sign($encoded); } public function delete($key) { // DO NOTHING } + + private function sign($data) + { + $key = sha1($this->secret_key . $this->secret_key); + return hash_hmac('sha1', $data, $key); + } } class DBSessionBackend implements SessionBackend