Add tokens cache

This commit is contained in:
Emili Castells Guasch 2024-08-06 10:44:54 +02:00
parent bea17db588
commit 54657ed649
7 changed files with 107 additions and 28 deletions

View file

@ -1657,7 +1657,7 @@ return array(
return new PurchaseUnitSanitizer( $behavior, $line_name ); return new PurchaseUnitSanitizer( $behavior, $line_name );
} }
), ),
'api.client-credentials' => static function(ContainerInterface $container): ClientCredentials { 'api.client-credentials' => static function( ContainerInterface $container ): ClientCredentials {
return new ClientCredentials( return new ClientCredentials(
$container->get( 'wcgateway.settings' ) $container->get( 'wcgateway.settings' )
); );
@ -1666,14 +1666,16 @@ return array(
return new UserIdToken( return new UserIdToken(
$container->get( 'api.host' ), $container->get( 'api.host' ),
$container->get( 'woocommerce.logger.woocommerce' ), $container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.client-credentials' ) $container->get( 'api.client-credentials' ),
new Cache( 'ppcp-client-credentials-cache' )
); );
}, },
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken { 'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {
return new SdkClientToken( return new SdkClientToken(
$container->get( 'api.host' ), $container->get( 'api.host' ),
$container->get( 'woocommerce.logger.woocommerce' ), $container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.client-credentials' ) $container->get( 'api.client-credentials' ),
new Cache( 'ppcp-client-credentials-cache' )
); );
}, },
); );

View file

@ -11,6 +11,7 @@ use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WP_Error; use WP_Error;
/** /**
@ -20,6 +21,8 @@ class SdkClientToken {
use RequestTrait; use RequestTrait;
const CACHE_KEY = 'sdk-client-token-key';
/** /**
* The host. * The host.
* *
@ -41,21 +44,31 @@ class SdkClientToken {
*/ */
private $client_credentials; private $client_credentials;
/**
* The cache.
*
* @var Cache
*/
private $cache;
/** /**
* SdkClientToken constructor. * SdkClientToken constructor.
* *
* @param string $host The host. * @param string $host The host.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
* @param ClientCredentials $client_credentials The client credentials. * @param ClientCredentials $client_credentials The client credentials.
* @param Cache $cache The cache.
*/ */
public function __construct( public function __construct(
string $host, string $host,
LoggerInterface $logger, LoggerInterface $logger,
ClientCredentials $client_credentials ClientCredentials $client_credentials,
Cache $cache
) { ) {
$this->host = $host; $this->host = $host;
$this->logger = $logger; $this->logger = $logger;
$this->client_credentials = $client_credentials; $this->client_credentials = $client_credentials;
$this->cache = $cache;
} }
/** /**
@ -69,6 +82,10 @@ class SdkClientToken {
* @throws RuntimeException If something unexpected happens. * @throws RuntimeException If something unexpected happens.
*/ */
public function sdk_client_token( string $target_customer_id = '' ): string { public function sdk_client_token( string $target_customer_id = '' ): string {
if ( $this->cache->has( self::CACHE_KEY ) ) {
return $this->cache->get( self::CACHE_KEY );
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$domain = wp_unslash( $_SERVER['HTTP_HOST'] ?? '' ); $domain = wp_unslash( $_SERVER['HTTP_HOST'] ?? '' );
$domain = preg_replace( '/^www\./', '', $domain ); $domain = preg_replace( '/^www\./', '', $domain );
@ -103,6 +120,9 @@ class SdkClientToken {
throw new PayPalApiException( $json, $status_code ); throw new PayPalApiException( $json, $status_code );
} }
return $json->access_token; $access_token = $json->access_token;
$this->cache->set( self::CACHE_KEY, $access_token );
return $access_token;
} }
} }

View file

