Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added autoRefreshSettings config option #23

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/OAuth/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class Client {
*/
protected $config = [];

/**
* @var int
*/
var $lastRefreshAttempt = null;

/**
* Create a new instance of Client.
*
Expand Down Expand Up @@ -123,13 +128,69 @@ public function __call($method, $args) {
$response = $e->getResponse();
$body = json_decode($response->getBody(), false);
$status_code = $response->getStatusCode();

if($status_code == 404 && !($this->config['404_error'] ?? false)) {
$response = new \stdClass;
$response->uri = $uri;
$response->error = "{$body->error}";
$response->code = $status_code;
return $response;
}

if (
$status_code == 401 &&
$body->error == "invalid_token" &&
isset( $this->config["autoRefreshSettings"] )
) {

/* let's only make an attempt if it's been at least one minute since the last attempt.
* If it's been less than a minute, chances are we just refreshed, and it *seemed* to
* work fine, but then our recursive call failed with another 401. It's unlikely that
* such an event will occur, which means that it will definitely occur. If we don't
* put some kind of check here, we could end up making infinite recursive calls.
*/
if (
!$this->lastRefreshAttempt ||
time() - $this->lastRefreshAttempt > 60
) {

$this->lastRefreshAttempt = time();

/* 2023-04-07: refreshAccessToken either works, or errors out. So checking for a valid
* response isn't all that useful, and we can never call a provided onFail function.
* But let's do it anyway in case refreshAccessToken is ever modified to handle errors differently
*/
$response = $this->refreshAccessToken( $this->config["autoRefreshSettings"]["refreshToken"] );

if (
$response &&
isset( $response["access_token"] ) &&
isset( $response["refresh_token"] )
) {
// success
$this->setApiKey( $response["access_token"] );

if (
isset( $this->config["autoRefreshSettings"]["onSuccess"] ) &&
is_callable( $this->config["autoRefreshSettings"]["onSuccess"] )
) {
$this->config["autoRefreshSettings"]["onSuccess"]($response);
}

// ok let's make a recursive call
return $this->__call( $method, $args );
} else {
// failure
if (
isset( $this->config["autoRefreshSettings"]["onFail"] ) &&
is_callable( $this->config["autoRefreshSettings"]["onFail"] )
) {
$this->config["autoRefreshSettings"]["onFail"]($response);
}
}
}
}

throw new RequestException(
"Received HTTP status code [$status_code] with error \"{$body->error}\"."
);
Expand Down