1. Go to this page and download the library: Download klsoft/yii3-jwt-auth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
klsoft / yii3-jwt-auth example snippets
namespace MyNamespace;
use Yiisoft\Cache\CacheInterface;
use Klsoft\Yii3JwtAuth\JwksRepositoryInterface;
final class JwksRepository implements JwksRepositoryInterface
{
private const JWKS = 'jwks';
public function __construct(
private string $jwksUrl,
private int $jwksCacheDuration,
private CacheInterface $cache)
{
}
public function getKeys(): ?array
{
$keys = $this->cache->getOrSet(
JwksRepository::JWKS,
function () {
$options = [
'http' => [
'method' => 'GET'
],
];
$responseData = file_get_contents($this->jwksUrl, false, stream_context_create($options));
if (!empty($responseData)) {
return json_decode($responseData, true);
}
return [];
},
$this->jwksCacheDuration);
if (empty($keys)) {
$this->cache->remove(JwksRepository::JWKS);
return null;
} else {
return $keys;
}
}
}