| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\OpenIDClient\AuthMethod; |
| 6 |
|
| 7 |
use Facile\OpenIDClient\AlgorithmManagerBuilder; |
| 8 |
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient; |
| 9 |
use Facile\OpenIDClient\Exception\RuntimeException; |
| 10 |
use Jose\Component\Core\JWK; |
| 11 |
use Jose\Component\Core\JWKSet; |
| 12 |
use Jose\Component\Signature\JWSBuilder; |
| 13 |
use Jose\Component\Signature\Serializer\CompactSerializer; |
| 14 |
use Jose\Component\Signature\Serializer\JWSSerializer; |
| 15 |
use Override; |
| 16 |
|
| 17 |
use function array_merge; |
| 18 |
use function Facile\OpenIDClient\base64url_encode; |
| 19 |
use function Facile\OpenIDClient\get_endpoint_uri; |
| 20 |
use function json_encode; |
| 21 |
use function random_bytes; |
| 22 |
use function time; |
| 23 |
|
| 24 |
final class PrivateKeyJwt extends AbstractJwtAuth |
| 25 |
{ |
| 26 |
private JWSBuilder $jwsBuilder; |
| 27 |
|
| 28 |
private JWSSerializer $jwsSerializer; |
| 29 |
|
| 30 |
private ?JWK $jwk; |
| 31 |
|
| 32 |
private int $tokenTTL; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function __construct( |
| 38 |
?JWSBuilder $jwsBuilder = null, |
| 39 |
?JWSSerializer $serializer = null, |
| 40 |
?JWK $jwk = null, |
| 41 |
int $tokenTTL = 60 |
| 42 |
) { |
| 43 |
$this->jwsBuilder = $jwsBuilder ?? new JWSBuilder((new AlgorithmManagerBuilder())->build()); |
| 44 |
$this->jwsSerializer = $serializer ?? new CompactSerializer(); |
| 45 |
$this->jwk = $jwk; |
| 46 |
$this->tokenTTL = $tokenTTL; |
| 47 |
} |
| 48 |
|
| 49 |
#[Override] |
| 50 |
public function getSupportedMethod(): string |
| 51 |
{ |
| 52 |
return 'private_key_jwt'; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#[Override] |
| 59 |
protected function createAuthJwt(OpenIDClient $client, array $claims = []): string |
| 60 |
{ |
| 61 |
$clientId = $client->getMetadata()->getClientId(); |
| 62 |
|
| 63 |
$jwk = $this->jwk ?? JWKSet::createFromKeyData($client->getJwksProvider()->getJwks())->selectKey('sig'); |
| 64 |
|
| 65 |
if (null === $jwk) { |
| 66 |
throw new RuntimeException('Unable to get a client signature jwk'); |
| 67 |
} |
| 68 |
|
| 69 |
$time = time(); |
| 70 |
$jti = base64url_encode(random_bytes(32)); |
| 71 |
|
| 72 |
$payload = json_encode(array_merge( |
| 73 |
$claims, |
| 74 |
[ |
| 75 |
'iss' => $clientId, |
| 76 |
'sub' => $clientId, |
| 77 |
'aud' => get_endpoint_uri($client, 'token_endpoint'), |
| 78 |
'iat' => $time, |
| 79 |
'exp' => $time + $this->tokenTTL, |
| 80 |
'jti' => $jti, |
| 81 |
] |
| 82 |
), JSON_THROW_ON_ERROR); |
| 83 |
|
| 84 |
$jwkAlg = $jwk->get('alg'); |
| 85 |
|
| 86 |
if (! is_string($jwkAlg)) { |
| 87 |
throw new RuntimeException('Invalid JWK `alg` value'); |
| 88 |
} |
| 89 |
|
| 90 |
$jws = $this->jwsBuilder->create() |
| 91 |
->withPayload($payload) |
| 92 |
->addSignature($jwk, ['alg' => $jwkAlg, 'jti' => $jti]) |
| 93 |
->build(); |
| 94 |
|
| 95 |
return $this->jwsSerializer->serialize($jws, 0); |
| 96 |
} |
| 97 |
} |