Skip to content

Commit

Permalink
Merge pull request #343 from PrestaShopCorp/feat/add-requests-signature
Browse files Browse the repository at this point in the history
feat: add api requests signature
  • Loading branch information
emmanuelgautier authored Jun 5, 2023
2 parents e55f546 + 07a718a commit def3f80
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ install.lock
composer-dev.json
composer-dev.lock

### PHPUnit ###
.phpunit.result.cache

### Configuration ###
config/config*.yml
config/**/services_*.yml
Expand Down
61 changes: 42 additions & 19 deletions src/Api/Client/AccountsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/
class AccountsClient implements TokenClientInterface
{
/**
* @var string
*/
private $apiUrl;

/**
* @var ShopProvider
*/
Expand All @@ -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;
}

/**
Expand All @@ -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,
],
]);
Expand All @@ -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,
],
]);
Expand All @@ -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,
]),
]);
});
Expand All @@ -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,
]);
Expand Down Expand Up @@ -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(),
]);
Expand All @@ -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);
}

Expand Down
32 changes: 24 additions & 8 deletions src/Api/Client/SsoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
*/
class SsoClient implements TokenClientInterface
{
/**
* @var string
*/
private $apiUrl;

/**
* @var AbstractGuzzleClient
*/
Expand All @@ -44,18 +49,29 @@ 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',
'X-Module-Version' => \Ps_accounts::VERSION,
'X-Prestashop-Version' => _PS_VERSION_,
],
],
]);
}

$this->client = $client;
return $this->client;
}

/**
Expand All @@ -65,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,
],
Expand All @@ -81,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,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit def3f80

Please sign in to comment.