|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Pdsinterop\Solid\Auth\Config; |
| 4 | + |
| 5 | +use JsonSerializable; |
| 6 | +use Pdsinterop\Solid\Auth\Enum\OpenId\OpenIdConnectMetadata as OidcMeta; |
| 7 | +use Pdsinterop\Solid\Auth\Exception\LogicException; |
| 8 | + |
| 9 | +class Server implements JsonSerializable |
| 10 | +{ |
| 11 | + ////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 12 | + |
| 13 | + /** @var array */ |
| 14 | + private $data; |
| 15 | + /** @var bool */ |
| 16 | + private $strict; |
| 17 | + |
| 18 | + //////////////////////////// GETTERS AND SETTERS \\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 19 | + |
| 20 | + private function getRecommended() : array |
| 21 | + { |
| 22 | + return [ |
| 23 | + OidcMeta::CLAIMS_SUPPORTED, |
| 24 | + OidcMeta::REGISTRATION_ENDPOINT, |
| 25 | + OidcMeta::SCOPES_SUPPORTED, |
| 26 | + OidcMeta::USERINFO_ENDPOINT, |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + private function getRequired() : array |
| 31 | + { |
| 32 | + $required = [ |
| 33 | + OidcMeta::AUTHORIZATION_ENDPOINT, |
| 34 | + OidcMeta::ID_TOKEN_SIGNING_ALG_VALUES_SUPPORTED, |
| 35 | + OidcMeta::ISSUER, |
| 36 | + OidcMeta::JWKS_URI, |
| 37 | + OidcMeta::RESPONSE_TYPES_SUPPORTED, |
| 38 | + OidcMeta::SUBJECT_TYPES_SUPPORTED, |
| 39 | + ]; |
| 40 | + |
| 41 | + if ($this->strict === true) { |
| 42 | + $required = array_merge($required, $this->getRecommended()); |
| 43 | + } |
| 44 | + |
| 45 | + return $required; |
| 46 | + } |
| 47 | + |
| 48 | + //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 49 | + |
| 50 | + final public function __construct(array $data, bool $strict = false) |
| 51 | + { |
| 52 | + $this->data = array_filter($data, [OidcMeta::class, 'has']); |
| 53 | + $this->strict = $strict; |
| 54 | + } |
| 55 | + |
| 56 | + final public function __toString() : string |
| 57 | + { |
| 58 | + return (string) json_encode($this); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return array |
| 63 | + * |
| 64 | + * @throws LogicException for missing required properties |
| 65 | + */ |
| 66 | + final public function jsonSerialize() : array |
| 67 | + { |
| 68 | + $data = $this->data; |
| 69 | + |
| 70 | + if ($this->validate() === false) { |
| 71 | + $missing = $this->checkForMissing($data, $this->getRequired()); |
| 72 | + throw new LogicException('Required properties have not been set: ' . implode(', ', $missing)); |
| 73 | + } |
| 74 | + |
| 75 | + return $data; |
| 76 | + } |
| 77 | + |
| 78 | + final public function validate() : bool |
| 79 | + { |
| 80 | + return $this->checkForMissing($this->data, $this->getRequired()) === []; |
| 81 | + } |
| 82 | + |
| 83 | + ////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 84 | + |
| 85 | + private function checkForMissing(array $data, array $required) : array |
| 86 | + { |
| 87 | + $available = array_keys($data); |
| 88 | + |
| 89 | + return array_diff($required, $available); |
| 90 | + } |
| 91 | +} |
0 commit comments