Skip to content

Commit

Permalink
Token retry (#2)
Browse files Browse the repository at this point in the history
* support clearing tokens, retry

* clear tokens on connection and endpoint

* correctly delete token file
  • Loading branch information
TomK committed Mar 30, 2020
1 parent 067f422 commit 222db27
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 38 deletions.
13 changes: 13 additions & 0 deletions src/ApiEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ function () {
);
}

public function clearToken()
{
$connection = $this->_getConnection();
if($connection)
{
$connection->clearToken();
}
if($this->_grant !== null)
{
$this->getTokenStorage()->clearToken($this->_grant->getKey());
}
}

public function setApiDefinition(IApiDefinition $definition)
{
$this->_definition = $definition;
Expand Down
78 changes: 40 additions & 38 deletions src/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Fortifi\Api\Core\Exceptions\ApiException;
use Fortifi\Api\Core\Exceptions\Client\ClientApiException;
use Fortifi\Api\Core\Exceptions\Client\ForbiddenException;
use Packaged\Helpers\RetryHelper;
use Packaged\Helpers\ValueAs;

class ApiRequest implements IApiRequest
Expand Down Expand Up @@ -57,58 +58,59 @@ public function setRawResult(IApiResult $result)
return $this;
}

/**
* @return IApiResult
* @throws ClientApiException
* @throws ForbiddenException
*/
protected function _getRawResult()
{
if($this->hasConnection())
{
if($this->_requestDetail->requiresAuth())
{
$this->_connection->setToken($this->_endpoint->getToken());
$result = $this->_connection->load($this);
if($result->getRawResult()->getStatusCode() == 403)
{
$msg = $result->getRawResult()->getStatusMessage();
throw new ForbiddenException($msg == 'OK' ? 'Invalid token' : $msg);
}
return $result->getRawResult();
}

return $this->_connection->load($this)->getRawResult();
}
throw new ClientApiException("No API Connection Available", 428);
}

/**
* @inheritdoc
*/
public function getRawResult()
{
if($this->_result === null)
{
if($this->hasConnection())
try
{
if($this->_requestDetail->requiresAuth())
{
$this->_getConnection()->setToken($this->_endpoint->getToken());
try
{
$this->_result = false;
$result = $this->_getConnection()->load($this);
if($result->getRawResult()->getStatusCode() == 403)
{
$msg = $result->getRawResult()->getStatusMessage();
$this->_result = new ForbiddenException(
$msg == 'OK' ? 'Invalid token' : $msg
);
throw $this->_result;
}
}
catch(\Exception $e)
{
$this->_result = RetryHelper::retry(
1,
function () {
return $this->_getRawResult();
},
function (\Exception $e) {
if($e->getCode() == 403 && stristr($e->getMessage(), 'token'))
{
$result = $this->_getConnection()->load($this);
}
else
{
$this->_result = ApiException::build(
$e->getCode(),
$e->getMessage(),
$e
);
throw $this->_result;
$this->_connection->clearToken();
$this->_endpoint->clearToken();
}
return true;
}
}
else
{
$result = $this->_getConnection()->load($this);
}

$this->setRawResult($result->getRawResult());
);
}
else
catch(\Exception $e)
{
throw new ClientApiException("No API Connection Available", 428);
$this->_result = ApiException::build($e->getCode(), $e->getMessage(), $e);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Connections/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function setToken(IToken $token)
return $this;
}

public function clearToken()
{
$this->_token = null;
return $this;
}

protected function _buildHeaders(IApiRequestDetail $request)
{
$headers = $request->getHeaders();
Expand Down
5 changes: 5 additions & 0 deletions src/IApiConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public function setOrganisationFid($fid);
* @return $this
*/
public function setToken(IToken $token);

/**
* @return $this
*/
public function clearToken();
}
2 changes: 2 additions & 0 deletions src/IApiEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public function setApiDefinition(IApiDefinition $definition);
* @return OAuth\Tokens\IToken|null
*/
public function getToken();

public function clearToken();
}
7 changes: 7 additions & 0 deletions src/OAuth/TokenStorage/ITokenStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ public function storeToken($key, IToken $token);
* @return IToken|null
*/
public function retrieveToken($key, callable $retrieve = null);

/**
* Clear the token, forcing revalidation
*
* @param string $key location key for token
*/
public function clearToken($key);
}
5 changes: 5 additions & 0 deletions src/OAuth/TokenStorage/MemcachedTokenStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ public function retrieveToken($key, callable $retrieve = null)

return $token instanceof IToken ? $token : null;
}

public function clearToken($key)
{
$this->_connection->delete($this->_cacheKey($key));
}
}
6 changes: 6 additions & 0 deletions src/OAuth/TokenStorage/TempFileTokenStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public function retrieveToken($key, callable $retrieve = null)
return $token instanceof IToken ? $token : null;
}

public function clearToken($key)
{
$location = $this->_createFileName($key);
unlink($location);
}

/**
* Create a temporary filename
*
Expand Down

0 comments on commit 222db27

Please sign in to comment.