| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\OpenIDClient\Claims; |
| 6 |
|
| 7 |
use Facile\OpenIDClient\AlgorithmManagerBuilder; |
| 8 |
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient; |
| 9 |
use Facile\OpenIDClient\Exception\InvalidArgumentException; |
| 10 |
use Facile\OpenIDClient\Exception\RuntimeException; |
| 11 |
use Facile\OpenIDClient\Issuer\IssuerBuilder; |
| 12 |
use Facile\OpenIDClient\Issuer\IssuerBuilderInterface; |
| 13 |
use Facile\OpenIDClient\Token\TokenSetInterface; |
| 14 |
use Jose\Component\Core\AlgorithmManager; |
| 15 |
use Jose\Component\Core\JWKSet; |
| 16 |
use Jose\Component\Signature\JWSVerifier; |
| 17 |
use Jose\Component\Signature\Serializer\CompactSerializer; |
| 18 |
use Jose\Component\Signature\Serializer\JWSSerializer; |
| 19 |
use JsonException; |
| 20 |
|
| 21 |
use function array_diff_key; |
| 22 |
use function array_flip; |
| 23 |
use function array_key_exists; |
| 24 |
use function count; |
| 25 |
use function explode; |
| 26 |
use function Facile\OpenIDClient\base64url_decode; |
| 27 |
use function json_decode; |
| 28 |
use function sprintf; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
abstract class AbstractClaims |
| 37 |
{ |
| 38 |
protected IssuerBuilderInterface $issuerBuilder; |
| 39 |
|
| 40 |
protected AlgorithmManager $algorithmManager; |
| 41 |
|
| 42 |
protected JWSVerifier $JWSVerifier; |
| 43 |
|
| 44 |
protected JWSSerializer $serializer; |
| 45 |
|
| 46 |
public function __construct( |
| 47 |
?IssuerBuilderInterface $issuerBuilder = null, |
| 48 |
?AlgorithmManager $algorithmManager = null, |
| 49 |
?JWSVerifier $JWSVerifier = null, |
| 50 |
?JWSSerializer $serializer = null |
| 51 |
) { |
| 52 |
$this->issuerBuilder = $issuerBuilder ?? new IssuerBuilder(); |
| 53 |
$this->algorithmManager = $algorithmManager ?? (new AlgorithmManagerBuilder())->build(); |
| 54 |
$this->JWSVerifier = $JWSVerifier ?? new JWSVerifier($this->algorithmManager); |
| 55 |
$this->serializer = $serializer ?? new CompactSerializer(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected function isAggregateSource(array $data): bool |
| 66 |
{ |
| 67 |
return array_key_exists('JWT', $data); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
protected function isDistributedSource(array $data): bool |
| 78 |
{ |
| 79 |
return array_key_exists('endpoint', $data); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
protected function claimJWT(OpenIDClient $client, string $jwt): array |
| 86 |
{ |
| 87 |
$issuer = $client->getIssuer(); |
| 88 |
|
| 89 |
try { |
| 90 |
|
| 91 |
$header = json_decode(base64url_decode(explode('.', $jwt)[0] ?? '{}'), true, 512, JSON_THROW_ON_ERROR); |
| 92 |
|
| 93 |
$payload = json_decode(base64url_decode(explode('.', $jwt)[1] ?? '{}'), true, 512, JSON_THROW_ON_ERROR); |
| 94 |
} catch (JsonException $e) { |
| 95 |
throw new InvalidArgumentException('Invalid JWT content', 0, $e); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
$alg = $header['alg'] ?? null; |
| 100 |
|
| 101 |
$kid = $header['kid'] ?? null; |
| 102 |
|
| 103 |
if (null === $alg) { |
| 104 |
throw new InvalidArgumentException('Claim source is missing JWT header alg property'); |
| 105 |
} |
| 106 |
|
| 107 |
if ('none' === $alg) { |
| 108 |
return $payload; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
$iss = $payload['iss'] ?? null; |
| 113 |
|
| 114 |
if (null === $iss || $iss === $issuer->getMetadata()->getIssuer()) { |
| 115 |
$jwks = JWKSet::createFromKeyData($issuer->getJwksProvider()->getJwks()); |
| 116 |
} else { |
| 117 |
$discovered = $this->issuerBuilder->build($iss); |
| 118 |
$jwks = JWKSet::createFromKeyData($discovered->getJwksProvider()->getJwks()); |
| 119 |
} |
| 120 |
|
| 121 |
$jws = $this->serializer->unserialize($jwt); |
| 122 |
|
| 123 |
$jwk = $jwks->selectKey('sig', $this->algorithmManager->get($alg), null !== $kid ? ['kid' => $kid] : []); |
| 124 |
|
| 125 |
if (null === $jwk) { |
| 126 |
throw new RuntimeException('Unable to get a key to verify claim source JWT'); |
| 127 |
} |
| 128 |
|
| 129 |
if (false === $this->JWSVerifier->verifyWithKey($jws, $jwk, 0)) { |
| 130 |
throw new InvalidArgumentException('Invalid claim source JWT signature'); |
| 131 |
} |
| 132 |
|
| 133 |
return $payload; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected function assignClaims(array $claims, array $sourceNames, array $sources): array |
| 148 |
{ |
| 149 |
foreach ($sourceNames as $claim => $inSource) { |
| 150 |
if (! array_key_exists($inSource, $sources)) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
if (! array_key_exists($claim, $sources[$inSource])) { |
| 155 |
throw new RuntimeException(sprintf('Unable to find claim "%s" in source "%s"', $claim, $inSource)); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
$value = $sources[$inSource][$claim]; |
| 160 |
$claims[$claim] = $value; |
| 161 |
|
| 162 |
$claims['_claim_names'] = array_diff_key($claims['_claim_names'] ?? [], array_flip([$claim])); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
return $claims; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
protected function cleanClaims(array $claims): array |
| 179 |
{ |
| 180 |
if (array_key_exists('_claim_names', $claims) && 0 === count($claims['_claim_names'] ?? [])) { |
| 181 |
|
| 182 |
$claims = array_diff_key($claims, array_flip(['_claim_names'])); |
| 183 |
} |
| 184 |
|
| 185 |
if (array_key_exists('_claim_sources', $claims) && 0 === count($claims['_claim_sources'] ?? [])) { |
| 186 |
|
| 187 |
$claims = array_diff_key($claims, array_flip(['_claim_sources'])); |
| 188 |
} |
| 189 |
|
| 190 |
return $claims; |
| 191 |
} |
| 192 |
} |