add nonce fix also to nonce generation

This commit is contained in:
David Remer 2020-07-10 12:33:13 +03:00
parent 34f0836c15
commit cdfb405fe4
3 changed files with 23 additions and 4 deletions

View file

@ -52,6 +52,7 @@ return [
$payeeRepository = $container->get('api.repository.payee');
$identityToken = $container->get('api.endpoint.identity-token');
$payerFactory = $container->get('api.factory.payer');
$requestData = $container->get('button.request-data');
$clientId = $container->get('button.client_id');
return new SmartButton(
@ -61,7 +62,8 @@ return [
$payeeRepository,
$identityToken,
$payerFactory,
$clientId
$clientId,
$requestData
);
},
'button.url' => static function (ContainerInterface $container): string {

View file

@ -11,6 +11,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
@ -23,6 +24,7 @@ class SmartButton implements SmartButtonInterface
private $identityToken;
private $payerFactory;
private $clientId;
private $requestData;
public function __construct(
string $moduleUrl,
@ -31,7 +33,8 @@ class SmartButton implements SmartButtonInterface
PayeeRepository $payeeRepository,
IdentityToken $identityToken,
PayerFactory $payerFactory,
string $clientId
string $clientId,
RequestData $requestData
) {
$this->moduleUrl = $moduleUrl;
@ -41,6 +44,7 @@ class SmartButton implements SmartButtonInterface
$this->identityToken = $identityToken;
$this->payerFactory = $payerFactory;
$this->clientId = $clientId;
$this->requestData = $requestData;
}
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
@ -201,6 +205,7 @@ class SmartButton implements SmartButtonInterface
private function localizeScript(): array
{
$this->requestData->enqueueNonceFix();
$localize = [
'script_attributes' => $this->attributes(),
'redirect' => wc_get_checkout_url(),
@ -242,6 +247,8 @@ class SmartButton implements SmartButtonInterface
],
],
];
$this->requestData->dequeueNonceFix();
return $localize;
}

View file

@ -9,11 +9,21 @@ use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
class RequestData
{
public function enqueueNonceFix()
{
add_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100);
}
public function dequeueNonceFix()
{
remove_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100);
}
public function readRequest(string $nonce): array
{
$stream = file_get_contents('php://input');
$json = json_decode($stream, true);
add_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100);
$this->enqueueNonceFix();
if (
! isset($json['nonce'])
|| !wp_verify_nonce($json['nonce'], $nonce)
@ -23,7 +33,7 @@ class RequestData
__('Could not validate nonce.', 'woocommerce-paypal-commerce-gateway')
);
}
remove_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100);
$this->dequeueNonceFix();
return $this->sanitize($json);
}