Add id token cache key for each user

This commit is contained in:
Emili Castells Guasch 2024-08-06 15:30:50 +02:00
parent a1413782e9
commit b42e9436ae
7 changed files with 15 additions and 62 deletions

View file

@ -10,7 +10,6 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
@ -100,15 +99,12 @@ class ApiModule implements ModuleInterface {
add_action(
'wp_logout',
function() use ( $c ) {
function( int $user_id ) use ( $c ) {
$client_credentials_cache = $c->get( 'api.client-credentials-cache' );
assert( $client_credentials_cache instanceof Cache );
if ( $client_credentials_cache->has( UserIdToken::CACHE_KEY ) ) {
$client_credentials_cache->delete( UserIdToken::CACHE_KEY );
}
if ( $client_credentials_cache->has( SdkClientToken::CACHE_KEY ) ) {
$client_credentials_cache->delete( SdkClientToken::CACHE_KEY );
if ( $client_credentials_cache->has( UserIdToken::CACHE_KEY . '-' . (string) $user_id ) ) {
$client_credentials_cache->delete( UserIdToken::CACHE_KEY . '-' . (string) $user_id );
}
}
);

View file

@ -72,23 +72,16 @@ class SdkClientToken {
}
/**
* Returns `sdk_client_token` which uniquely identifies the payer.
*
* @param string $target_customer_id Vaulted customer id.
* Returns the client token for SDK `data-sdk-client-token`.
*
* @return string
*
* @throws PayPalApiException If the request fails.
* @throws RuntimeException If something unexpected happens.
*/
public function sdk_client_token( string $target_customer_id = '' ): string {
public function sdk_client_token(): string {
if ( $this->cache->has( self::CACHE_KEY ) ) {
$user_id = $this->cache->get( self::CACHE_KEY )['user_id'] ?? 0;
$access_token = $this->cache->get( self::CACHE_KEY )['access_token'] ?? '';
if ( $user_id === get_current_user_id() && $access_token ) {
return $access_token;
}
$this->cache->get( self::CACHE_KEY );
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
@ -97,15 +90,6 @@ class SdkClientToken {
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=client_token&intent=sdk_init&domains[]=' . $domain;
if ( $target_customer_id ) {
$url = add_query_arg(
array(
'target_customer_id' => $target_customer_id,
),
$url
);
}
$args = array(
'method' => 'POST',
'headers' => array(
@ -126,13 +110,7 @@ class SdkClientToken {
}
$access_token = $json->access_token;
$data = array(
'access_token' => $access_token,
'user_id' => get_current_user_id(),
);
$this->cache->set( self::CACHE_KEY, $data );
$this->cache->set( self::CACHE_KEY, $access_token );
return $access_token;
}

View file

@ -82,13 +82,8 @@ class UserIdToken {
* @throws RuntimeException If something unexpected happens.
*/
public function id_token( string $target_customer_id = '' ): string {
if ( $this->cache->has( self::CACHE_KEY ) ) {
$user_id = $this->cache->get( self::CACHE_KEY )['user_id'] ?? 0;
$id_token = $this->cache->get( self::CACHE_KEY )['id_token'] ?? '';
if ( $user_id === get_current_user_id() && $id_token ) {
return $id_token;
}
if ( $this->cache->has( self::CACHE_KEY . '-' . (string) get_current_user_id() ) ) {
return $this->cache->get( self::CACHE_KEY . '-' . (string) get_current_user_id() );
}
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
@ -122,12 +117,7 @@ class UserIdToken {
$id_token = $json->id_token;
$data = array(
'id_token' => $id_token,
'user_id' => get_current_user_id(),
);
$this->cache->set( self::CACHE_KEY, $data );
$this->cache->set( self::CACHE_KEY . '-' . (string) get_current_user_id(), $id_token );
return $id_token;
}