From 4e58f91d72c7042fa2faff9292399072ce28c9ed Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 7 Apr 2019 12:12:25 +0200 Subject: [PATCH] no globals in session --- ipf/crypto.php | 45 --------------------------------------------- ipf/session/app.php | 27 +++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 49 deletions(-) delete mode 100644 ipf/crypto.php 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 -- 2.49.0