Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert caching to PSR-16 interface #403

Merged
merged 2 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
"php": "^7.1",
"guzzlehttp/guzzle": "~6.0",
"ext-json": "*",
"lcobucci/jwt": "^3.3"
"lcobucci/jwt": "^3.3",
"psr/simple-cache": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"josegonzalez/dotenv": "^2.0",
"squizlabs/php_codesniffer": "^3.2",
"phpcompatibility/php-compatibility": "^8.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0"
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"cache/array-adapter": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 5 additions & 7 deletions src/Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
use Auth0\SDK\Exception\CoreException;
use Auth0\SDK\Exception\ApiException;
use Auth0\SDK\Exception\InvalidTokenException;
use Auth0\SDK\Helpers\Cache\CacheHandler;
use Auth0\SDK\Helpers\Cache\NoCacheHandler;
use Auth0\SDK\Helpers\JWKFetcher;
use Auth0\SDK\Helpers\Tokens\IdTokenVerifier;
use Auth0\SDK\Helpers\Tokens\AsymmetricVerifier;
use Auth0\SDK\Helpers\Tokens\SymmetricVerifier;
use Auth0\SDK\Helpers\TransientStoreHandler;
use Auth0\SDK\Store\CookieStore;
use Auth0\SDK\Store\EmptyStore;
use Auth0\SDK\Store\SessionStore;
use Auth0\SDK\Store\StoreInterface;
use Auth0\SDK\API\Authentication;

use GuzzleHttp\Exception\RequestException;
use Psr\SimpleCache\CacheInterface;

/**
* Class Auth0
Expand Down Expand Up @@ -202,7 +201,7 @@ class Auth0
/**
* Cache Handler.
*
* @var CacheHandler
* @var CacheInterface
*/
protected $cacheHandler;

Expand All @@ -229,7 +228,7 @@ class Auth0
* leave empty to default to SessionStore
* - transient_store (Mixed) Optional. StorageInterface for transient auth data;
* leave empty to default to CookieStore
* - cache_handler (Mixed) Optional. A class that implements CacheHandler of false for none
* - cache_handler (Mixed) Optional. CacheInterface instance or false for none
* - persist_user (Boolean) Optional. Persist the user info, default true
* - persist_access_token (Boolean) Optional. Persist the access token, default false
* - persist_refresh_token (Boolean) Optional. Persist the refresh token, default false
Expand Down Expand Up @@ -311,9 +310,8 @@ public function __construct(array $config)

$this->transientHandler = new TransientStoreHandler( $transientStore );

if (isset($config['cache_handler']) && $config['cache_handler'] instanceof CacheHandler) {
$this->cacheHandler = $config['cache_handler'];
} else {
$this->cacheHandler = $config['cache_handler'] ?? null;
if (! $this->cacheHandler instanceof CacheInterface) {
$this->cacheHandler = new NoCacheHandler();
}

Expand Down
27 changes: 0 additions & 27 deletions src/Helpers/Cache/CacheHandler.php

This file was deleted.

80 changes: 0 additions & 80 deletions src/Helpers/Cache/FileSystemCacheHandler.php

This file was deleted.

100 changes: 91 additions & 9 deletions src/Helpers/Cache/NoCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,115 @@

namespace Auth0\SDK\Helpers\Cache;

class NoCacheHandler implements CacheHandler
use Psr\SimpleCache\CacheInterface;

class NoCacheHandler implements CacheInterface
{
/**
* Fetches a value from the cache.
*
* @param string $key The unique key of this item in the cache.
* @param mixed $default Default value to return if the key does not exist.
*
* @return mixed The value of the item from the cache, or $default in case of cache miss.
*/
public function get($key, $default = null)
{
return $default;
}

/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|integer|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @param string $key
* @return null
* @return boolean True on success and false on failure.
*/
public function get($key)
public function set($key, $value, $ttl = null)
{
return null;
return true;
}

/**
* Delete an item from the cache by its unique key.
*
* @param string $key
* @param string $key The unique cache key of the item to delete.
*
* @return boolean True if the item was successfully removed. False if there was an error.
*/
public function delete($key)
{
return true;
}

/**
* Wipes clean the entire cache's keys.
*
* @return boolean True on success and false on failure.
*/
public function clear()
{
return true;
}

/**
* Obtains multiple cache items by their unique keys.
*
* @param iterable $keys A list of keys that can obtained in a single operation.
* @param mixed $default Default value to return for keys that do not exist.
*
* @return iterable A list of key => value pairs.
*/
public function getMultiple($keys, $default = null)
{
$values = [];
foreach ($keys as $key) {
$values[$key] = $default;
}

return $values;
}

/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*
* @param iterable $values A list of key => value pairs for a multiple-set operation.
* @param null|integer|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return boolean True on success and false on failure.
*/
public function setMultiple($values, $ttl = null)
{
return true;
}

/**
* Deletes multiple cache items in a single operation.
*
* @param iterable $keys A list of string-based keys to be deleted.
*
* @return boolean True if the items were successfully removed. False if there was an error.
*/
public function deleteMultiple($keys)
{
return true;
}

/**
* Determines whether an item is present in the cache.
*
* @param string $key The cache item key.
*
* @param string $key
* @param mixed $value
* @return boolean
*/
public function set($key, $value)
public function has($key)
{
return false;
}
}
15 changes: 8 additions & 7 deletions src/Helpers/JWKFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace Auth0\SDK\Helpers;

use Auth0\SDK\API\Helpers\RequestBuilder;
use Auth0\SDK\Helpers\Cache\CacheHandler;
use Auth0\SDK\Helpers\Cache\NoCacheHandler;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use Psr\SimpleCache\CacheInterface;

/**
* Class JWKFetcher.
Expand All @@ -20,7 +20,7 @@ class JWKFetcher
/**
* Cache handler or null for no caching.
*
* @var CacheHandler|null
* @var CacheInterface|null
*/
private $cache;

Expand All @@ -34,10 +34,10 @@ class JWKFetcher
/**
* JWKFetcher constructor.
*
* @param CacheHandler|null $cache Cache handler or null for no caching.
* @param array $guzzleOptions Options for the Guzzle HTTP client.
* @param CacheInterface|null $cache Cache handler or null for no caching.
* @param array $guzzleOptions Options for the Guzzle HTTP client.
*/
public function __construct(CacheHandler $cache = null, array $guzzleOptions = [])
public function __construct(CacheInterface $cache = null, array $guzzleOptions = [])
{
if ($cache === null) {
$cache = new NoCacheHandler();
Expand Down Expand Up @@ -71,7 +71,8 @@ protected function convertCertToPem(string $cert) : string
*/
public function getKeys(string $jwks_url) : array
{
$keys = $this->cache->get($jwks_url);
$cache_key = md5($jwks_url);
$keys = $this->cache->get($cache_key);
if (is_array($keys) && ! empty($keys)) {
return $keys;
}
Expand All @@ -91,7 +92,7 @@ public function getKeys(string $jwks_url) : array
$keys[$key['kid']] = $this->convertCertToPem( $key['x5c'][0] );
}

$this->cache->set($jwks_url, $keys);
$this->cache->set($cache_key, $keys);
return $keys;
}

Expand Down
Loading