| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\OpenIDClient\Client; |
| 6 |
|
| 7 |
use Facile\JoseVerifier\JWK\JwksProviderInterface; |
| 8 |
use Facile\JoseVerifier\JWK\MemoryJwksProvider; |
| 9 |
use Facile\OpenIDClient\AuthMethod\AuthMethodFactory; |
| 10 |
use Facile\OpenIDClient\AuthMethod\AuthMethodFactoryInterface; |
| 11 |
use Facile\OpenIDClient\AuthMethod\ClientSecretBasic; |
| 12 |
use Facile\OpenIDClient\AuthMethod\ClientSecretJwt; |
| 13 |
use Facile\OpenIDClient\AuthMethod\ClientSecretPost; |
| 14 |
use Facile\OpenIDClient\AuthMethod\None; |
| 15 |
use Facile\OpenIDClient\AuthMethod\PrivateKeyJwt; |
| 16 |
use Facile\OpenIDClient\AuthMethod\SelfSignedTLSClientAuth; |
| 17 |
use Facile\OpenIDClient\AuthMethod\TLSClientAuth; |
| 18 |
use Facile\OpenIDClient\Client\Metadata\ClientMetadataInterface; |
| 19 |
use Facile\OpenIDClient\Exception\InvalidArgumentException; |
| 20 |
use Facile\OpenIDClient\Issuer\IssuerInterface; |
| 21 |
use Http\Discovery\Psr18ClientDiscovery; |
| 22 |
use Psr\Http\Client\ClientInterface as HttpClient; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
final class ClientBuilder |
| 28 |
{ |
| 29 |
private ?ClientMetadataInterface $clientMetadata = null; |
| 30 |
|
| 31 |
private ?IssuerInterface $issuer = null; |
| 32 |
|
| 33 |
private ?JwksProviderInterface $jwksProvider = null; |
| 34 |
|
| 35 |
private ?AuthMethodFactoryInterface $authMethodFactory = null; |
| 36 |
|
| 37 |
private ?HttpClient $httpClient = null; |
| 38 |
|
| 39 |
public function setClientMetadata(?ClientMetadataInterface $clientMetadata): self |
| 40 |
{ |
| 41 |
$this->clientMetadata = $clientMetadata; |
| 42 |
|
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
public function setIssuer(?IssuerInterface $issuer): self |
| 47 |
{ |
| 48 |
$this->issuer = $issuer; |
| 49 |
|
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
public function setJwksProvider(?JwksProviderInterface $jwksProvider): self |
| 54 |
{ |
| 55 |
$this->jwksProvider = $jwksProvider; |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
public function setAuthMethodFactory(?AuthMethodFactoryInterface $authMethodFactory): self |
| 61 |
{ |
| 62 |
$this->authMethodFactory = $authMethodFactory; |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
public function setHttpClient(?HttpClient $httpClient): self |
| 68 |
{ |
| 69 |
$this->httpClient = $httpClient; |
| 70 |
|
| 71 |
return $this; |
| 72 |
} |
| 73 |
|
| 74 |
private function buildJwksProvider(): JwksProviderInterface |
| 75 |
{ |
| 76 |
if (null !== $this->jwksProvider) { |
| 77 |
return $this->jwksProvider; |
| 78 |
} |
| 79 |
|
| 80 |
if (null === $this->clientMetadata) { |
| 81 |
return new MemoryJwksProvider(['keys' => []]); |
| 82 |
} |
| 83 |
|
| 84 |
$jwks = $this->clientMetadata->getJwks(); |
| 85 |
|
| 86 |
if (null !== $jwks) { |
| 87 |
return new MemoryJwksProvider($jwks); |
| 88 |
} |
| 89 |
|
| 90 |
return new MemoryJwksProvider(['keys' => []]); |
| 91 |
} |
| 92 |
|
| 93 |
private function buildAuthMethodFactory(): AuthMethodFactoryInterface |
| 94 |
{ |
| 95 |
return $this->authMethodFactory ?? new AuthMethodFactory([ |
| 96 |
new ClientSecretBasic(), |
| 97 |
new ClientSecretJwt(), |
| 98 |
new ClientSecretPost(), |
| 99 |
new None(), |
| 100 |
new PrivateKeyJwt(), |
| 101 |
new TLSClientAuth(), |
| 102 |
new SelfSignedTLSClientAuth(), |
| 103 |
]); |
| 104 |
} |
| 105 |
|
| 106 |
private function buildHttpClient(): HttpClient |
| 107 |
{ |
| 108 |
return $this->httpClient ?? Psr18ClientDiscovery::find(); |
| 109 |
} |
| 110 |
|
| 111 |
public function build(): ClientInterface |
| 112 |
{ |
| 113 |
if (null === $this->issuer) { |
| 114 |
throw new InvalidArgumentException('Issuer must be provided'); |
| 115 |
} |
| 116 |
|
| 117 |
if (null === $this->clientMetadata) { |
| 118 |
throw new InvalidArgumentException('Client metadata must be provided'); |
| 119 |
} |
| 120 |
|
| 121 |
return new Client( |
| 122 |
$this->issuer, |
| 123 |
$this->clientMetadata, |
| 124 |
$this->buildJwksProvider(), |
| 125 |
$this->buildAuthMethodFactory(), |
| 126 |
$this->buildHttpClient() |
| 127 |
); |
| 128 |
} |
| 129 |
} |