From 22ba368ba1d57e6a62e3c8e933739207e214c09e Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 4 Oct 2024 12:58:16 +0200 Subject: [PATCH 1/2] Cache id token response for 5 seconds --- modules/ppcp-api-client/services.php | 6 +++- .../src/Authentication/UserIdToken.php | 28 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php index aa8b5159f..a3850bd7c 100644 --- a/modules/ppcp-api-client/services.php +++ b/modules/ppcp-api-client/services.php @@ -834,11 +834,15 @@ return array( 'api.client-credentials-cache' => static function( ContainerInterface $container ): Cache { return new Cache( 'ppcp-client-credentials-cache' ); }, + 'api.user-id-token-cache' => static function( ContainerInterface $container ): Cache { + return new Cache( 'ppcp-id-token-cache' ); + }, 'api.user-id-token' => static function( ContainerInterface $container ): UserIdToken { return new UserIdToken( $container->get( 'api.host' ), $container->get( 'woocommerce.logger.woocommerce' ), - $container->get( 'api.client-credentials' ) + $container->get( 'api.client-credentials' ), + $container->get( 'api.user-id-token-cache' ) ); }, 'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken { diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index dde47c223..19965d5c4 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -11,6 +11,7 @@ use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; +use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WP_Error; /** @@ -20,6 +21,8 @@ class UserIdToken { use RequestTrait; + const CACHE_KEY = 'id-token-key'; + /** * The host. * @@ -41,21 +44,31 @@ class UserIdToken { */ private $client_credentials; + /** + * The cache. + * + * @var Cache + */ + private $cache; + /** * UserIdToken constructor. * * @param string $host The host. * @param LoggerInterface $logger The logger. * @param ClientCredentials $client_credentials The client credentials. + * @param Cache $cache The cache. */ public function __construct( string $host, LoggerInterface $logger, - ClientCredentials $client_credentials + ClientCredentials $client_credentials, + Cache $cache ) { $this->host = $host; $this->logger = $logger; $this->client_credentials = $client_credentials; + $this->cache = $cache; } /** @@ -69,6 +82,11 @@ class UserIdToken { * @throws RuntimeException If something unexpected happens. */ public function id_token( string $target_customer_id = '' ): string { + $session_customer_id = WC()->session->get_customer_id() ?? ''; + if ( $session_customer_id && $this->cache->has( self::CACHE_KEY . (string) $session_customer_id ) ) { + return $this->cache->get( self::CACHE_KEY . (string) $session_customer_id ); + } + $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token'; if ( $target_customer_id ) { $url = add_query_arg( @@ -98,6 +116,12 @@ class UserIdToken { throw new PayPalApiException( $json, $status_code ); } - return $json->id_token; + $id_token = $json->id_token; + + if ( $session_customer_id ) { + $this->cache->set( self::CACHE_KEY . (string) $session_customer_id, $id_token, 5 ); + } + + return $id_token; } } From 6d52f39ad0dbf0aa9817988f3b142d1784341f5a Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 4 Oct 2024 14:56:15 +0200 Subject: [PATCH 2/2] Fix psalm --- modules/ppcp-api-client/src/Authentication/UserIdToken.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index 19965d5c4..080091266 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -82,7 +82,7 @@ class UserIdToken { * @throws RuntimeException If something unexpected happens. */ public function id_token( string $target_customer_id = '' ): string { - $session_customer_id = WC()->session->get_customer_id() ?? ''; + $session_customer_id = WC()->session->get_customer_id() ?: ''; if ( $session_customer_id && $this->cache->has( self::CACHE_KEY . (string) $session_customer_id ) ) { return $this->cache->get( self::CACHE_KEY . (string) $session_customer_id ); }