Skip to content

Commit

Permalink
Auth api and unit tests. Updated usage docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Hughes committed Mar 5, 2015
1 parent 850bbff commit 3e69dc0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 27 deletions.
47 changes: 43 additions & 4 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,52 @@

Using the API is designed to be as easy as possible for developers.

## Human Connect popup
See the [official documentation](https://docs.humanapi.co/docs/connect-setup) on how to set this up for new and
existing users.

## Finalising the auth flow
Following the [official documentation](https://docs.humanapi.co/docs/connect-setup) on setting up your Connect
popup will result in `POST` data being sent to your server endpoint defined in the Connect options of the previous step.
Typically the `POST` data will look something like this:

```php
array(3) {
'humanId' =>
string(24) "52867cbede3155565f000a0d"
'clientId' =>
string(40) "2e9574ecd415c99346879d07689ec1c732c11036"
'sessionToken' =>
string(32) "8836c122c0483eb193ac2dd121136931"
}
```
To finalise the auth flow process we just need to create an auth object, and provide this `POST` data and your app's
'client secret' key found in the settings of your application in the HumanAPI developer portal.

```php
use Choccybiccy\HumanApi\Auth;

$clientSecret = "ee1551fb509598d0b656811633310889dc306aa3";

$auth = new Auth($_POST, $clientSecret);
$data = $auth->finish();
```
The response from the finish() method will look something like this:
```php
array(3) {
'humanId' =>
string(24) "52867cbede3155565f000a0d"
'accessToken' =>
string(40) "95891f14f4bcpa23261987effc7cfac7fedf7330"
'publicToken' =>
string(32) "2767d6oea95f4c3db8e8f3d0a1238302"
}
```
You should consider storing `humanId`, `accessToken` and `publicToken` against your user for later use.

## Human
Creating an instance of human requires the user's access token.
```php
<?php

use Choccybiccy\HumanApi\Human;

$human = new Human("myAccessToken");
Expand All @@ -17,8 +58,6 @@ From there you can begin querying the HumanAPI endpoints.
## Endpoints
Creating an endpoint instance only requires the endpoint name.
```php
<?php

use Choccybiccy\HumanApi\Human;

$human = new Human("myAccessToken");
Expand Down
35 changes: 12 additions & 23 deletions src/HumanApi/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Choccybiccy\HumanApi;

use SebastianBergmann\Exporter\Exception;

/**
* Class Auth
* @package Choccybiccy\HumanApi
Expand All @@ -22,7 +20,7 @@ class Auth extends Api
protected $apiVersion = 1;

/**
* @var stdClass
* @var array
*/
protected $sessionTokenObject;

Expand All @@ -31,42 +29,33 @@ class Auth extends Api
*
* @see https://docs.humanapi.co/docs/connect-backend
*
* @param string $sessionTokenObject
* @param array $sessionTokenData
* @param string $clientSecret
*/
public function __construct($sessionTokenObject, $clientSecret)
public function __construct(array $sessionTokenData, $clientSecret)
{

$sessionTokenObject = $this->decodeJson($sessionTokenObject);
$sessionTokenObject->clientSecret = $clientSecret;
$this->sessionTokenObject = $sessionTokenObject;
$sessionTokenData['clientSecret'] = $clientSecret;
$this->sessionTokenData = $sessionTokenData;

parent::__construct();

}

/**
* Finish the auth flow and post to connect endpoint
* Finish the auth flow and post to connect endpoint, and return response array
* containing accessToken and other data about the 'human' entity.
*
* @return array
*/
public function finish()
{
$response = $this->post(
$this->buildUrlParts(array("tokens"), "connect"),
array(
"json" => (array) $this->sessionTokenObject,
"json" => $this->sessionTokenData,
)
);
return $response->json();
}

/**
* 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);
}
}
}
73 changes: 73 additions & 0 deletions tests/HumanApi/AuthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Choccybiccy\HumanApi;

use Choccybiccy\HumanApi\Traits\ReflectionMethods;

/**
* Class AuthTest
* @package Choccybiccy\HumanApi
*/
class AuthTest extends \PHPUnit_Framework_TestCase
{

use ReflectionMethods;

/**
* Get mock response
*
* @param int $statusCode Response status code
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getMockResponse($statusCode = 204)
{
$response = $this->getMockBuilder('GuzzleHttp\Message\ResponseInterface')
->disableOriginalConstructor()
->setMethods(array("json", "getStatusCode", "getReasonPhrase"))
->getMockForAbstractClass();
$response->expects($this->any())
->method("json")
->willReturn(array());
$response->expects($this->any())
->method("getStatusCode")
->willReturn($statusCode);
return $response;
}

/**
* Test auth
*/
public function testAuth()
{

$sessionTokenData = array(
"humanId" => "exampleHumanId",
"clientId" => "exampleClientId",
"sessionToken" => "exampleSessionToken",
);

$clientSecret = "exampleClientSecret";

$auth = $this->getMockBuilder("Choccybiccy\\HumanApi\\Auth")
->setConstructorArgs(array($sessionTokenData, $clientSecret))
->setMethods(array("post"))
->getMock();

$auth->expects($this->once())
->method("post")
->with(
$this->runProtectedMethod(
$auth,
"buildUrlParts",
array(array("tokens"), "connect")
),
array(
"json" => array_merge($sessionTokenData, array("clientSecret" => $clientSecret)),
)
)
->willReturn($this->getMockResponse());

$auth->finish();

}
}

0 comments on commit 3e69dc0

Please sign in to comment.