From bab07c7ee7691167a1cc125725292a2a410d839c Mon Sep 17 00:00:00 2001 From: Ketil Stadskleiv Date: Tue, 11 Oct 2016 11:09:29 +0200 Subject: [PATCH] Rewrite of the cache functionality, replacing internal cache interface with generic PSR-6 cache interface support. --- composer.json | 10 +++- src/Login.php | 53 ++++++++++---------- src/TGTCache/File.php | 78 ------------------------------ src/TGTCache/Session.php | 44 ----------------- src/TGTCache/TGTCacheInterface.php | 28 ----------- tests/LoginTest.php | 16 +++++- tests/config_template.ini | 3 +- 7 files changed, 51 insertions(+), 181 deletions(-) delete mode 100644 src/TGTCache/File.php delete mode 100644 src/TGTCache/Session.php delete mode 100644 src/TGTCache/TGTCacheInterface.php diff --git a/composer.json b/composer.json index 9c59cbb..e6fa112 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,15 @@ ], "require": { "php": ">=5.6", - "guzzlehttp/guzzle": "~6.2" + "guzzlehttp/guzzle": "~6.2", + "psr/cache": "~1", + "psr/http-message": "~1" }, "require-dev": { - "phpunit/phpunit": "5.*" + "phpunit/phpunit": "5.*", + "symfony/cache": "3.*" + }, + "provide": { + "psr/cache-implementation": "1.0" } } \ No newline at end of file diff --git a/src/Login.php b/src/Login.php index 833b614..c8362fb 100644 --- a/src/Login.php +++ b/src/Login.php @@ -1,7 +1,7 @@ url = $url; $this->tgtCache = $tgtCache; + $this->tgtExpireMinutes = self::DEFAULT_TGT_EXPIRE_TIME_MINUTES; - if (! $this->isCachedTGT($tgtCache)) { - $this->auth($username, $password, $tgtCache, $httpOptions); + if (! $this->isCachedTGT()) { + $this->auth($username, $password, $httpOptions); } } /** * Check if TGT is cached and if cache is valid, will set $this->tgt to cached value if true * - * @param TGTCacheInterface $tgtCache * @return bool */ - protected function isCachedTGT(TGTCacheInterface $tgtCache = null) + protected function isCachedTGT() { - if (is_null($tgtCache) || empty($tgtCache->getTGT()) || $this->isTGTSoonExpired($tgtCache)) { + if (is_null($this->tgtCache)) { return false; } else { - $this->tgt = $tgtCache->getTGT(); - return true; + $cachedItem = $this->tgtCache->getItem(self::CACHE_ITEM_KEY); + if (is_null($cachedItem) || ! $this->tgtCache->getItem(self::CACHE_ITEM_KEY)->isHit()) { + return false; + } else { + $this->tgt = $cachedItem->get(); + return ! empty($this->tgt); + } } } @@ -106,12 +113,15 @@ protected function isCachedTGT(TGTCacheInterface $tgtCache = null) * @param string $password * @param array $httpOptions * @throws BokbasenAuthException + * @return void */ protected function auth($username, $password, array $httpOptions = []) { $httpOptions = array_merge([ RequestOptions::ALLOW_REDIRECTS => false ], $httpOptions); + + // @todo, consider replacing this with generic support for injecting an PSR-7 adapter $this->httpClient = new \GuzzleHttp\Client($httpOptions); $response = $this->httpClient->request('POST', $this->url, [ @@ -128,25 +138,16 @@ protected function auth($username, $password, array $httpOptions = []) $this->tgt = array_pop($tgtHeaders); if (! is_null($this->tgtCache)) { - $this->tgtCache->setTGT($this->tgt); + $tgtCacheItem = $this->tgtCache->getItem(self::CACHE_ITEM_KEY); + $tgtCacheItem->set($this->tgt); + $tgtCacheItem->expiresAfter($this->tgtExpireMinutes * 60); + $this->tgtCache->save($tgtCacheItem); } } /** * - * @param TGTCacheInterface $tgtCache - * @return bool - */ - public function isTGTSoonExpired(TGTCacheInterface $tgtCache = null) - { - $dateTime = time() - $this->tgtExpireMinutes * 60; - - return $dateTime > $tgtCache->getCreatedUnixTimestamp(); - } - - /** - * - * @return the $tgt + * @return string */ public function getTgt() { @@ -184,7 +185,7 @@ public function getAuthHeadersAsArray() } /** - * If no TGT cache is defined, then destruct will perform a HTTP DELETE call to clear the ticket + * If no TGT cache is defined, then destruct will perform a HTTP DELETE call to clear the TGT */ public function __destruct() { diff --git a/src/TGTCache/File.php b/src/TGTCache/File.php deleted file mode 100644 index 398f311..0000000 --- a/src/TGTCache/File.php +++ /dev/null @@ -1,78 +0,0 @@ -pathToFile = $pathTofile; - } - - protected function getDataAsArray() - { - if (empty($this->dataInFile)) { - $data = file_get_contents($this->pathToFile); - - if (empty($data)) { - return []; - } - - $this->dataInFile = explode(self::FILE_DELIMITER, $data); - - if (count($this->dataInFile) != 2) { - throw new BokbasenAuthException('Invalid data in file, explode gave more than 2 elements: ' . $this->pathToFile); - } - } - - return $this->dataInFile; - } - - public function setTGT($tgt) - { - $data = $tgt . self::FILE_DELIMITER . time(); - $bytes = file_put_contents($this->pathToFile, $data); - - if ($bytes === false) { - throw new BokbasenAuthException('Could not write to file: ' . $this->pathToFile); - } - } - - public function getTGT() - { - $data = $this->getDataAsArray(); - return isset($data[0]) ? $data[0] : null; - } - - public function getCreatedUnixTimestamp() - { - $data = $this->getDataAsArray(); - return isset($data[1]) ? $data[1] : null; - } -} \ No newline at end of file diff --git a/src/TGTCache/Session.php b/src/TGTCache/Session.php deleted file mode 100644 index 69808ab..0000000 --- a/src/TGTCache/Session.php +++ /dev/null @@ -1,44 +0,0 @@ -namespace = $namespace; - } - - public function setTGT($tgt) - { - $_SESSION[$this->namespace]['tgt'] = $tgt; - $_SESSION[$this->namespace]['timestamp'] = time(); - } - - public function getTGT() - { - return isset($_SESSION[$this->namespace]['tgt']) ? $_SESSION[$this->namespace]['tgt'] : null; - } - - public function getCreatedUnixTimestamp() - { - return isset($_SESSION[$this->namespace]['timestamp']) ? $_SESSION[$this->namespace]['timestamp'] : null; - } -} \ No newline at end of file diff --git a/src/TGTCache/TGTCacheInterface.php b/src/TGTCache/TGTCacheInterface.php deleted file mode 100644 index a554bf3..0000000 --- a/src/TGTCache/TGTCacheInterface.php +++ /dev/null @@ -1,28 +0,0 @@ -auth = new Login($this->config['url'], $this->config['username'], $this->config['password']); + $this->auth = new Login($this->config['username'], $this->config['password'], null, $this->config['url']); $this->assertNotEmpty($this->auth->getTgt()); } @@ -35,11 +36,22 @@ public function testFailedLogin() $this->expectException(\GuzzleHttp\Exception\ClientException::class); try { - $auth = new Login($this->config['url'], 'dsds', 'fsdfsdfo8s'); + $auth = new Login('dsds', 'fsdfsdfo8s', null, $this->config['url']); $this->assertNotEmpty($auth->getTgt()); } catch (\Exception $e) { $this->assertEquals(400, $e->getCode()); throw $e; } } + + public function testCache() + { + $cache = new FilesystemAdapter(null, 0, $this->config['fileCacheDir']); + $auth = new Login($this->config['username'], $this->config['password'], $cache, $this->config['url']); + $this->assertNotEmpty($auth->getTgt()); + + // rerun auth with cache to see that we get the same TGT + $auth2 = new Login($this->config['username'], $this->config['password'], $cache, $this->config['url']); + $this->assertEquals($auth->getTgt(), $auth2->getTgt()); + } } \ No newline at end of file diff --git a/tests/config_template.ini b/tests/config_template.ini index 2a99f20..ce1fde3 100644 --- a/tests/config_template.ini +++ b/tests/config_template.ini @@ -1,4 +1,5 @@ ;config template, rename this to config.ini before executing tests url = https://login.boknett.webbe.no/v1/tickets username = -password = \ No newline at end of file +password = +fileCacheDir = \ No newline at end of file