-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Hughes
committed
Mar 4, 2015
1 parent
1c99e12
commit 850bbff
Showing
1 changed file
with
72 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Choccybiccy\HumanApi; | ||
|
||
use SebastianBergmann\Exporter\Exception; | ||
|
||
/** | ||
* Class Auth | ||
* @package Choccybiccy\HumanApi | ||
*/ | ||
class Auth extends Api | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $apiUrl = "https://user.humanapi.co"; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $apiVersion = 1; | ||
|
||
/** | ||
* @var stdClass | ||
*/ | ||
protected $sessionTokenObject; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @see https://docs.humanapi.co/docs/connect-backend | ||
* | ||
* @param string $sessionTokenObject | ||
* @param string $clientSecret | ||
*/ | ||
public function __construct($sessionTokenObject, $clientSecret) | ||
{ | ||
|
||
$sessionTokenObject = $this->decodeJson($sessionTokenObject); | ||
$sessionTokenObject->clientSecret = $clientSecret; | ||
$this->sessionTokenObject = $sessionTokenObject; | ||
|
||
parent::__construct(); | ||
|
||
} | ||
|
||
/** | ||
* Finish the auth flow and post to connect endpoint | ||
*/ | ||
public function finish() | ||
{ | ||
$response = $this->post( | ||
$this->buildUrlParts(array("tokens"), "connect"), | ||
array( | ||
"json" => (array) $this->sessionTokenObject, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Decode JSON data | ||
* | ||
* @param string $str JSON data | ||
* @return stdClass | ||
*/ | ||
protected function decodeJson($str) | ||
{ | ||
$str = preg_replace("/([a-zA-Z0-9_]+?):/" , "\"$1\":", $str); // fix variable names | ||
return json_decode($str); | ||
} | ||
} |