Skip to content

Commit

Permalink
Merge pull request #376 from PrestaShopCorp/fix/5.x/call-only-refresh…
Browse files Browse the repository at this point in the history
…-token-once-by-instance

[ACCOUNT-2044] limit refresh token calls on error by request
  • Loading branch information
hschoenenberger authored Oct 16, 2023
2 parents efe518a + 57c119f commit 6b3ee6f
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions classes/Repository/AbstractTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ abstract class AbstractTokenRepository
*/
protected $tokenType;

/**
* @var array
*/
protected $refreshTokenErrors = [];

/**
* AbstractTokenRepository constructor.
*
Expand Down Expand Up @@ -106,18 +111,26 @@ abstract public function updateCredentials($idToken, $refreshToken);
*/
public function getOrRefreshToken($forceRefresh = false)
{
$refreshToken = $this->getRefreshToken();

if (!is_string($refreshToken) || '' === $refreshToken) {
return $this->getToken();
}

if ($this->getRefreshTokenErrors($refreshToken)) {
return $this->getToken();
}

if (true === $forceRefresh || $this->isTokenExpired()) {
$refreshToken = $this->getRefreshToken();
if (is_string($refreshToken) && '' != $refreshToken) {
try {
$token = $this->refreshToken($refreshToken, $newRefreshToken);
$this->updateCredentials(
(string) $token,
$newRefreshToken
);
} catch (RefreshTokenException $e) {
Logger::getInstance()->debug($e);
}
try {
$token = $this->refreshToken($refreshToken, $newRefreshToken);
$this->updateCredentials(
(string) $token,
$newRefreshToken
);
} catch (RefreshTokenException $e) {
$this->setRefreshTokenErrors($refreshToken);
Logger::getInstance()->debug($e);
}
}

Expand Down Expand Up @@ -246,4 +259,24 @@ protected function onMaxRefreshTokenAttempts()
$service->resetLinkAccount();
$this->configuration->updateShopUnlinkedAuto(true);
}

/**
* @param string $refreshToken
*
* @return bool
*/
protected function getRefreshTokenErrors($refreshToken)
{
return isset($this->refreshTokenErrors[$refreshToken]) && $this->refreshTokenErrors[$refreshToken];
}

/**
* @param string $refreshToken
*
* @return void
*/
protected function setRefreshTokenErrors($refreshToken)
{
$this->refreshTokenErrors[$refreshToken] = true;
}
}

0 comments on commit 6b3ee6f

Please sign in to comment.