@@ -14,167 +14,167 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Claims implements \Countable, \IteratorAggregate |
| 16 | 16 | { |
| 17 | - use TypedClaims; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Claims. |
|
| 21 | - * |
|
| 22 | - * @var Claim[] |
|
| 23 | - */ |
|
| 24 | - protected $_claims; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param Claim ...$claims Zero or more claims |
|
| 30 | - */ |
|
| 31 | - public function __construct(Claim ...$claims) |
|
| 32 | - { |
|
| 33 | - $this->_claims = []; |
|
| 34 | - foreach ($claims as $claim) { |
|
| 35 | - $this->_claims[$claim->name()] = $claim; |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Convert to string. |
|
| 41 | - * |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public function __toString(): string |
|
| 45 | - { |
|
| 46 | - return $this->toJSON(); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Initialize from a JSON string. |
|
| 51 | - * |
|
| 52 | - * @param string $json JSON |
|
| 53 | - * |
|
| 54 | - * @throws \UnexpectedValueException If JSON is malformed |
|
| 55 | - * |
|
| 56 | - * @return self |
|
| 57 | - */ |
|
| 58 | - public static function fromJSON(string $json): self |
|
| 59 | - { |
|
| 60 | - $claims = []; |
|
| 61 | - $fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
| 62 | - if (!is_array($fields)) { |
|
| 63 | - throw new \UnexpectedValueException('Invalid JSON.'); |
|
| 64 | - } |
|
| 65 | - foreach ($fields as $name => $value) { |
|
| 66 | - $claims[] = Claim::fromNameAndValue($name, $value); |
|
| 67 | - } |
|
| 68 | - return new self(...$claims); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Get self with Claim objects added. |
|
| 73 | - * |
|
| 74 | - * @param Claim ...$claims One or more Claim objects |
|
| 75 | - * |
|
| 76 | - * @return self |
|
| 77 | - */ |
|
| 78 | - public function withClaims(Claim ...$claims): self |
|
| 79 | - { |
|
| 80 | - $obj = clone $this; |
|
| 81 | - foreach ($claims as $claim) { |
|
| 82 | - $obj->_claims[$claim->name()] = $claim; |
|
| 83 | - } |
|
| 84 | - return $obj; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Get all claims. |
|
| 89 | - * |
|
| 90 | - * @return Claim[] |
|
| 91 | - */ |
|
| 92 | - public function all(): array |
|
| 93 | - { |
|
| 94 | - return $this->_claims; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Check whether claim is present. |
|
| 99 | - * |
|
| 100 | - * @param string $name Claim name |
|
| 101 | - * |
|
| 102 | - * @return true |
|
| 103 | - */ |
|
| 104 | - public function has(string $name): bool |
|
| 105 | - { |
|
| 106 | - return isset($this->_claims[$name]); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get claim by name. |
|
| 111 | - * |
|
| 112 | - * @param string $name Claim name |
|
| 113 | - * |
|
| 114 | - * @throws \LogicException If claim is not present |
|
| 115 | - * |
|
| 116 | - * @return Claim |
|
| 117 | - */ |
|
| 118 | - public function get(string $name): Claim |
|
| 119 | - { |
|
| 120 | - if (!isset($this->_claims[$name])) { |
|
| 121 | - throw new \LogicException("Claim {$name} not set."); |
|
| 122 | - } |
|
| 123 | - return $this->_claims[$name]; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Convert to a JSON. |
|
| 128 | - * |
|
| 129 | - * @return string |
|
| 130 | - */ |
|
| 131 | - public function toJSON(): string |
|
| 132 | - { |
|
| 133 | - $data = []; |
|
| 134 | - foreach ($this->_claims as $claim) { |
|
| 135 | - $data[$claim->name()] = $claim->value(); |
|
| 136 | - } |
|
| 137 | - return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Check whether a claims set is valid in the given context. |
|
| 142 | - * |
|
| 143 | - * @param ValidationContext $ctx Validation context |
|
| 144 | - * |
|
| 145 | - * @return bool |
|
| 146 | - */ |
|
| 147 | - public function isValid(ValidationContext $ctx): bool |
|
| 148 | - { |
|
| 149 | - try { |
|
| 150 | - $ctx->validate($this); |
|
| 151 | - } catch (\RuntimeException $e) { |
|
| 152 | - return false; |
|
| 153 | - } |
|
| 154 | - return true; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Get the number of claims. |
|
| 159 | - * |
|
| 160 | - * @see \Countable::count() |
|
| 161 | - * |
|
| 162 | - * @return int |
|
| 163 | - */ |
|
| 164 | - public function count(): int |
|
| 165 | - { |
|
| 166 | - return count($this->_claims); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Get iterator for Claim objects keyed by claim name. |
|
| 171 | - * |
|
| 172 | - * @see \IteratorAggregate::getIterator() |
|
| 173 | - * |
|
| 174 | - * @return \ArrayIterator |
|
| 175 | - */ |
|
| 176 | - public function getIterator(): \ArrayIterator |
|
| 177 | - { |
|
| 178 | - return new \ArrayIterator($this->_claims); |
|
| 179 | - } |
|
| 17 | + use TypedClaims; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Claims. |
|
| 21 | + * |
|
| 22 | + * @var Claim[] |
|
| 23 | + */ |
|
| 24 | + protected $_claims; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param Claim ...$claims Zero or more claims |
|
| 30 | + */ |
|
| 31 | + public function __construct(Claim ...$claims) |
|
| 32 | + { |
|
| 33 | + $this->_claims = []; |
|
| 34 | + foreach ($claims as $claim) { |
|
| 35 | + $this->_claims[$claim->name()] = $claim; |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Convert to string. |
|
| 41 | + * |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public function __toString(): string |
|
| 45 | + { |
|
| 46 | + return $this->toJSON(); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Initialize from a JSON string. |
|
| 51 | + * |
|
| 52 | + * @param string $json JSON |
|
| 53 | + * |
|
| 54 | + * @throws \UnexpectedValueException If JSON is malformed |
|
| 55 | + * |
|
| 56 | + * @return self |
|
| 57 | + */ |
|
| 58 | + public static function fromJSON(string $json): self |
|
| 59 | + { |
|
| 60 | + $claims = []; |
|
| 61 | + $fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
| 62 | + if (!is_array($fields)) { |
|
| 63 | + throw new \UnexpectedValueException('Invalid JSON.'); |
|
| 64 | + } |
|
| 65 | + foreach ($fields as $name => $value) { |
|
| 66 | + $claims[] = Claim::fromNameAndValue($name, $value); |
|
| 67 | + } |
|
| 68 | + return new self(...$claims); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Get self with Claim objects added. |
|
| 73 | + * |
|
| 74 | + * @param Claim ...$claims One or more Claim objects |
|
| 75 | + * |
|
| 76 | + * @return self |
|
| 77 | + */ |
|
| 78 | + public function withClaims(Claim ...$claims): self |
|
| 79 | + { |
|
| 80 | + $obj = clone $this; |
|
| 81 | + foreach ($claims as $claim) { |
|
| 82 | + $obj->_claims[$claim->name()] = $claim; |
|
| 83 | + } |
|
| 84 | + return $obj; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Get all claims. |
|
| 89 | + * |
|
| 90 | + * @return Claim[] |
|
| 91 | + */ |
|
| 92 | + public function all(): array |
|
| 93 | + { |
|
| 94 | + return $this->_claims; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Check whether claim is present. |
|
| 99 | + * |
|
| 100 | + * @param string $name Claim name |
|
| 101 | + * |
|
| 102 | + * @return true |
|
| 103 | + */ |
|
| 104 | + public function has(string $name): bool |
|
| 105 | + { |
|
| 106 | + return isset($this->_claims[$name]); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get claim by name. |
|
| 111 | + * |
|
| 112 | + * @param string $name Claim name |
|
| 113 | + * |
|
| 114 | + * @throws \LogicException If claim is not present |
|
| 115 | + * |
|
| 116 | + * @return Claim |
|
| 117 | + */ |
|
| 118 | + public function get(string $name): Claim |
|
| 119 | + { |
|
| 120 | + if (!isset($this->_claims[$name])) { |
|
| 121 | + throw new \LogicException("Claim {$name} not set."); |
|
| 122 | + } |
|
| 123 | + return $this->_claims[$name]; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Convert to a JSON. |
|
| 128 | + * |
|
| 129 | + * @return string |
|
| 130 | + */ |
|
| 131 | + public function toJSON(): string |
|
| 132 | + { |
|
| 133 | + $data = []; |
|
| 134 | + foreach ($this->_claims as $claim) { |
|
| 135 | + $data[$claim->name()] = $claim->value(); |
|
| 136 | + } |
|
| 137 | + return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Check whether a claims set is valid in the given context. |
|
| 142 | + * |
|
| 143 | + * @param ValidationContext $ctx Validation context |
|
| 144 | + * |
|
| 145 | + * @return bool |
|
| 146 | + */ |
|
| 147 | + public function isValid(ValidationContext $ctx): bool |
|
| 148 | + { |
|
| 149 | + try { |
|
| 150 | + $ctx->validate($this); |
|
| 151 | + } catch (\RuntimeException $e) { |
|
| 152 | + return false; |
|
| 153 | + } |
|
| 154 | + return true; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Get the number of claims. |
|
| 159 | + * |
|
| 160 | + * @see \Countable::count() |
|
| 161 | + * |
|
| 162 | + * @return int |
|
| 163 | + */ |
|
| 164 | + public function count(): int |
|
| 165 | + { |
|
| 166 | + return count($this->_claims); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Get iterator for Claim objects keyed by claim name. |
|
| 171 | + * |
|
| 172 | + * @see \IteratorAggregate::getIterator() |
|
| 173 | + * |
|
| 174 | + * @return \ArrayIterator |
|
| 175 | + */ |
|
| 176 | + public function getIterator(): \ArrayIterator |
|
| 177 | + { |
|
| 178 | + return new \ArrayIterator($this->_claims); |
|
| 179 | + } |
|
| 180 | 180 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT; |
| 6 | 6 | |
@@ -11,84 +11,84 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class RegisteredClaim extends Claim |
| 13 | 13 | { |
| 14 | - // JWT claims |
|
| 15 | - const NAME_ISSUER = 'iss'; |
|
| 16 | - const NAME_SUBJECT = 'sub'; |
|
| 17 | - const NAME_AUDIENCE = 'aud'; |
|
| 18 | - const NAME_EXPIRATION_TIME = 'exp'; |
|
| 19 | - const NAME_NOT_BEFORE = 'nbf'; |
|
| 20 | - const NAME_ISSUED_AT = 'iat'; |
|
| 21 | - const NAME_JWT_ID = 'jti'; |
|
| 14 | + // JWT claims |
|
| 15 | + const NAME_ISSUER = 'iss'; |
|
| 16 | + const NAME_SUBJECT = 'sub'; |
|
| 17 | + const NAME_AUDIENCE = 'aud'; |
|
| 18 | + const NAME_EXPIRATION_TIME = 'exp'; |
|
| 19 | + const NAME_NOT_BEFORE = 'nbf'; |
|
| 20 | + const NAME_ISSUED_AT = 'iat'; |
|
| 21 | + const NAME_JWT_ID = 'jti'; |
|
| 22 | 22 | |
| 23 | - // OpenID claims |
|
| 24 | - const NAME_FULL_NAME = 'name'; |
|
| 25 | - const NAME_GIVEN_NAME = 'given_name'; |
|
| 26 | - const NAME_FAMILY_NAME = 'family_name'; |
|
| 27 | - const NAME_MIDDLE_NAME = 'middle_name'; |
|
| 28 | - const NAME_NICKNAME = 'nickname'; |
|
| 29 | - const NAME_PREFERRED_USERNAME = 'preferred_username'; |
|
| 30 | - const NAME_PROFILE_URL = 'profile'; |
|
| 31 | - const NAME_PICTURE_URL = 'picture'; |
|
| 32 | - const NAME_WEBSITE_URL = 'website'; |
|
| 33 | - const NAME_EMAIL = 'email'; |
|
| 34 | - const NAME_EMAIL_VERIFIED = 'email_verified'; |
|
| 35 | - const NAME_GENDER = 'gender'; |
|
| 36 | - const NAME_BIRTHDATE = 'birthdate'; |
|
| 37 | - const NAME_TIMEZONE = 'zoneinfo'; |
|
| 38 | - const NAME_LOCALE = 'locale'; |
|
| 39 | - const NAME_PHONE_NUMBER = 'phone_number'; |
|
| 40 | - const NAME_PHONE_NUMBER_VERIFIED = 'phone_number_verified'; |
|
| 41 | - const NAME_ADDRESS = 'address'; |
|
| 42 | - const NAME_UPDATED_AT = 'updated_at'; |
|
| 43 | - const NAME_AUTHORIZED_PARTY = 'azp'; |
|
| 44 | - const NAME_NONCE = 'nonce'; |
|
| 45 | - const NAME_AUTH_TIME = 'auth_time'; |
|
| 46 | - const NAME_ACCESS_TOKEN_HASH = 'at_hash'; |
|
| 47 | - const NAME_CODE_HASH = 'c_hash'; |
|
| 48 | - const NAME_ACR = 'acr'; |
|
| 49 | - const NAME_AMR = 'amr'; |
|
| 50 | - const NAME_SUB_JWK = 'sub_jwk'; |
|
| 23 | + // OpenID claims |
|
| 24 | + const NAME_FULL_NAME = 'name'; |
|
| 25 | + const NAME_GIVEN_NAME = 'given_name'; |
|
| 26 | + const NAME_FAMILY_NAME = 'family_name'; |
|
| 27 | + const NAME_MIDDLE_NAME = 'middle_name'; |
|
| 28 | + const NAME_NICKNAME = 'nickname'; |
|
| 29 | + const NAME_PREFERRED_USERNAME = 'preferred_username'; |
|
| 30 | + const NAME_PROFILE_URL = 'profile'; |
|
| 31 | + const NAME_PICTURE_URL = 'picture'; |
|
| 32 | + const NAME_WEBSITE_URL = 'website'; |
|
| 33 | + const NAME_EMAIL = 'email'; |
|
| 34 | + const NAME_EMAIL_VERIFIED = 'email_verified'; |
|
| 35 | + const NAME_GENDER = 'gender'; |
|
| 36 | + const NAME_BIRTHDATE = 'birthdate'; |
|
| 37 | + const NAME_TIMEZONE = 'zoneinfo'; |
|
| 38 | + const NAME_LOCALE = 'locale'; |
|
| 39 | + const NAME_PHONE_NUMBER = 'phone_number'; |
|
| 40 | + const NAME_PHONE_NUMBER_VERIFIED = 'phone_number_verified'; |
|
| 41 | + const NAME_ADDRESS = 'address'; |
|
| 42 | + const NAME_UPDATED_AT = 'updated_at'; |
|
| 43 | + const NAME_AUTHORIZED_PARTY = 'azp'; |
|
| 44 | + const NAME_NONCE = 'nonce'; |
|
| 45 | + const NAME_AUTH_TIME = 'auth_time'; |
|
| 46 | + const NAME_ACCESS_TOKEN_HASH = 'at_hash'; |
|
| 47 | + const NAME_CODE_HASH = 'c_hash'; |
|
| 48 | + const NAME_ACR = 'acr'; |
|
| 49 | + const NAME_AMR = 'amr'; |
|
| 50 | + const NAME_SUB_JWK = 'sub_jwk'; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Mapping from registered claim name to class name. |
|
| 54 | - * |
|
| 55 | - * @internal |
|
| 56 | - * |
|
| 57 | - * @var array |
|
| 58 | - */ |
|
| 59 | - const MAP_NAME_TO_CLASS = [ |
|
| 60 | - self::NAME_ISSUER => IssuerClaim::class, |
|
| 61 | - self::NAME_SUBJECT => SubjectClaim::class, |
|
| 62 | - self::NAME_AUDIENCE => AudienceClaim::class, |
|
| 63 | - self::NAME_EXPIRATION_TIME => ExpirationTimeClaim::class, |
|
| 64 | - self::NAME_NOT_BEFORE => NotBeforeClaim::class, |
|
| 65 | - self::NAME_ISSUED_AT => IssuedAtClaim::class, |
|
| 66 | - self::NAME_JWT_ID => JWTIDClaim::class, |
|
| 67 | - ]; |
|
| 52 | + /** |
|
| 53 | + * Mapping from registered claim name to class name. |
|
| 54 | + * |
|
| 55 | + * @internal |
|
| 56 | + * |
|
| 57 | + * @var array |
|
| 58 | + */ |
|
| 59 | + const MAP_NAME_TO_CLASS = [ |
|
| 60 | + self::NAME_ISSUER => IssuerClaim::class, |
|
| 61 | + self::NAME_SUBJECT => SubjectClaim::class, |
|
| 62 | + self::NAME_AUDIENCE => AudienceClaim::class, |
|
| 63 | + self::NAME_EXPIRATION_TIME => ExpirationTimeClaim::class, |
|
| 64 | + self::NAME_NOT_BEFORE => NotBeforeClaim::class, |
|
| 65 | + self::NAME_ISSUED_AT => IssuedAtClaim::class, |
|
| 66 | + self::NAME_JWT_ID => JWTIDClaim::class, |
|
| 67 | + ]; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Constructor. |
|
| 71 | - * |
|
| 72 | - * Defined here for type strictness. Parameters are passed to the |
|
| 73 | - * superclass. |
|
| 74 | - * |
|
| 75 | - * @param mixed ...$args |
|
| 76 | - */ |
|
| 77 | - public function __construct(...$args) |
|
| 78 | - { |
|
| 79 | - parent::__construct((string) $args[0], $args[1], |
|
| 80 | - isset($args[2]) ? $args[2] : null); |
|
| 81 | - } |
|
| 69 | + /** |
|
| 70 | + * Constructor. |
|
| 71 | + * |
|
| 72 | + * Defined here for type strictness. Parameters are passed to the |
|
| 73 | + * superclass. |
|
| 74 | + * |
|
| 75 | + * @param mixed ...$args |
|
| 76 | + */ |
|
| 77 | + public function __construct(...$args) |
|
| 78 | + { |
|
| 79 | + parent::__construct((string) $args[0], $args[1], |
|
| 80 | + isset($args[2]) ? $args[2] : null); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Initialize concrete claim instance from a JSON value. |
|
| 85 | - * |
|
| 86 | - * @param mixed $value |
|
| 87 | - * |
|
| 88 | - * @return RegisteredClaim |
|
| 89 | - */ |
|
| 90 | - public static function fromJSONValue($value): RegisteredClaim |
|
| 91 | - { |
|
| 92 | - return new static($value); |
|
| 93 | - } |
|
| 83 | + /** |
|
| 84 | + * Initialize concrete claim instance from a JSON value. |
|
| 85 | + * |
|
| 86 | + * @param mixed $value |
|
| 87 | + * |
|
| 88 | + * @return RegisteredClaim |
|
| 89 | + */ |
|
| 90 | + public static function fromJSONValue($value): RegisteredClaim |
|
| 91 | + { |
|
| 92 | + return new static($value); |
|
| 93 | + } |
|
| 94 | 94 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT\Claim; |
| 6 | 6 | |
@@ -15,18 +15,18 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class ExpirationTimeClaim extends RegisteredClaim |
| 17 | 17 | { |
| 18 | - use NumericDateClaim; |
|
| 19 | - use ReferenceTimeValidation; |
|
| 18 | + use NumericDateClaim; |
|
| 19 | + use ReferenceTimeValidation; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param int $exp_time Expiration time as a unix timestamp |
|
| 25 | - */ |
|
| 26 | - public function __construct(int $exp_time) |
|
| 27 | - { |
|
| 28 | - // validate that claim is after the constraint (reference time) |
|
| 29 | - parent::__construct(self::NAME_EXPIRATION_TIME, $exp_time, |
|
| 30 | - new GreaterValidator()); |
|
| 31 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param int $exp_time Expiration time as a unix timestamp |
|
| 25 | + */ |
|
| 26 | + public function __construct(int $exp_time) |
|
| 27 | + { |
|
| 28 | + // validate that claim is after the constraint (reference time) |
|
| 29 | + parent::__construct(self::NAME_EXPIRATION_TIME, $exp_time, |
|
| 30 | + new GreaterValidator()); |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT\Claim; |
| 6 | 6 | |
@@ -9,99 +9,99 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | trait NumericDateClaim |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Constructor. |
|
| 14 | - * |
|
| 15 | - * @param int $timestamp Unix timestamp |
|
| 16 | - */ |
|
| 17 | - abstract public function __construct(int $timestamp); |
|
| 12 | + /** |
|
| 13 | + * Constructor. |
|
| 14 | + * |
|
| 15 | + * @param int $timestamp Unix timestamp |
|
| 16 | + */ |
|
| 17 | + abstract public function __construct(int $timestamp); |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Get the parameter value. |
|
| 21 | - * |
|
| 22 | - * @return string |
|
| 23 | - */ |
|
| 24 | - abstract public function value(); |
|
| 19 | + /** |
|
| 20 | + * Get the parameter value. |
|
| 21 | + * |
|
| 22 | + * @return string |
|
| 23 | + */ |
|
| 24 | + abstract public function value(); |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Initialize instance from date/time string. |
|
| 28 | - * |
|
| 29 | - * @param string $time <code>strtotime</code> compatible time string |
|
| 30 | - * @param string $tz Default timezone |
|
| 31 | - * |
|
| 32 | - * @throws \RuntimeException |
|
| 33 | - * |
|
| 34 | - * @return static |
|
| 35 | - */ |
|
| 36 | - public static function fromString(string $time, string $tz = 'UTC') |
|
| 37 | - { |
|
| 38 | - try { |
|
| 39 | - $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 40 | - return new static($dt->getTimestamp()); |
|
| 41 | - } catch (\Exception $e) { |
|
| 42 | - throw new \RuntimeException( |
|
| 43 | - 'Failed to create DateTime: ' . |
|
| 44 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 45 | - } |
|
| 46 | - } |
|
| 26 | + /** |
|
| 27 | + * Initialize instance from date/time string. |
|
| 28 | + * |
|
| 29 | + * @param string $time <code>strtotime</code> compatible time string |
|
| 30 | + * @param string $tz Default timezone |
|
| 31 | + * |
|
| 32 | + * @throws \RuntimeException |
|
| 33 | + * |
|
| 34 | + * @return static |
|
| 35 | + */ |
|
| 36 | + public static function fromString(string $time, string $tz = 'UTC') |
|
| 37 | + { |
|
| 38 | + try { |
|
| 39 | + $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 40 | + return new static($dt->getTimestamp()); |
|
| 41 | + } catch (\Exception $e) { |
|
| 42 | + throw new \RuntimeException( |
|
| 43 | + 'Failed to create DateTime: ' . |
|
| 44 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Get date as a unix timestamp. |
|
| 50 | - * |
|
| 51 | - * @return int |
|
| 52 | - */ |
|
| 53 | - public function timestamp(): int |
|
| 54 | - { |
|
| 55 | - return (int) $this->value(); |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Get date as a unix timestamp. |
|
| 50 | + * |
|
| 51 | + * @return int |
|
| 52 | + */ |
|
| 53 | + public function timestamp(): int |
|
| 54 | + { |
|
| 55 | + return (int) $this->value(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Get date as a datetime object. |
|
| 60 | - * |
|
| 61 | - * @param string $tz Timezone |
|
| 62 | - * |
|
| 63 | - * @throws \RuntimeException |
|
| 64 | - * |
|
| 65 | - * @return \DateTimeImmutable |
|
| 66 | - */ |
|
| 67 | - public function dateTime(string $tz = 'UTC'): \DateTimeImmutable |
|
| 68 | - { |
|
| 69 | - $dt = \DateTimeImmutable::createFromFormat('!U', strval($this->value()), |
|
| 70 | - self::_createTimeZone($tz)); |
|
| 71 | - if (false === $dt) { |
|
| 72 | - throw new \RuntimeException( |
|
| 73 | - 'Failed to create DateTime: ' . |
|
| 74 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 75 | - } |
|
| 76 | - return $dt; |
|
| 77 | - } |
|
| 58 | + /** |
|
| 59 | + * Get date as a datetime object. |
|
| 60 | + * |
|
| 61 | + * @param string $tz Timezone |
|
| 62 | + * |
|
| 63 | + * @throws \RuntimeException |
|
| 64 | + * |
|
| 65 | + * @return \DateTimeImmutable |
|
| 66 | + */ |
|
| 67 | + public function dateTime(string $tz = 'UTC'): \DateTimeImmutable |
|
| 68 | + { |
|
| 69 | + $dt = \DateTimeImmutable::createFromFormat('!U', strval($this->value()), |
|
| 70 | + self::_createTimeZone($tz)); |
|
| 71 | + if (false === $dt) { |
|
| 72 | + throw new \RuntimeException( |
|
| 73 | + 'Failed to create DateTime: ' . |
|
| 74 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 75 | + } |
|
| 76 | + return $dt; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Create DateTimeZone object from string. |
|
| 81 | - * |
|
| 82 | - * @param string $tz |
|
| 83 | - * |
|
| 84 | - * @throws \UnexpectedValueException |
|
| 85 | - * |
|
| 86 | - * @return \DateTimeZone |
|
| 87 | - */ |
|
| 88 | - private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 89 | - { |
|
| 90 | - try { |
|
| 91 | - return new \DateTimeZone($tz); |
|
| 92 | - } catch (\Exception $e) { |
|
| 93 | - throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
| 94 | - } |
|
| 95 | - } |
|
| 79 | + /** |
|
| 80 | + * Create DateTimeZone object from string. |
|
| 81 | + * |
|
| 82 | + * @param string $tz |
|
| 83 | + * |
|
| 84 | + * @throws \UnexpectedValueException |
|
| 85 | + * |
|
| 86 | + * @return \DateTimeZone |
|
| 87 | + */ |
|
| 88 | + private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 89 | + { |
|
| 90 | + try { |
|
| 91 | + return new \DateTimeZone($tz); |
|
| 92 | + } catch (\Exception $e) { |
|
| 93 | + throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Get last error caused by DateTimeImmutable. |
|
| 99 | - * |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 103 | - { |
|
| 104 | - $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
| 105 | - return implode(', ', $errors); |
|
| 106 | - } |
|
| 97 | + /** |
|
| 98 | + * Get last error caused by DateTimeImmutable. |
|
| 99 | + * |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 103 | + { |
|
| 104 | + $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
| 105 | + return implode(', ', $errors); |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT\Claim\Feature; |
| 6 | 6 | |
@@ -11,39 +11,39 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | trait ReferenceTimeValidation |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Validate the claim against given constraint. |
|
| 16 | - * |
|
| 17 | - * @param mixed $constraint |
|
| 18 | - * |
|
| 19 | - * @return bool |
|
| 20 | - */ |
|
| 21 | - abstract public function validate($constraint): bool; |
|
| 14 | + /** |
|
| 15 | + * Validate the claim against given constraint. |
|
| 16 | + * |
|
| 17 | + * @param mixed $constraint |
|
| 18 | + * |
|
| 19 | + * @return bool |
|
| 20 | + */ |
|
| 21 | + abstract public function validate($constraint): bool; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Override default Claim validation. |
|
| 25 | - * |
|
| 26 | - * Uses reference time of the validation context as a constraint. |
|
| 27 | - * |
|
| 28 | - * @see \Sop\JWX\JWT\Claim\Claim::validateWithContext() |
|
| 29 | - * |
|
| 30 | - * @param ValidationContext $ctx |
|
| 31 | - * |
|
| 32 | - * @return bool |
|
| 33 | - */ |
|
| 34 | - public function validateWithContext(ValidationContext $ctx): bool |
|
| 35 | - { |
|
| 36 | - if ($ctx->hasReferenceTime()) { |
|
| 37 | - // try to validate with leeway added |
|
| 38 | - if ($this->validate($ctx->referenceTime() + $ctx->leeway())) { |
|
| 39 | - return true; |
|
| 40 | - } |
|
| 41 | - // try to validate with leeway substracted |
|
| 42 | - if ($this->validate($ctx->referenceTime() - $ctx->leeway())) { |
|
| 43 | - return true; |
|
| 44 | - } |
|
| 45 | - return false; |
|
| 46 | - } |
|
| 47 | - return true; |
|
| 48 | - } |
|
| 23 | + /** |
|
| 24 | + * Override default Claim validation. |
|
| 25 | + * |
|
| 26 | + * Uses reference time of the validation context as a constraint. |
|
| 27 | + * |
|
| 28 | + * @see \Sop\JWX\JWT\Claim\Claim::validateWithContext() |
|
| 29 | + * |
|
| 30 | + * @param ValidationContext $ctx |
|
| 31 | + * |
|
| 32 | + * @return bool |
|
| 33 | + */ |
|
| 34 | + public function validateWithContext(ValidationContext $ctx): bool |
|
| 35 | + { |
|
| 36 | + if ($ctx->hasReferenceTime()) { |
|
| 37 | + // try to validate with leeway added |
|
| 38 | + if ($this->validate($ctx->referenceTime() + $ctx->leeway())) { |
|
| 39 | + return true; |
|
| 40 | + } |
|
| 41 | + // try to validate with leeway substracted |
|
| 42 | + if ($this->validate($ctx->referenceTime() - $ctx->leeway())) { |
|
| 43 | + return true; |
|
| 44 | + } |
|
| 45 | + return false; |
|
| 46 | + } |
|
| 47 | + return true; |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT\Claim\Feature; |
| 6 | 6 | |
@@ -13,13 +13,13 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class IssuerClaim extends RegisteredClaim |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $issuer |
|
| 20 | - */ |
|
| 21 | - public function __construct(string $issuer) |
|
| 22 | - { |
|
| 23 | - parent::__construct(self::NAME_ISSUER, $issuer, new EqualsValidator()); |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $issuer |
|
| 20 | + */ |
|
| 21 | + public function __construct(string $issuer) |
|
| 22 | + { |
|
| 23 | + parent::__construct(self::NAME_ISSUER, $issuer, new EqualsValidator()); |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT\Claim; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT\Claim; |
| 6 | 6 | |
@@ -9,11 +9,11 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class EqualsValidator extends Validator |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * {@inheritdoc} |
|
| 14 | - */ |
|
| 15 | - public function validate($value, $constraint): bool |
|
| 16 | - { |
|
| 17 | - return $value == $constraint; |
|
| 18 | - } |
|
| 12 | + /** |
|
| 13 | + * {@inheritdoc} |
|
| 14 | + */ |
|
| 15 | + public function validate($value, $constraint): bool |
|
| 16 | + { |
|
| 17 | + return $value == $constraint; |
|
| 18 | + } |
|
| 19 | 19 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT\Claim\Validator; |
| 6 | 6 | |
@@ -12,14 +12,14 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class ContainsValidator extends Validator |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - public function validate($value, $constraint): bool |
|
| 19 | - { |
|
| 20 | - if (is_array($value)) { |
|
| 21 | - return in_array($constraint, $value); |
|
| 22 | - } |
|
| 23 | - return $value == $constraint; |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + public function validate($value, $constraint): bool |
|
| 19 | + { |
|
| 20 | + if (is_array($value)) { |
|
| 21 | + return in_array($constraint, $value); |
|
| 22 | + } |
|
| 23 | + return $value == $constraint; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWT\Claim\Validator; |
| 6 | 6 | |