Skip to content

Commit

Permalink
Add direct access token auth (#11)
Browse files Browse the repository at this point in the history
* Add direct access token auth

* Rename method, update credentials validation
  • Loading branch information
ionutcalara authored Aug 27, 2020
1 parent 13698d8 commit 7699b1a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function __construct($endpointUri, array $credentials, $container) {
$this->endpointUri = $endpointUri;
$this->container = $container;
$credentials = $this->removeNullValuesFromArray($credentials);
$this->credentials = self::validateCredentials($credentials);
$this->credentials = self::validateCredentials($credentials);
$this->initializeAccessToken();
}

private function removeNullValuesFromArray(array $collectionToFilter) {
Expand Down Expand Up @@ -89,6 +90,12 @@ private static function validateCredentials($credentials) {
return $credentials;
}

private function initializeAccessToken(){
if(array_key_exists("access_token", $this->credentials)) {
$this->tokenDetails = $this->credentials['access_token'];
}
}

/**
* Returns the api key for the configured credentials
*
Expand Down
28 changes: 22 additions & 6 deletions src/GettyImages_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,26 @@ class GettyImages_Client {
* @param null $password
* @param null $refreshToken
* @param null $container
* @param null $accessToken
* @example UsageExamples.php Examples
*/
private function __construct($apiKey, $apiSecret, $username = null, $password = null, $refreshToken = null, $container) {
private function __construct($apiKey, $apiSecret, $username = null, $password = null, $refreshToken = null, $container, $accessToken = null) {

$credentials = array(
"client_key" => $apiKey,
"client_secret" => $apiSecret,
"username" => $username,
"password" => $password,
"refresh_token" => $refreshToken);
"refresh_token" => $refreshToken,
"access_token" => $accessToken);

if($container == null)
{
$builder = new \DI\ContainerBuilder();
$this->container = $builder->build();
$this->container->set('ICurler', \DI\Object(Curler\Curler::Class));
}
else
else
{
$this->container = $container;
}
Expand All @@ -118,7 +120,7 @@ public static function getClientWithClientCredentials($apiKey, $apiSecret, $cont
{
return new GettyImages_Client($apiKey, $apiSecret, null, null, null, $container);
}

/**
* Get client using resource owner credentials
*
Expand Down Expand Up @@ -146,6 +148,20 @@ public static function getClientWithRefreshToken($apiKey, $apiSecret, $refreshTo
return new GettyImages_Client($apiKey, $apiSecret, null, null, $refreshToken, $container);
}

/**
* Get client using access token and refresh token
*
* @param null $apiKey
* @param null $apiSecret
* @param null $accessToken
* @param null $refreshToken
* @param null $container
*/
public static function getClientWithAccessToken($apiKey, $apiSecret, $accessToken, $refreshToken, $container = null)
{
return new GettyImages_Client($apiKey, $apiSecret, null, null, $refreshToken, $container, $accessToken);
}

/**
* Retrieves a authentication token for configured credentials
*/
Expand Down Expand Up @@ -247,8 +263,8 @@ public function DownloadVideo() {

/**
* Events
*
* Get metadata fro events
*
* Get metadata fro events
*/
public function Events() {
$eventsObj = new Events($this->credentials,$this->apiBaseUri,$this->container);
Expand Down
20 changes: 19 additions & 1 deletion unitTests/CredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ public function testGetClientWithRefreshTokenTest()

$this->assertContains("test_token", $curlerMock->options[CURLOPT_HTTPHEADER][1]);
}
}

public function testGetClientWithAccessTokenTest()
{
$curlerMock = new CurlerMock();
$builder = new \DI\ContainerBuilder();
$container = $builder->build();
$container->set('ICurler', $curlerMock);

$client = GettyImages_Client::getClientWithAccessToken("", "", array(
"access_token" => "test_token",
"token_type" => "Bearer",
"sdk_expire_time"=> time() + 1476
), "", $container);

$response = $client->Collections()->execute();

$this->assertContains("test_token", $curlerMock->options[CURLOPT_HTTPHEADER][1]);
}
}

0 comments on commit 7699b1a

Please sign in to comment.