| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Checker; |
| 6 |
|
| 7 |
use function in_array; |
| 8 |
use function is_array; |
| 9 |
use function is_string; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
final class AudienceChecker implements ClaimChecker, HeaderChecker |
| 16 |
{ |
| 17 |
private const CLAIM_NAME = 'aud'; |
| 18 |
|
| 19 |
public function __construct( |
| 20 |
private readonly string $audience, |
| 21 |
private readonly bool $protectedHeader = false |
| 22 |
) { |
| 23 |
} |
| 24 |
|
| 25 |
public function checkClaim(mixed $value): void |
| 26 |
{ |
| 27 |
$this->checkValue($value, InvalidClaimException::class); |
| 28 |
} |
| 29 |
|
| 30 |
public function checkHeader(mixed $value): void |
| 31 |
{ |
| 32 |
$this->checkValue($value, InvalidHeaderException::class); |
| 33 |
} |
| 34 |
|
| 35 |
public function supportedClaim(): string |
| 36 |
{ |
| 37 |
return self::CLAIM_NAME; |
| 38 |
} |
| 39 |
|
| 40 |
public function supportedHeader(): string |
| 41 |
{ |
| 42 |
return self::CLAIM_NAME; |
| 43 |
} |
| 44 |
|
| 45 |
public function protectedHeaderOnly(): bool |
| 46 |
{ |
| 47 |
return $this->protectedHeader; |
| 48 |
} |
| 49 |
|
| 50 |
private function checkValue(mixed $value, string $class): void |
| 51 |
{ |
| 52 |
if (is_string($value) && $value !== $this->audience) { |
| 53 |
throw new $class('Bad audience.', self::CLAIM_NAME, $value); |
| 54 |
} |
| 55 |
if (is_array($value) && ! in_array($this->audience, $value, true)) { |
| 56 |
throw new $class('Bad audience.', self::CLAIM_NAME, $value); |
| 57 |
} |
| 58 |
if (! is_array($value) && ! is_string($value)) { |
| 59 |
throw new $class('Bad audience.', self::CLAIM_NAME, $value); |
| 60 |
} |
| 61 |
} |
| 62 |
} |