Skip to content

Commit

Permalink
fix: refresh both tokens at once
Browse files Browse the repository at this point in the history
  • Loading branch information
hschoenenberger committed Feb 8, 2024
1 parent 623d83e commit a2f3432
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 52 deletions.
1 change: 1 addition & 0 deletions ps_accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Ps_accounts extends Module
\PrestaShop\Module\PsAccounts\Hook\ActionObjectShopDeleteBefore::class,
\PrestaShop\Module\PsAccounts\Hook\ActionObjectShopUpdateAfter::class,
\PrestaShop\Module\PsAccounts\Hook\ActionObjectShopUrlUpdateAfter::class,
\PrestaShop\Module\PsAccounts\Hook\ActionShopAccessTokenRefreshAfter::class,
\PrestaShop\Module\PsAccounts\Hook\ActionShopAccountLinkAfter::class,
\PrestaShop\Module\PsAccounts\Hook\ActionShopAccountUnlinkAfter::class,
\PrestaShop\Module\PsAccounts\Hook\DisplayAccountUpdateWarning::class,
Expand Down
1 change: 0 additions & 1 deletion src/Account/CommandHandler/UpdateUserShopHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use PrestaShop\Module\PsAccounts\Account\Session\Firebase\ShopSession;
use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient;
use PrestaShop\Module\PsAccounts\Context\ShopContext;
use PrestaShop\Module\PsAccounts\Account\LinkShop;

class UpdateUserShopHandler
{
Expand Down
4 changes: 2 additions & 2 deletions src/Account/LinkShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public function delete()
*/
public function update(Dto\LinkShop $payload)
{
$this->shopSession->setToken($payload->shopToken, $payload->shopRefreshToken);
$this->ownerSession->setToken($payload->userToken, $payload->userRefreshToken);
//$this->shopSession->setToken($payload->shopToken, $payload->shopRefreshToken);
//$this->ownerSession->setToken($payload->userToken, $payload->userRefreshToken);
$this->setEmployeeId((int) $payload->employeeId ?: null);
}

Expand Down
20 changes: 1 addition & 19 deletions src/Account/Session/Firebase/OwnerSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,7 @@ public function refreshToken($refreshToken)
{
$accessToken = $this->shopSession->getOrRefreshToken();

$response = $this->apiClient->firebaseTokens($accessToken->getJwt());

if ($response && true === $response['status']) {
// FIXME : strange to receive both tokens here
return new Token(
$response['body']['userToken']
// $response['body']['shopToken']
);
}

// if ($response['httpCode'] >= 400 && $response['httpCode'] < 500) {
// // TODO
// }

$errorMsg = isset($response['body']['message']) ?
$response['body']['message'] :
'';

throw new RefreshTokenException('Unable to refresh owner token : ' . $response['httpCode'] . ' ' . print_r($errorMsg, true));
return $this->getToken();
}

/**
Expand Down
20 changes: 1 addition & 19 deletions src/Account/Session/Firebase/ShopSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,7 @@ public function refreshToken($refreshToken)
{
$accessToken = $this->shopSession->getOrRefreshToken();

$response = $this->apiClient->firebaseTokens($accessToken->getJwt());

if ($response && true === $response['status']) {
// FIXME : strange to receive both tokens here
return new Token(
// $response['body']['userToken'],
$response['body']['shopToken']
);
}

// if ($response['httpCode'] >= 400 && $response['httpCode'] < 500) {
// // TODO
// }

$errorMsg = isset($response['body']['message']) ?
$response['body']['message'] :
'';

throw new RefreshTokenException('Unable to refresh shop token : ' . $response['httpCode'] . ' ' . print_r($errorMsg, true));
return $this->getToken();
}

/**
Expand Down
39 changes: 36 additions & 3 deletions src/Account/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ public function getOrRefreshToken($forceRefresh = false)
try {
$token = $this->refreshToken(null);
$this->setToken((string) $token->getJwt()/*, $token->getRefreshToken()*/);
} catch (RefreshTokenException $e) {
Logger::getInstance()->error($e->getMessage());
} catch (ConnectException $e) {
} catch (\Error $e) {
} catch (\Exception $e) {
// } catch (RefreshTokenException $e) {
// Logger::getInstance()->error($e->getMessage());
// } catch (ConnectException $e) {
// Logger::getInstance()->error($e->getMessage());
}
if (isset($e)) {
Logger::getInstance()->error($e->getMessage());
}
}
Expand Down Expand Up @@ -73,4 +78,32 @@ public function isEmailVerified()

return (bool) $jwt->claims()->get('email_verified');
}

