Skip to content

Commit

Permalink
Change constructor order so that URL is not longer first and not mand…
Browse files Browse the repository at this point in the history
…atory, added file caching class, updated documentation with new constructor and more examples.
  • Loading branch information
Ketil Stadskleiv committed Oct 3, 2016
1 parent fe1e281 commit 04108d7
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 8 deletions.
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
# PHP SDK for Bokbasen authentication service

This PHP SDK enables easy usage of Bokbasen's authentication service that is required for accessing any of Bokbasen's API suchs as digital distribution platform, metadata or orders. Bokbasen's APIs are not public and only available on commercial terms, you must have a username/password from Bokbasen in order to use this package.
This PHP SDK enables easy usage of Bokbasen's authentication service that is required for accessing any of Bokbasen's API suchs as digital distribution platform, metadata or orders. Bokbasen's APIs are not public and only available on commercial terms, you must have a username/password from Bokbasen in order to use this package.

The basic package enable creation of a TGT that can be used for further login to API services. The package also provides an interface for caching TGTs so one one can get a more effecient flow, only renewing TGT when needed. The API documenation is available on [this page](https://bokbasen.jira.com/wiki/display/api/Authentication+Service).
The basic package enable creation of a TGT that can be used for further login to API services. The package also provides an interface for caching TGTs so one one can get a more effecient flow, only renewing TGT when it is about to expire. The API documenation is available on [this page](https://bokbasen.jira.com/wiki/display/api/Authentication+Service).

## Basic usage

```php
<?php
use Bokbasen\Auth\Login;
try{
$auth = new Login(Login::URL_PROD, 'my_username','my_password');
$auth = new Login('my_username', 'my_password');
//To use TGT manually
$tgt = $auth->getTgt();
//If you are using with a Bokbasen PHP SDK, then just pass the entire $auth object
} catch(\Exception $e){
//error handling
}
```

## Use with proxy

```php
<?php
use Bokbasen\Auth\Login;
use GuzzleHttp\RequestOptions;
try{
$httpOptions = [RequestOptions::PROXY => 'https://urlToPRoxy:port'];
$auth = new Login('my_username', 'my_password', null, Login::URL_PROD, $httpOptions);
} catch(\Exception $e){
//error handling
}
```

## Use TGT cache
```php
<?php
use Bokbasen\Auth\Login;
use Bokbasen\Auth\TGTCache;
try{
//Create a TGT cache instance, any class implementing the TGTCacheInterface
$tgtCacheSession = TGTCache\Session('myNamespace');
$auth = new Login('my_username', 'my_password', $tgtCacheSession);
//If the TGT is cached, the SDK will only call the Bokbasen login server when the token is about to expire
} catch(\Exception $e){
//error handling
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
}
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.1"
"php": ">=5.6",
"guzzlehttp/guzzle": "~6.2"
},
"require-dev": {
"phpunit/phpunit": "5.*"
Expand Down
7 changes: 4 additions & 3 deletions src/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,19 @@ class Login

/**
*
* @param string $url
* @param string $username
* @param string $password
* @param TGTCacheInterface $tgtCache
* @param string $url
* @param array $httpOptions
*/
public function __construct($url, $username, $password, TGTCacheInterface $tgtCache = null)
public function __construct($username, $password, TGTCacheInterface $tgtCache = null, $url = self::URL_PROD, array $httpOptions = [])
{
$this->url = $url;
$this->tgtCache = $tgtCache;

if (! $this->isCachedTGT($tgtCache)) {
$this->auth($username, $password, $tgtCache);
$this->auth($username, $password, $tgtCache, $httpOptions);
}
}

Expand Down
78 changes: 78 additions & 0 deletions src/TGTCache/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace Bokbasen\Auth\TGTCache;

use Bokbasen\Auth\Exceptions\BokbasenAuthException;

/**
* Basic session cache class using the $_SESSION variable to cache TGT
*
*
* @license https://opensource.org/licenses/MIT
*/
class File implements TGTCacheInterface
{

/**
*
* @var string
*/
protected $pathToFile;

/**
*
* @var array
*/
protected $dataInFile;

const FILE_DELIMITER = ';';

/**
*
* @param string $pathTofile
*/
public function __construct($pathTofile)
{
$this->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;
}
}

0 comments on commit 04108d7

Please sign in to comment.