| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\JoseVerifier\JWK; |
| 6 |
|
| 7 |
use Facile\JoseVerifier\Exception\InvalidArgumentException; |
| 8 |
use Nyholm\Psr7\Factory\Psr17Factory; |
| 9 |
use Psr\Http\Client\ClientInterface; |
| 10 |
use Psr\Http\Message\RequestFactoryInterface; |
| 11 |
use Psr\SimpleCache\CacheInterface; |
| 12 |
use Symfony\Component\HttpClient\Psr18Client; |
| 13 |
|
| 14 |
use function sha1; |
| 15 |
use function substr; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
final class JwksProviderBuilder |
| 23 |
{ |
| 24 |
|
| 25 |
private ?array $jwks = null; |
| 26 |
|
| 27 |
private ?string $jwksUri = null; |
| 28 |
|
| 29 |
private ?ClientInterface $httpClient = null; |
| 30 |
|
| 31 |
private ?RequestFactoryInterface $requestFactory = null; |
| 32 |
|
| 33 |
private ?CacheInterface $cache = null; |
| 34 |
|
| 35 |
private ?int $cacheTtl = 86_400; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function withJwks(array $jwks): static |
| 41 |
{ |
| 42 |
$new = clone $this; |
| 43 |
$new->jwks = $jwks; |
| 44 |
|
| 45 |
return $new; |
| 46 |
} |
| 47 |
|
| 48 |
public function withJwksUri(string $jwksUri): static |
| 49 |
{ |
| 50 |
$new = clone $this; |
| 51 |
$new->jwksUri = $jwksUri; |
| 52 |
|
| 53 |
return $new; |
| 54 |
} |
| 55 |
|
| 56 |
public function withHttpClient(ClientInterface $httpClient): static |
| 57 |
{ |
| 58 |
$new = clone $this; |
| 59 |
$new->httpClient = $httpClient; |
| 60 |
|
| 61 |
return $new; |
| 62 |
} |
| 63 |
|
| 64 |
public function withRequestFactory(RequestFactoryInterface $requestFactory): static |
| 65 |
{ |
| 66 |
$new = clone $this; |
| 67 |
$new->requestFactory = $requestFactory; |
| 68 |
|
| 69 |
return $new; |
| 70 |
} |
| 71 |
|
| 72 |
public function withCache(CacheInterface $cache): static |
| 73 |
{ |
| 74 |
$new = clone $this; |
| 75 |
$new->cache = $cache; |
| 76 |
|
| 77 |
return $new; |
| 78 |
} |
| 79 |
|
| 80 |
public function withCacheTtl(int $cacheTtl): static |
| 81 |
{ |
| 82 |
$new = clone $this; |
| 83 |
$new->cacheTtl = $cacheTtl; |
| 84 |
|
| 85 |
return $new; |
| 86 |
} |
| 87 |
|
| 88 |
protected function buildRequestFactory(): RequestFactoryInterface |
| 89 |
{ |
| 90 |
return $this->requestFactory ?? new Psr17Factory(); |
| 91 |
} |
| 92 |
|
| 93 |
protected function buildHttpClient(): ClientInterface |
| 94 |
{ |
| 95 |
return $this->httpClient ?? new Psr18Client(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function build(): JwksProviderInterface |
| 102 |
{ |
| 103 |
if (null !== $this->jwks && null !== $this->jwksUri) { |
| 104 |
throw new InvalidArgumentException('You should provide only one between remote or static jwks'); |
| 105 |
} |
| 106 |
|
| 107 |
if (null === $this->jwksUri) { |
| 108 |
$jwks = $this->jwks ?? ['keys' => []]; |
| 109 |
|
| 110 |
return new MemoryJwksProvider($jwks); |
| 111 |
} |
| 112 |
|
| 113 |
$provider = new RemoteJwksProvider( |
| 114 |
$this->buildHttpClient(), |
| 115 |
$this->buildRequestFactory(), |
| 116 |
$this->jwksUri |
| 117 |
); |
| 118 |
|
| 119 |
if (null !== $this->cache) { |
| 120 |
$provider = new CachedJwksProvider( |
| 121 |
$provider, |
| 122 |
$this->cache, |
| 123 |
substr(sha1(__CLASS__ . $this->jwksUri), 0, 65), |
| 124 |
$this->cacheTtl |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
return $provider; |
| 129 |
} |
| 130 |
} |