/**
* @param string $name
* @param array $response
*
* @return void
*
* @throws RefreshTokenException
*/
public function refreshTokenFromResponse($name, $response)
{
//$response = $this->apiClient->getCachedResponse('firebaseTokens');
if (!isset($response)) {
return;
}

if ($response && true === $response['status']) {
$this->setToken($response['body'][$name]);

return;
}

$errorMsg = isset($response['body']['message']) ?
$response['body']['message'] :
'';

throw new RefreshTokenException('Unable to refresh ' . $name . ' token : ' . $response['httpCode'] . ' ' . print_r($errorMsg, true));
}
}
35 changes: 30 additions & 5 deletions src/Account/Session/ShopSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PrestaShop\Module\PsAccounts\Account\LinkShop;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Hook\ActionShopAccessTokenRefreshAfter;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Provider\OAuth2\ShopProvider;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
Expand Down Expand Up @@ -55,6 +56,29 @@ public function __construct(
$this->oauth2ClientProvider = $oauth2ClientProvider;
}

/**
* @param bool $forceRefresh
*
* @return Token
*
* @throws \Exception
*/
public function getOrRefreshToken($forceRefresh = false)
{
$token = parent::getOrRefreshToken($forceRefresh);

try {
\Hook::exec(ActionShopAccessTokenRefreshAfter::getName(), ['token' => $token]);
} catch (\Error $e) {
} catch (\Exception $e) {
}
if (isset($e)) {
Logger::getInstance()->error('Unable to get or refresh shop token : ' . $e->getMessage());
}

return $token;
}

/**
* @param string $refreshToken
*
Expand All @@ -64,15 +88,16 @@ public function __construct(
*/
public function refreshToken($refreshToken)
{
$shopUuid = $this->getShopUuid();

try {
$shopUuid = $this->getShopUuid();
$accessToken = $this->getAccessToken($shopUuid);

return new Token($accessToken->getToken(), $accessToken->getRefreshToken());
} catch (IdentityProviderException $e) {
throw new RefreshTokenException('Unable to refresh shop token : ' . $e->getMessage());
} catch (\Error $e) {
} catch (\Exception $e) {
}

return new Token($accessToken->getToken(), $accessToken->getRefreshToken());
throw new RefreshTokenException('Unable to refresh shop token : ' . $e->getMessage());
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Api/Client/AccountsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace PrestaShop\Module\PsAccounts\Api\Client;

use PrestaShop\Module\PsAccounts\Account\Dto\UpdateShop;
use PrestaShop\Module\PsAccounts\Account\LinkShop;
use PrestaShop\Module\PsAccounts\Factory\CircuitBreakerFactory;
use PrestaShop\Module\PsAccounts\Http\Client\CircuitBreaker\CircuitBreaker;
use PrestaShop\Module\PsAccounts\Http\Client\Guzzle\GuzzleClient;
Expand Down
54 changes: 54 additions & 0 deletions src/Hook/ActionShopAccessTokenRefreshAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PsAccounts\Hook;

use PrestaShop\Module\PsAccounts\Account\Session\Firebase\OwnerSession;
use PrestaShop\Module\PsAccounts\Account\Session\Firebase\ShopSession;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient;

class ActionShopAccessTokenRefreshAfter extends Hook
{
/**
* @param array $params
*
* @return void
*
* @throws \Exception
*/
public function execute(array $params = [])
{
/** @var Token $token */
$token = $params['token'];

/** @var AccountsClient $apiClient */
$apiClient = $this->ps_accounts->getService(AccountsClient::class);
$response = $apiClient->firebaseTokens($token);

/** @var OwnerSession $ownerSession */
$ownerSession = $this->ps_accounts->getService(OwnerSession::class);
$ownerSession->refreshTokenFromResponse('userToken', $response);

/** @var ShopSession $shopSession */
$shopSession = $this->ps_accounts->getService(ShopSession::class);
$shopSession->refreshTokenFromResponse('shopToken', $response);
}
}
1 change: 0 additions & 1 deletion src/Presenter/PsAccountsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public function __construct(
$this->linkShop = $module->getService(LinkShop::class);
$this->installer = $module->getService(Installer::class);
$this->configuration = $module->getService(ConfigurationRepository::class);

}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/OAuth2/ShopProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private function fetchWellKnown()
try {
$this->wellKnown = WellKnown::fetch(
$this->getParameter('ps_accounts.oauth2_url'),
(bool)$this->module->getParameter('ps_accounts.check_api_ssl_cert')
(bool) $this->module->getParameter('ps_accounts.check_api_ssl_cert')
);
} catch (\Exception $e) {
$this->wellKnown = new WellKnown([]);
Expand Down
18 changes: 18 additions & 0 deletions views/css/login.css

Large diffs are not rendered by default.

0 comments on commit a2f3432

Please sign in to comment.