Skip to content

Commit

Permalink
Fixed the refresh token retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
tharangakothalawala committed Nov 17, 2019
1 parent 68dba82 commit 1f9cfb1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 42 deletions.
46 changes: 4 additions & 42 deletions src/Auth/RedirectingLoginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,11 @@
use Hub\HubAPI\Service\Exception\HubIdApiException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Hub\HubAPI\Service\Service;
use Valitron\Validator;

class RedirectingLoginHelper
class RedirectingLoginHelper extends Service
{
const API_BASE_PATH = 'https://id.hubculture.com';
private $config;
private $client;

/**
* RedirectLoginHelper constructor.
*
* @param array $config
*
* @throws \Exception
*/
public function __construct(array $config)
{
$validator = new Validator($config);
$validator
->rule('required', ['private_key', 'public_key', 'client_id'])
->message('{field} - is required');
if (!$validator->validate()) {
throw new \Exception('fields: private_key, public_key & client_id are required');
}

$this->config = array_merge(
array(
'base_path' => self::API_BASE_PATH,
'verify' => true,
// https://hubculture.com/developer/home
'client_id' => 0,
'private_key' => '',
'public_key' => '',
),
$config
);

if ($this->config['verify'] === false) {
$this->client = new Client(array('verify' => false));
} else {
$this->client = new Client(array('verify' => true));
}
}

/**
* Use this to redirect the user to the Hub Culture login page.
*
Expand Down Expand Up @@ -90,7 +51,8 @@ public function getLoginUrl($callbackUrl)
*/
public function getRefreshToken($accessToken)
{
$response = $this->request(sprintf("/token?grant_type=refresh_token&token=%s", $accessToken));
$this->setAccessToken($accessToken);
$response = $this->put('/token');
if (!empty($response['error']) && $response['error'] === 'token_invalid') {
throw new HubIdApiException('Invalid access token provided');
}
Expand Down
10 changes: 10 additions & 0 deletions src/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public function __construct(array $config)
}
}

/**
* Use this to set an access token.
*
* @param string $accessToken
*/
public function setAccessToken($accessToken)
{
$this->config['token'] = $accessToken;
}

protected function get($api, array $params = array())
{
return $this->requestWithForm($api, 'get', $params);
Expand Down

0 comments on commit 1f9cfb1

Please sign in to comment.