Skip to content

Commit

Permalink
fix: json post
Browse files Browse the repository at this point in the history
  • Loading branch information
hschoenenberger committed Jan 9, 2025
1 parent 8aab1e7 commit 19f7e8f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
4 changes: 4 additions & 0 deletions ps_accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ public function getContent()
/** @var \PrestaShop\Module\PsAccounts\Presenter\PsAccountsPresenter $psAccountsPresenter */
$psAccountsPresenter = $this->getService(\PrestaShop\Module\PsAccounts\Presenter\PsAccountsPresenter::class);

/** @var \PrestaShop\Module\PsAccounts\Provider\RsaKeysProvider $rsaKeysProvider */
$rsaKeysProvider = $this->getService(\PrestaShop\Module\PsAccounts\Provider\RsaKeysProvider::class);
$rsaKeysProvider->getOrGenerateAccountsRsaPublicKey();

Media::addJsDef([
'contextPsAccounts' => $psAccountsPresenter->present((string) $this->name),
]);
Expand Down
4 changes: 4 additions & 0 deletions src/Api/Controller/AbstractRestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,15 @@ protected function decodePayload($defaultShopId = null)
$this->setContextShop($shop);
$publicKey = $shopKeysService->getPublicKey();

$this->module->getLogger()->debug('trying to verify token with pkey: ' . $publicKey);

if (
!empty($publicKey) &&
is_string($publicKey) &&
true === $jwt->verify(new Sha256(), new Key((string) $publicKey))
) {
$this->module->getLogger()->debug('token verified: ' . $jwtString);

return $jwt->claims()->all();
}
$this->module->getLogger()->error('Failed to verify token: ' . $jwtString);
Expand Down
3 changes: 2 additions & 1 deletion src/Hook/ActionShopAccountUnlinkAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function execute(array $params = [])
$rsaKeysProvider = $this->module->getService(RsaKeysProvider::class);
try {
$rsaKeysProvider->cleanupKeys();
$rsaKeysProvider->generateKeys();
// FIXME: We MUST generate a new key here to avoid concurrent key re-generation at Presenter display time
//$rsaKeysProvider->generateKeys();
} catch (\Exception $e) {
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/Provider/RsaKeysProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,12 @@ public function generateKeys($refresh = false)
}

/**
* @return string|bool|null
* @return string|null
*/
public function getOrGenerateAccountsRsaPublicKey()
{
$publicKey = $this->getPublicKey();
if ($publicKey) {
return $publicKey;
}

try {
$this->regenerateKeys();

$this->generateKeys();
return $this->getPublicKey();
} catch (\Exception $e) {
return null;
Expand All @@ -164,15 +158,15 @@ public function regenerateKeys()
*/
public function hasKeys()
{
return false === empty($this->configuration->getAccountsRsaPublicKey());
return false === empty($this->configuration->getAccountsRsaPublicKey(false, false));
}

/**
* @return string|bool
*/
public function getPublicKey()
{
return $this->configuration->getAccountsRsaPublicKey();
return $this->configuration->getAccountsRsaPublicKey(false, false);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Provider/ShopProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public function formatShopData(array $shopData, $psxName = '', $refreshTokens =

// LinkAccount
'uuid' => $linkShop->getShopUuid() ?: null,
'publicKey' => $rsaKeyProvider->getOrGenerateAccountsRsaPublicKey() ?: null,
//'publicKey' => $rsaKeyProvider->getOrGenerateAccountsRsaPublicKey() ?: null,
'publicKey' => $rsaKeyProvider->getPublicKey() ?: null,
'employeeId' => (int) $linkShop->getEmployeeId() ?: null,
'user' => [
'email' => $linkShop->getOwnerEmail() ?: null,
Expand Down
7 changes: 5 additions & 2 deletions src/Repository/ConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,14 @@ public function updateAccountsRsaPrivateKey($key)
}

/**
* @param bool $cached
* @param bool|mixed $default
*
* @return string|bool
*/
public function getAccountsRsaPublicKey()
public function getAccountsRsaPublicKey($default = false, $cached = false)
{
return $this->configuration->get(ConfigurationKeys::PS_ACCOUNTS_RSA_PUBLIC_KEY);
return $this->configuration->get(ConfigurationKeys::PS_ACCOUNTS_RSA_PUBLIC_KEY, $default, $cached);
}

/**
Expand Down

0 comments on commit 19f7e8f

Please sign in to comment.