@ -11,6 +11,7 @@ use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WP_Error; use WP_Error;
/** /**
@ -20,6 +21,8 @@ class UserIdToken {
use RequestTrait; use RequestTrait;
const CACHE_KEY = 'user-id-token-key';
/** /**
* The host. * The host.
* *
@ -41,21 +44,31 @@ class UserIdToken {
*/ */
private $client_credentials; private $client_credentials;
/**
* The cache.
*
* @var Cache
*/
private $cache;
/** /**
* UserIdToken constructor. * UserIdToken constructor.
* *
* @param string $host The host. * @param string $host The host.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
* @param ClientCredentials $client_credentials The client credentials. * @param ClientCredentials $client_credentials The client credentials.
* @param Cache $cache The cache.
*/ */
public function __construct( public function __construct(
string $host, string $host,
LoggerInterface $logger, LoggerInterface $logger,
ClientCredentials $client_credentials ClientCredentials $client_credentials,
Cache $cache
) { ) {
$this->host = $host; $this->host = $host;
$this->logger = $logger; $this->logger = $logger;
$this->client_credentials = $client_credentials; $this->client_credentials = $client_credentials;
$this->cache = $cache;
} }
/** /**
@ -69,6 +82,10 @@ class UserIdToken {
* @throws RuntimeException If something unexpected happens. * @throws RuntimeException If something unexpected happens.
*/ */
public function id_token( string $target_customer_id = '' ): string { public function id_token( string $target_customer_id = '' ): string {
if ( $this->cache->has( self::CACHE_KEY ) ) {
return $this->cache->get( self::CACHE_KEY );
}
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token'; $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
if ( $target_customer_id ) { if ( $target_customer_id ) {
$url = add_query_arg( $url = add_query_arg(
@ -98,6 +115,9 @@ class UserIdToken {
throw new PayPalApiException( $json, $status_code ); throw new PayPalApiException( $json, $status_code );
} }
return $json->id_token; $id_token = $json->id_token;
$this->cache->set( self::CACHE_KEY, $id_token );
return $id_token;
} }
} }

View file

@ -189,9 +189,8 @@ return array(
$login_seller_sandbox = $container->get( 'api.endpoint.login-seller-sandbox' ); $login_seller_sandbox = $container->get( 'api.endpoint.login-seller-sandbox' );
$partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' ); $partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' );
$settings = $container->get( 'wcgateway.settings' ); $settings = $container->get( 'wcgateway.settings' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$cache = new Cache( 'ppcp-paypal-bearer' ); $cache = new Cache( 'ppcp-paypal-bearer' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new LoginSellerEndpoint( return new LoginSellerEndpoint(
$request_data, $request_data,
$login_seller_production, $login_seller_production,
@ -199,7 +198,8 @@ return array(
$partner_referrals_data, $partner_referrals_data,
$settings, $settings,
$cache, $cache,
$logger $logger,
new Cache( 'ppcp-client-credentials-cache' )
); );
}, },
'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : UpdateSignupLinksEndpoint { 'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : UpdateSignupLinksEndpoint {

View file

@ -12,6 +12,8 @@ namespace WooCommerce\PayPalCommerce\Onboarding\Endpoint;
use Exception; use Exception;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData; use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
@ -76,6 +78,13 @@ class LoginSellerEndpoint implements EndpointInterface {
*/ */
protected $logger; protected $logger;
/**
* The client credentials cache.
*
* @var Cache
*/
private $client_credentials_cache;
/** /**
* LoginSellerEndpoint constructor. * LoginSellerEndpoint constructor.
* *
@ -86,6 +95,7 @@ class LoginSellerEndpoint implements EndpointInterface {
* @param Settings $settings The Settings. * @param Settings $settings The Settings.
* @param Cache $cache The Cache. * @param Cache $cache The Cache.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
* @param Cache $client_credentials_cache The client credentials cache.
*/ */
public function __construct( public function __construct(
RequestData $request_data, RequestData $request_data,
@ -94,16 +104,18 @@ class LoginSellerEndpoint implements EndpointInterface {
PartnerReferralsData $partner_referrals_data, PartnerReferralsData $partner_referrals_data,
Settings $settings, Settings $settings,
Cache $cache, Cache $cache,
LoggerInterface $logger LoggerInterface $logger,
Cache $client_credentials_cache
) { ) {
$this->request_data = $request_data; $this->request_data = $request_data;
$this->login_seller_production = $login_seller_production; $this->login_seller_production = $login_seller_production;
$this->login_seller_sandbox = $login_seller_sandbox; $this->login_seller_sandbox = $login_seller_sandbox;
$this->partner_referrals_data = $partner_referrals_data; $this->partner_referrals_data = $partner_referrals_data;
$this->settings = $settings; $this->settings = $settings;
$this->cache = $cache; $this->cache = $cache;
$this->logger = $logger; $this->logger = $logger;
$this->client_credentials_cache = $client_credentials_cache;
} }
/** /**
@ -175,6 +187,12 @@ class LoginSellerEndpoint implements EndpointInterface {
if ( $this->cache->has( PayPalBearer::CACHE_KEY ) ) { if ( $this->cache->has( PayPalBearer::CACHE_KEY ) ) {
$this->cache->delete( PayPalBearer::CACHE_KEY ); $this->cache->delete( PayPalBearer::CACHE_KEY );
} }
if ( $this->client_credentials_cache->has( UserIdToken::CACHE_KEY ) ) {
$this->client_credentials_cache->delete( UserIdToken::CACHE_KEY );
}
if ( $this->client_credentials_cache->has( SdkClientToken::CACHE_KEY ) ) {
$this->client_credentials_cache->delete( SdkClientToken::CACHE_KEY );
}
wp_schedule_single_event( wp_schedule_single_event(
time() + 5, time() + 5,

View file

@ -366,7 +366,8 @@ return array(
$container->get( 'api.partner_merchant_id-production' ), $container->get( 'api.partner_merchant_id-production' ),
$container->get( 'api.partner_merchant_id-sandbox' ), $container->get( 'api.partner_merchant_id-sandbox' ),
$container->get( 'api.endpoint.billing-agreements' ), $container->get( 'api.endpoint.billing-agreements' ),
$logger $logger,
new Cache( 'ppcp-client-credentials-cache' )
); );
}, },
'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor { 'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor {

View file

@ -12,6 +12,8 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Settings;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingAgreementsEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingAgreementsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
@ -164,6 +166,13 @@ class SettingsListener {
*/ */
private $logger; private $logger;
/**
* The client credentials cache.
*
* @var Cache
*/
private $client_credentials_cache;
/** /**
* SettingsListener constructor. * SettingsListener constructor.
* *
@ -183,6 +192,7 @@ class SettingsListener {
* @param string $partner_merchant_id_sandbox Partner merchant ID sandbox. * @param string $partner_merchant_id_sandbox Partner merchant ID sandbox.
* @param BillingAgreementsEndpoint $billing_agreements_endpoint Billing Agreements endpoint. * @param BillingAgreementsEndpoint $billing_agreements_endpoint Billing Agreements endpoint.
* @param ?LoggerInterface $logger The logger. * @param ?LoggerInterface $logger The logger.
* @param Cache $client_credentials_cache The client credentials cache.
*/ */
public function __construct( public function __construct(
Settings $settings, Settings $settings,
@ -200,7 +210,8 @@ class SettingsListener {
string $partner_merchant_id_production, string $partner_merchant_id_production,
string $partner_merchant_id_sandbox, string $partner_merchant_id_sandbox,
BillingAgreementsEndpoint $billing_agreements_endpoint, BillingAgreementsEndpoint $billing_agreements_endpoint,
LoggerInterface $logger = null LoggerInterface $logger = null,
Cache $client_credentials_cache
) { ) {
$this->settings = $settings; $this->settings = $settings;
@ -219,6 +230,7 @@ class SettingsListener {
$this->partner_merchant_id_sandbox = $partner_merchant_id_sandbox; $this->partner_merchant_id_sandbox = $partner_merchant_id_sandbox;
$this->billing_agreements_endpoint = $billing_agreements_endpoint; $this->billing_agreements_endpoint = $billing_agreements_endpoint;
$this->logger = $logger ?: new NullLogger(); $this->logger = $logger ?: new NullLogger();
$this->client_credentials_cache = $client_credentials_cache;
} }
/** /**
@ -490,6 +502,12 @@ class SettingsListener {
if ( $this->cache->has( PayPalBearer::CACHE_KEY ) ) { if ( $this->cache->has( PayPalBearer::CACHE_KEY ) ) {
$this->cache->delete( PayPalBearer::CACHE_KEY ); $this->cache->delete( PayPalBearer::CACHE_KEY );
} }
if ( $this->client_credentials_cache->has( UserIdToken::CACHE_KEY ) ) {
$this->client_credentials_cache->delete( UserIdToken::CACHE_KEY );
}
if ( $this->client_credentials_cache->has( SdkClientToken::CACHE_KEY ) ) {
$this->client_credentials_cache->delete( SdkClientToken::CACHE_KEY );
}
if ( $this->pui_status_cache->has( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY ) ) { if ( $this->pui_status_cache->has( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY ) ) {
$this->pui_status_cache->delete( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY ); $this->pui_status_cache->delete( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY );