| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\OpenIDClient\AuthMethod; |
| 6 |
|
| 7 |
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient; |
| 8 |
use Facile\OpenIDClient\Exception\InvalidArgumentException; |
| 9 |
use Psr\Http\Message\RequestInterface; |
| 10 |
use Override; |
| 11 |
|
| 12 |
use function base64_encode; |
| 13 |
use function http_build_query; |
| 14 |
use function urlencode; |
| 15 |
|
| 16 |
final class ClientSecretBasic implements AuthMethodInterface |
| 17 |
{ |
| 18 |
#[Override] |
| 19 |
public function getSupportedMethod(): string |
| 20 |
{ |
| 21 |
return 'client_secret_basic'; |
| 22 |
} |
| 23 |
|
| 24 |
#[Override] |
| 25 |
public function createRequest( |
| 26 |
RequestInterface $request, |
| 27 |
OpenIDClient $client, |
| 28 |
array $claims |
| 29 |
): RequestInterface { |
| 30 |
$clientId = $client->getMetadata()->getClientId(); |
| 31 |
$clientSecret = $client->getMetadata()->getClientSecret(); |
| 32 |
|
| 33 |
if (null === $clientSecret) { |
| 34 |
throw new InvalidArgumentException($this->getSupportedMethod() . ' cannot be used without client_secret metadata'); |
| 35 |
} |
| 36 |
|
| 37 |
$request = $request->withHeader( |
| 38 |
'Authorization', |
| 39 |
'Basic ' . base64_encode(urlencode($clientId) . ':' . urlencode($clientSecret)) |
| 40 |
); |
| 41 |
|
| 42 |
$request->getBody()->write(http_build_query($claims)); |
| 43 |
|
| 44 |
return $request; |
| 45 |
} |
| 46 |
} |