Skip to content

Commit

Permalink
release 1.0.2: implemented token based authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
intodeep committed Feb 9, 2021
1 parent fa3f49e commit f8a4320
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Or edit `composer.json` and add:

Example
-------

Login with Basic Authentication
```php
<?php

Expand All @@ -49,6 +49,20 @@ $password = 'password';
// create a deep desk object
\Deepser\Deepser::init($host, $username, $password);

// ...
```
Login with Token Based Authentication
```php
<?php

require 'vendor/autoload.php';

$host = 'https://your.deepser.net';
$username = 'username';
$token = 'mytokenhere';
// create a deep desk object
\Deepser\Deepser::init($host, $token);

// ...
```

Expand Down
34 changes: 31 additions & 3 deletions src/Deepser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,40 @@

final class Deepser
{
const AUTH_BASIC = 'basic';
const AUTH_TOKEN = 'token';

static private $_adapter = null;
static private $_client = null;
static private $_host = null;
static private $_authType = null;
static private $_token = null;
static private $_username = null;
static private $_password = null;

/**
* @param null $host
* @param null $username
* @param null $password
*/
public static function init($host = null, $username = null, $password = null){
if($host){
self::$_host = $host;
}

if($username){
self::$_username = $username;
}
if($password){
self::$_password = $password;
}

if($username && $password){
self::$_authType = self::AUTH_BASIC;
}else{
self::$_authType = self::AUTH_TOKEN;
self::setToken($username);
}
}

public static function setHost($host){
Expand All @@ -38,6 +56,10 @@ public static function setPassword($password){
self::$_password = $password;
}

public static function setToken($token){
self::$_token = $token;
}

public static function getAdapter(){
if(self::$_adapter == null){
self::$_adapter = new \Deepser\Adapter\GuzzleHttpAdapter(self::$_host, self::getClient());
Expand Down Expand Up @@ -69,9 +91,15 @@ public static function getClient(){
*/
public static function getHeaders(){
$token = base64_encode(self::$_username . ":" . self::$_password);
return [
'Authorization' => 'Basic ' . $token,
'Accept' => 'application/json',
$headers = [
'Accept' => 'application/json'
];
if(self::$_authType == self::AUTH_BASIC){
$headers['Authorization'] = 'Basic ' . $token;
}else{
$headers['Authorization'] = 'Bearer ' . self::$_token;
}

return $headers;
}
}

0 comments on commit f8a4320

Please sign in to comment.