-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change constructor order so that URL is not longer first and not mand…
…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
Showing
4 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |