Skip to content

Commit

Permalink
Rewrite of the cache functionality, replacing internal cache interfac…
Browse files Browse the repository at this point in the history
…e with generic PSR-6 cache interface support.
  • Loading branch information
Ketil Stadskleiv committed Oct 11, 2016
1 parent 895646f commit bab07c7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 181 deletions.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
53 changes: 27 additions & 26 deletions src/Login.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Bokbasen\Auth;

use Bokbasen\Auth\TGTCache\TGTCacheInterface;
use Psr\Cache\CacheItemPoolInterface;
use GuzzleHttp\RequestOptions;
use Bokbasen\Auth\Exceptions\BokbasenAuthException;

Expand Down Expand Up @@ -42,7 +42,7 @@ class Login

/**
*
* @var TGTCacheInterface
* @var \Psr\Cache\CacheItemPoolInterface
*/
protected $tgtCache;

Expand All @@ -65,37 +65,44 @@ class Login

const DEFAULT_TGT_EXPIRE_TIME_MINUTES = 115;

const CACHE_ITEM_KEY = 'bokbasen.tgt';

/**
*
* @param string $username
* @param string $password
* @param TGTCacheInterface $tgtCache
* @param CacheItemPoolInterface $tgtCache
* @param string $url
* @param array $httpOptions
*/
public function __construct($username, $password, TGTCacheInterface $tgtCache = null, $url = self::URL_PROD, array $httpOptions = [])
public function __construct($username, $password, CacheItemPoolInterface $tgtCache = null, $url = self::URL_PROD, array $httpOptions = [])
{
$this->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);
}
}
}

Expand All @@ -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, [
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down
78 changes: 0 additions & 78 deletions src/TGTCache/File.php

This file was deleted.

44 changes: 0 additions & 44 deletions src/TGTCache/Session.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/TGTCache/TGTCacheInterface.php

This file was deleted.

16 changes: 14 additions & 2 deletions tests/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Bokbasen\Auth\Tests;

use Bokbasen\Auth\Login;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

class LoginTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -26,7 +27,7 @@ public function __construct($name = null, array $data = [], $dataName = '')

public function testLogin()
{
$this->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());
}

Expand All @@ -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());
}
}
3 changes: 2 additions & 1 deletion tests/config_template.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
;config template, rename this to config.ini before executing tests
url = https://login.boknett.webbe.no/v1/tickets
username =
password =
password =
fileCacheDir =

0 comments on commit bab07c7

Please sign in to comment.