-
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.
Merge branch 'feature/final_auth_flow' into develop
- Loading branch information
Showing
3 changed files
with
177 additions
and
4 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
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,61 @@ | ||
<?php | ||
|
||
namespace Choccybiccy\HumanApi; | ||
|
||
/** | ||
* Class Auth | ||
* @package Choccybiccy\HumanApi | ||
*/ | ||
class Auth extends Api | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $apiUrl = "https://user.humanapi.co"; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $apiVersion = 1; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $sessionTokenObject; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @see https://docs.humanapi.co/docs/connect-backend | ||
* | ||
* @param array $sessionTokenData | ||
* @param string $clientSecret | ||
*/ | ||
public function __construct(array $sessionTokenData, $clientSecret) | ||
{ | ||
|
||
$sessionTokenData['clientSecret'] = $clientSecret; | ||
$this->sessionTokenData = $sessionTokenData; | ||
|
||
parent::__construct(); | ||
|
||
} | ||
|
||
/** | ||
* 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" => $this->sessionTokenData, | ||
) | ||
); | ||
return $response->json(); | ||
} | ||
} |
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,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(); | ||
|
||
} | ||
} |