Skip to content

Commit

Permalink
Merge pull request #348 from PrestaShopCorp/feat/5.x/rotate-refresh-t…
Browse files Browse the repository at this point in the history
…oken

feat: update refresh token returned by firebase
  • Loading branch information
hschoenenberger authored Jun 19, 2023
2 parents 8a54bb6 + 8ff9563 commit f58d408
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
11 changes: 7 additions & 4 deletions classes/Repository/AbstractTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ abstract class AbstractTokenRepository

const TOKEN_TYPE = '';
const TOKEN_KEY = '';
const REFRESH_TOKEN_KEY = '';

/**
* @var ConfigurationRepository
Expand Down Expand Up @@ -109,10 +110,10 @@ public function getOrRefreshToken($forceRefresh = false)
$refreshToken = $this->getRefreshToken();
if (is_string($refreshToken) && '' != $refreshToken) {
try {
$token = $this->refreshToken($refreshToken);
$token = $this->refreshToken($refreshToken, $newRefreshToken);
$this->updateCredentials(
(string) $token,
$refreshToken
$newRefreshToken
);
} catch (RefreshTokenException $e) {
Logger::getInstance()->debug($e);
Expand Down Expand Up @@ -151,18 +152,20 @@ public function parseToken($token)

/**
* @param string $refreshToken
* @param string $newRefreshToken
*
* @return Token|null idToken
* @return Token|null
*
* @throws RefreshTokenException
* @throws Exception
*/
public function refreshToken($refreshToken)
public function refreshToken($refreshToken, &$newRefreshToken = null)
{
$response = $this->client()->refreshToken($refreshToken);

if ($response && true === $response['status']) {
$token = $this->parseToken($response['body'][static::TOKEN_KEY]);
$newRefreshToken = $response['body'][static::REFRESH_TOKEN_KEY];

$this->onRefreshTokenSuccess();

Expand Down
1 change: 1 addition & 0 deletions classes/Repository/ShopTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ShopTokenRepository extends AbstractTokenRepository
{
const TOKEN_TYPE = 'shop';
const TOKEN_KEY = 'token';
const REFRESH_TOKEN_KEY = 'refresh_token';

/**
* @return AccountsClient
Expand Down
1 change: 1 addition & 0 deletions classes/Repository/UserTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class UserTokenRepository extends AbstractTokenRepository
{
const TOKEN_TYPE = 'user';
const TOKEN_KEY = 'idToken';
const REFRESH_TOKEN_KEY = 'refreshToken';

/**
* @return SsoClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,49 @@ public function itShouldRefreshExpiredToken()

$this->assertEquals((string) $idTokenRefreshed, (string) $tokenRepos->getOrRefreshToken());
}

/**
* @test
* @throws \Exception
*/
public function itShouldUpdateRefreshToken()
{
$payload = [
'token' => $this->makeJwtToken(new \DateTimeImmutable('yesterday'), [
'user_id' => $this->faker->uuid,
]),
'refresh_token' => $this->makeJwtToken(new \DateTimeImmutable('+1 year')),
];

$client = $this->createMock(AccountsClient::class);
$client->method('refreshToken')->willReturn($payload);

/** @var ConfigurationRepository $configuration */
$configuration = $this->module->getService(ConfigurationRepository::class);

/** @var ShopTokenRepository $tokenRepos */
$tokenRepos = $this->getMockBuilder(ShopTokenRepository::class)
->setConstructorArgs([$configuration])
//->disableOriginalConstructor()
//->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();

$tokenRepos->method('client')
->willReturn($client);

$tokenRepos->updateCredentials(
$this->makeJwtToken(new \DateTimeImmutable('yesterday'), [
'user_id' => $this->faker->uuid,
'email' => $this->faker->safeEmail,
]),
$this->makeJwtToken(new \DateTimeImmutable('+1 year'))
);

$tokenRepos->getOrRefreshToken();

$this->assertEquals($payload['token'], $tokenRepos->getToken());
$this->assertEquals($payload['refresh_token'], $tokenRepos->getRefreshToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PrestaShop\Module\PsAccounts\Tests\Unit\Repository\UserTokenRepository;

use PrestaShop\Module\PsAccounts\Api\Client\SsoClient;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
use PrestaShop\Module\PsAccounts\Repository\UserTokenRepository;
use PrestaShop\Module\PsAccounts\Tests\TestCase;
Expand Down Expand Up @@ -61,4 +62,49 @@ public function itShouldRefreshExpiredToken()

$this->assertEquals((string) $idTokenRefreshed, $tokenRepos->getOrRefreshToken());
}

/**
* @test
* @throws \Exception
*/
public function itShouldUpdateRefreshToken()
{
$payload = [
'idToken' => $this->makeJwtToken(new \DateTimeImmutable('tomorrow'), [
'user_id' => $this->faker->uuid,
]),
'refreshToken' => $this->makeJwtToken(new \DateTimeImmutable('+1 year')),
];

$client = $this->createMock(SsoClient::class);
$client->method('refreshToken')->willReturn($payload);

/** @var ConfigurationRepository $configuration */
$configuration = $this->module->getService(ConfigurationRepository::class);

/** @var UserTokenRepository $tokenRepos */
$tokenRepos = $this->getMockBuilder(UserTokenRepository::class)
->setConstructorArgs([$configuration])
//->disableOriginalConstructor()
//->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();

$tokenRepos->method('client')
->willReturn($client);

$tokenRepos->updateCredentials(
$this->makeJwtToken(new \DateTimeImmutable('yesterday'), [
'user_id' => $this->faker->uuid,
'email' => $this->faker->safeEmail,
]),
$this->makeJwtToken(new \DateTimeImmutable('+1 year'))
);

$tokenRepos->getOrRefreshToken();

$this->assertEquals($payload['idToken'], $tokenRepos->getToken());
$this->assertEquals($payload['refreshToken'], $tokenRepos->getRefreshToken());
}
}

0 comments on commit f58d408

Please sign in to comment.