From 3cde811b9d985e0f1bb64e1773d099ce09b5c25f Mon Sep 17 00:00:00 2001 From: Emmanuel Gautier Date: Fri, 26 May 2023 19:03:50 +0200 Subject: [PATCH 1/2] feat: add api requests signature --- .gitignore | 3 + src/Api/Client/AccountsClient.php | 61 +++++++++++++------ src/Api/Client/SsoClient.php | 2 + .../GetOrRefreshTokenTest.php | 1 - .../GetTokenEmailVerifiedTest.php | 1 - 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 063827353..2185f7c3a 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,9 @@ install.lock composer-dev.json composer-dev.lock +### PHPUnit ### +.phpunit.result.cache + ### Configuration ### config/config*.yml config/**/services_*.yml diff --git a/src/Api/Client/AccountsClient.php b/src/Api/Client/AccountsClient.php index de0de5ee0..ae27e675d 100644 --- a/src/Api/Client/AccountsClient.php +++ b/src/Api/Client/AccountsClient.php @@ -34,6 +34,11 @@ */ class AccountsClient implements TokenClientInterface { + /** + * @var string + */ + private $apiUrl; + /** * @var ShopProvider */ @@ -56,15 +61,26 @@ public function __construct( ShopProvider $shopProvider, AbstractGuzzleClient $client = null ) { + $this->apiUrl = $apiUrl; $this->shopProvider = $shopProvider; + $this->client = $client; + } - if (null === $client) { - $client = (new GuzzleClientFactory())->create([ - 'base_url' => $apiUrl, + /** + * @return AbstractGuzzleClient + */ + private function getClient() + { + if (null === $this->client) { + $this->client = (new GuzzleClientFactory())->create([ + 'base_url' => $this->apiUrl, + 'defaults' => [ + 'headers' => $this->getHeaders(), + ], ]); } - $this->client = $client; + return $this->client; } /** @@ -74,11 +90,13 @@ public function __construct( */ public function verifyToken($idToken) { - $this->client->setRoute('shop/token/verify'); + $this->getClient()->setRoute('shop/token/verify'); - return $this->client->post([ + return $this->getClient()->post([ 'json' => [ - 'headers' => $this->getHeaders(), + 'headers' => $this->getHeaders([ + 'X-Shop-Id' => $this->shopProvider->getCurrentShop()['id'], + ]), 'token' => $idToken, ], ]); @@ -91,11 +109,13 @@ public function verifyToken($idToken) */ public function refreshToken($refreshToken) { - $this->client->setRoute('shop/token/refresh'); + $this->getClient()->setRoute('shop/token/refresh'); - return $this->client->post([ + return $this->getClient()->post([ 'json' => [ - 'headers' => $this->getHeaders(), + 'headers' => $this->getHeaders([ + 'X-Shop-Id' => $this->shopProvider->getCurrentShop()['id'], + ]), 'token' => $refreshToken, ], ]); @@ -110,15 +130,16 @@ public function refreshToken($refreshToken) */ public function deleteUserShop($shopId) { - return $this->shopProvider->getShopContext()->execInShopContext((int) $shopId, function () { + return $this->shopProvider->getShopContext()->execInShopContext((int) $shopId, function () use ($shopId) { $userToken = $this->getUserTokenRepository(); $shopToken = $this->getShopTokenRepository(); - $this->client->setRoute('user/' . $userToken->getTokenUuid() . '/shop/' . $shopToken->getTokenUuid()); + $this->getClient()->setRoute('user/' . $userToken->getTokenUuid() . '/shop/' . $shopToken->getTokenUuid()); - return $this->client->delete([ + return $this->getClient()->delete([ 'headers' => $this->getHeaders([ 'Authorization' => 'Bearer ' . $userToken->getOrRefreshToken(), + 'X-Shop-Id' => $shopId, ]), ]); }); @@ -136,12 +157,12 @@ public function reonboardShop($currentShop) return $this->shopProvider->getShopContext()->execInShopContext((int) $currentShop['id'], function () use ($currentShop) { $shopToken = $this->getShopTokenRepository(); - $this->client->setRoute('shop/' . $currentShop['uuid'] . '/reonboard'); + $this->getClient()->setRoute('shop/' . $currentShop['uuid'] . '/reonboard'); - return $this->client->post([ + return $this->getClient()->post([ 'headers' => $this->getHeaders([ 'Authorization' => 'Bearer ' . $shopToken->getOrRefreshToken(), - 'content-type' => 'application/json', + 'X-Shop-Id' => $currentShop['id'], ]), 'json' => $currentShop, ]); @@ -171,12 +192,12 @@ public function updateUserShop(UpdateShop $shop) return null; } - $this->client->setRoute('user/' . $userToken->getTokenUuid() . '/shop/' . $shopToken->getTokenUuid()); + $this->getClient()->setRoute('user/' . $userToken->getTokenUuid() . '/shop/' . $shopToken->getTokenUuid()); - return $this->client->patch([ + return $this->getClient()->patch([ 'headers' => $this->getHeaders([ 'Authorization' => 'Bearer ' . $userToken->getOrRefreshToken(), - 'content-type' => 'application/json', + 'X-Shop-Id' => $shop->shopId, ]), 'json' => $shop->jsonSerialize(), ]); @@ -192,6 +213,8 @@ private function getHeaders($additionalHeaders = []) { return array_merge([ 'Accept' => 'application/json', + 'X-Module-Version' => \Ps_accounts::VERSION, + 'X-Prestashop-Version' => _PS_VERSION_, ], $additionalHeaders); } diff --git a/src/Api/Client/SsoClient.php b/src/Api/Client/SsoClient.php index 56eefa8d8..b0d358d6d 100644 --- a/src/Api/Client/SsoClient.php +++ b/src/Api/Client/SsoClient.php @@ -50,6 +50,8 @@ public function __construct( 'defaults' => [ 'headers' => [ 'Accept' => 'application/json', + 'X-Module-Version' => \Ps_accounts::VERSION, + 'X-Prestashop-Version' => _PS_VERSION_, ], ], ]); diff --git a/tests/Unit/Repository/UserTokenRepository/GetOrRefreshTokenTest.php b/tests/Unit/Repository/UserTokenRepository/GetOrRefreshTokenTest.php index 18a1a4064..27a9b4a82 100644 --- a/tests/Unit/Repository/UserTokenRepository/GetOrRefreshTokenTest.php +++ b/tests/Unit/Repository/UserTokenRepository/GetOrRefreshTokenTest.php @@ -2,7 +2,6 @@ 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; diff --git a/tests/Unit/Repository/UserTokenRepository/GetTokenEmailVerifiedTest.php b/tests/Unit/Repository/UserTokenRepository/GetTokenEmailVerifiedTest.php index 26603bddd..d4b573c05 100644 --- a/tests/Unit/Repository/UserTokenRepository/GetTokenEmailVerifiedTest.php +++ b/tests/Unit/Repository/UserTokenRepository/GetTokenEmailVerifiedTest.php @@ -2,7 +2,6 @@ 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; From 07a718a823271aebb5e09b0f5437c9c73d708a21 Mon Sep 17 00:00:00 2001 From: Emmanuel Gautier Date: Mon, 5 Jun 2023 10:04:31 +0200 Subject: [PATCH 2/2] feat: add lazy client for SsoClient as well --- src/Api/Client/SsoClient.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Api/Client/SsoClient.php b/src/Api/Client/SsoClient.php index b0d358d6d..010b321ea 100644 --- a/src/Api/Client/SsoClient.php +++ b/src/Api/Client/SsoClient.php @@ -29,6 +29,11 @@ */ class SsoClient implements TokenClientInterface { + /** + * @var string + */ + private $apiUrl; + /** * @var AbstractGuzzleClient */ @@ -44,9 +49,18 @@ public function __construct( $apiUrl, AbstractGuzzleClient $client = null ) { - if (null === $client) { - $client = (new GuzzleClientFactory())->create([ - 'base_url' => $apiUrl, + $this->apiUrl = $apiUrl; + $this->client = $client; + } + + /** + * @return AbstractGuzzleClient + */ + private function getClient() + { + if (null === $this->client) { + $this->client = (new GuzzleClientFactory())->create([ + 'base_url' => $this->apiUrl, 'defaults' => [ 'headers' => [ 'Accept' => 'application/json', @@ -57,7 +71,7 @@ public function __construct( ]); } - $this->client = $client; + return $this->client; } /** @@ -67,9 +81,9 @@ public function __construct( */ public function verifyToken($idToken) { - $this->client->setRoute('auth/token/verify'); + $this->getClient()->setRoute('auth/token/verify'); - return $this->client->post([ + return $this->getClient()->post([ 'json' => [ 'token' => $idToken, ], @@ -83,9 +97,9 @@ public function verifyToken($idToken) */ public function refreshToken($refreshToken) { - $this->client->setRoute('auth/token/refresh'); + $this->getClient()->setRoute('auth/token/refresh'); - return $this->client->post([ + return $this->getClient()->post([ 'json' => [ 'token' => $refreshToken, ],