@@ -11,36 +11,36 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | trait ReferenceTimeValidation |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Validate the claim against given constraint. |
|
| 16 | - * |
|
| 17 | - * @param mixed $constraint |
|
| 18 | - * @return bool |
|
| 19 | - */ |
|
| 20 | - abstract public function validate($constraint): bool; |
|
| 14 | + /** |
|
| 15 | + * Validate the claim against given constraint. |
|
| 16 | + * |
|
| 17 | + * @param mixed $constraint |
|
| 18 | + * @return bool |
|
| 19 | + */ |
|
| 20 | + abstract public function validate($constraint): bool; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Override default Claim validation. |
|
| 24 | - * |
|
| 25 | - * Uses reference time of the validation context as a constraint. |
|
| 26 | - * |
|
| 27 | - * @see \JWX\JWT\Claim\Claim::validateWithContext() |
|
| 28 | - * @param ValidationContext $ctx |
|
| 29 | - * @return bool |
|
| 30 | - */ |
|
| 31 | - public function validateWithContext(ValidationContext $ctx): bool |
|
| 32 | - { |
|
| 33 | - if ($ctx->hasReferenceTime()) { |
|
| 34 | - // try to validate with leeway added |
|
| 35 | - if ($this->validate($ctx->referenceTime() + $ctx->leeway())) { |
|
| 36 | - return true; |
|
| 37 | - } |
|
| 38 | - // try to validate with leeway substracted |
|
| 39 | - if ($this->validate($ctx->referenceTime() - $ctx->leeway())) { |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 42 | - return false; |
|
| 43 | - } |
|
| 44 | - return true; |
|
| 45 | - } |
|
| 22 | + /** |
|
| 23 | + * Override default Claim validation. |
|
| 24 | + * |
|
| 25 | + * Uses reference time of the validation context as a constraint. |
|
| 26 | + * |
|
| 27 | + * @see \JWX\JWT\Claim\Claim::validateWithContext() |
|
| 28 | + * @param ValidationContext $ctx |
|
| 29 | + * @return bool |
|
| 30 | + */ |
|
| 31 | + public function validateWithContext(ValidationContext $ctx): bool |
|
| 32 | + { |
|
| 33 | + if ($ctx->hasReferenceTime()) { |
|
| 34 | + // try to validate with leeway added |
|
| 35 | + if ($this->validate($ctx->referenceTime() + $ctx->leeway())) { |
|
| 36 | + return true; |
|
| 37 | + } |
|
| 38 | + // try to validate with leeway substracted |
|
| 39 | + if ($this->validate($ctx->referenceTime() - $ctx->leeway())) { |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | + return false; |
|
| 43 | + } |
|
| 44 | + return true; |
|
| 45 | + } |
|
| 46 | 46 | } |
@@ -9,93 +9,93 @@ |
||
| 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 | - * @throws \RuntimeException |
|
| 32 | - * @return static |
|
| 33 | - */ |
|
| 34 | - public static function fromString(string $time, string $tz = "UTC") |
|
| 35 | - { |
|
| 36 | - try { |
|
| 37 | - $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 38 | - return new static($dt->getTimestamp()); |
|
| 39 | - } catch (\Exception $e) { |
|
| 40 | - throw new \RuntimeException( |
|
| 41 | - "Failed to create DateTime: " . |
|
| 42 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 43 | - } |
|
| 44 | - } |
|
| 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 | + * @throws \RuntimeException |
|
| 32 | + * @return static |
|
| 33 | + */ |
|
| 34 | + public static function fromString(string $time, string $tz = "UTC") |
|
| 35 | + { |
|
| 36 | + try { |
|
| 37 | + $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 38 | + return new static($dt->getTimestamp()); |
|
| 39 | + } catch (\Exception $e) { |
|
| 40 | + throw new \RuntimeException( |
|
| 41 | + "Failed to create DateTime: " . |
|
| 42 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Get date as a unix timestamp. |
|
| 48 | - * |
|
| 49 | - * @return int |
|
| 50 | - */ |
|
| 51 | - public function timestamp(): int |
|
| 52 | - { |
|
| 53 | - return (int) $this->value(); |
|
| 54 | - } |
|
| 46 | + /** |
|
| 47 | + * Get date as a unix timestamp. |
|
| 48 | + * |
|
| 49 | + * @return int |
|
| 50 | + */ |
|
| 51 | + public function timestamp(): int |
|
| 52 | + { |
|
| 53 | + return (int) $this->value(); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Get date as a datetime object. |
|
| 58 | - * |
|
| 59 | - * @param string $tz Timezone |
|
| 60 | - * @throws \RuntimeException |
|
| 61 | - * @return \DateTimeImmutable |
|
| 62 | - */ |
|
| 63 | - public function dateTime(string $tz = "UTC"): \DateTimeImmutable |
|
| 64 | - { |
|
| 65 | - $dt = \DateTimeImmutable::createFromFormat("!U", strval($this->value()), |
|
| 66 | - self::_createTimeZone($tz)); |
|
| 67 | - if (false === $dt) { |
|
| 68 | - throw new \RuntimeException( |
|
| 69 | - "Failed to create DateTime: " . |
|
| 70 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 71 | - } |
|
| 72 | - return $dt; |
|
| 73 | - } |
|
| 56 | + /** |
|
| 57 | + * Get date as a datetime object. |
|
| 58 | + * |
|
| 59 | + * @param string $tz Timezone |
|
| 60 | + * @throws \RuntimeException |
|
| 61 | + * @return \DateTimeImmutable |
|
| 62 | + */ |
|
| 63 | + public function dateTime(string $tz = "UTC"): \DateTimeImmutable |
|
| 64 | + { |
|
| 65 | + $dt = \DateTimeImmutable::createFromFormat("!U", strval($this->value()), |
|
| 66 | + self::_createTimeZone($tz)); |
|
| 67 | + if (false === $dt) { |
|
| 68 | + throw new \RuntimeException( |
|
| 69 | + "Failed to create DateTime: " . |
|
| 70 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 71 | + } |
|
| 72 | + return $dt; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Create DateTimeZone object from string. |
|
| 77 | - * |
|
| 78 | - * @param string $tz |
|
| 79 | - * @throws \UnexpectedValueException |
|
| 80 | - * @return \DateTimeZone |
|
| 81 | - */ |
|
| 82 | - private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 83 | - { |
|
| 84 | - try { |
|
| 85 | - return new \DateTimeZone($tz); |
|
| 86 | - } catch (\Exception $e) { |
|
| 87 | - throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 88 | - } |
|
| 89 | - } |
|
| 75 | + /** |
|
| 76 | + * Create DateTimeZone object from string. |
|
| 77 | + * |
|
| 78 | + * @param string $tz |
|
| 79 | + * @throws \UnexpectedValueException |
|
| 80 | + * @return \DateTimeZone |
|
| 81 | + */ |
|
| 82 | + private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 83 | + { |
|
| 84 | + try { |
|
| 85 | + return new \DateTimeZone($tz); |
|
| 86 | + } catch (\Exception $e) { |
|
| 87 | + throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Get last error caused by DateTimeImmutable. |
|
| 93 | - * |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 97 | - { |
|
| 98 | - $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 99 | - return implode(", ", $errors); |
|
| 100 | - } |
|
| 91 | + /** |
|
| 92 | + * Get last error caused by DateTimeImmutable. |
|
| 93 | + * |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 97 | + { |
|
| 98 | + $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 99 | + return implode(", ", $errors); |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -9,10 +9,10 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | interface HeaderParameters |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Get an array of JOSE header parameters representing this object. |
|
| 14 | - * |
|
| 15 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 16 | - */ |
|
| 17 | - public function headerParameters(): array; |
|
| 12 | + /** |
|
| 13 | + * Get an array of JOSE header parameters representing this object. |
|
| 14 | + * |
|
| 15 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 16 | + */ |
|
| 17 | + public function headerParameters(): array; |
|
| 18 | 18 | } |
@@ -13,453 +13,453 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | trait TypedHeader |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Whether parameters are present. |
|
| 18 | - * |
|
| 19 | - * @param string ...$names Parameter names |
|
| 20 | - * @return bool |
|
| 21 | - */ |
|
| 22 | - abstract public function has(string ...$names): bool; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Get a parameter. |
|
| 26 | - * |
|
| 27 | - * @param string $name Parameter name |
|
| 28 | - * @throws \LogicException If the parameter is not present |
|
| 29 | - * @return JWTParameter |
|
| 30 | - */ |
|
| 31 | - abstract public function get(string $name): JWTParameter; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Check whether the algorithm parameter is present. |
|
| 35 | - * |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - public function hasAlgorithm(): bool |
|
| 39 | - { |
|
| 40 | - return $this->has(Parameter\JWTParameter::P_ALG); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Get the algorithm parameter. |
|
| 45 | - * |
|
| 46 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 47 | - * @throws \LogicException If the parameter is not present |
|
| 48 | - * @return \JWX\JWT\Parameter\AlgorithmParameter |
|
| 49 | - */ |
|
| 50 | - public function algorithm(): Parameter\AlgorithmParameter |
|
| 51 | - { |
|
| 52 | - return self::_checkType($this->get(Parameter\JWTParameter::P_ALG), |
|
| 53 | - Parameter\AlgorithmParameter::class); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Check whether the authentication tag parameter is present. |
|
| 58 | - * |
|
| 59 | - * @return bool |
|
| 60 | - */ |
|
| 61 | - public function hasAuthenticationTag(): bool |
|
| 62 | - { |
|
| 63 | - return $this->has(Parameter\JWTParameter::P_TAG); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the authentication tag parameter. |
|
| 68 | - * |
|
| 69 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 70 | - * @throws \LogicException If the parameter is not present |
|
| 71 | - * @return \JWX\JWT\Parameter\AuthenticationTagParameter |
|
| 72 | - */ |
|
| 73 | - public function authenticationTag(): Parameter\AuthenticationTagParameter |
|
| 74 | - { |
|
| 75 | - return self::_checkType($this->get(Parameter\JWTParameter::P_TAG), |
|
| 76 | - Parameter\AuthenticationTagParameter::class); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Check whether the 'base64url-encode payload' parameter is present. |
|
| 81 | - * |
|
| 82 | - * @return bool |
|
| 83 | - */ |
|
| 84 | - public function hasB64Payload(): bool |
|
| 85 | - { |
|
| 86 | - return $this->has(Parameter\JWTParameter::P_B64); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Get the 'base64url-encode payload' parameter. |
|
| 91 | - * |
|
| 92 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 93 | - * @throws \LogicException If the parameter is not present |
|
| 94 | - * @return \JWX\JWT\Parameter\B64PayloadParameter |
|
| 95 | - */ |
|
| 96 | - public function B64Payload(): Parameter\B64PayloadParameter |
|
| 97 | - { |
|
| 98 | - return self::_checkType($this->get(Parameter\JWTParameter::P_B64), |
|
| 99 | - Parameter\B64PayloadParameter::class); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Check whether the compression algorithm parameter is present. |
|
| 104 | - * |
|
| 105 | - * @return bool |
|
| 106 | - */ |
|
| 107 | - public function hasCompressionAlgorithm(): bool |
|
| 108 | - { |
|
| 109 | - return $this->has(Parameter\JWTParameter::P_ZIP); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get the compression algorithm parameter. |
|
| 114 | - * |
|
| 115 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 116 | - * @throws \LogicException If the parameter is not present |
|
| 117 | - * @return \JWX\JWT\Parameter\CompressionAlgorithmParameter |
|
| 118 | - */ |
|
| 119 | - public function compressionAlgorithm(): Parameter\CompressionAlgorithmParameter |
|
| 120 | - { |
|
| 121 | - return self::_checkType($this->get(Parameter\JWTParameter::P_ZIP), |
|
| 122 | - Parameter\CompressionAlgorithmParameter::class); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Check whether the content type parameter is present. |
|
| 127 | - * |
|
| 128 | - * @return bool |
|
| 129 | - */ |
|
| 130 | - public function hasContentType(): bool |
|
| 131 | - { |
|
| 132 | - return $this->has(Parameter\JWTParameter::P_CTY); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Get the content type parameter. |
|
| 137 | - * |
|
| 138 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 139 | - * @throws \LogicException If the parameter is not present |
|
| 140 | - * @return \JWX\JWT\Parameter\ContentTypeParameter |
|
| 141 | - */ |
|
| 142 | - public function contentType(): Parameter\ContentTypeParameter |
|
| 143 | - { |
|
| 144 | - return self::_checkType($this->get(Parameter\JWTParameter::P_CTY), |
|
| 145 | - Parameter\ContentTypeParameter::class); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Check whether the critical parameter is present. |
|
| 150 | - * |
|
| 151 | - * @return bool |
|
| 152 | - */ |
|
| 153 | - public function hasCritical(): bool |
|
| 154 | - { |
|
| 155 | - return $this->has(Parameter\JWTParameter::P_CRIT); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Get the critical parameter. |
|
| 160 | - * |
|
| 161 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 162 | - * @throws \LogicException If the parameter is not present |
|
| 163 | - * @return \JWX\JWT\Parameter\CriticalParameter |
|
| 164 | - */ |
|
| 165 | - public function critical(): Parameter\CriticalParameter |
|
| 166 | - { |
|
| 167 | - return self::_checkType($this->get(Parameter\JWTParameter::P_CRIT), |
|
| 168 | - Parameter\CriticalParameter::class); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Check whether the encryption algorithm parameter is present. |
|
| 173 | - * |
|
| 174 | - * @return bool |
|
| 175 | - */ |
|
| 176 | - public function hasEncryptionAlgorithm(): bool |
|
| 177 | - { |
|
| 178 | - return $this->has(Parameter\JWTParameter::P_ENC); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get the encryption algorithm parameter. |
|
| 183 | - * |
|
| 184 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 185 | - * @throws \LogicException If the parameter is not present |
|
| 186 | - * @return \JWX\JWT\Parameter\EncryptionAlgorithmParameter |
|
| 187 | - */ |
|
| 188 | - public function encryptionAlgorithm(): Parameter\EncryptionAlgorithmParameter |
|
| 189 | - { |
|
| 190 | - return self::_checkType($this->get(Parameter\JWTParameter::P_ENC), |
|
| 191 | - Parameter\EncryptionAlgorithmParameter::class); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Check whether the initialization vector parameter is present. |
|
| 196 | - * |
|
| 197 | - * @return bool |
|
| 198 | - */ |
|
| 199 | - public function hasInitializationVector(): bool |
|
| 200 | - { |
|
| 201 | - return $this->has(Parameter\JWTParameter::P_IV); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Get the initialization vector parameter. |
|
| 206 | - * |
|
| 207 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 208 | - * @throws \LogicException If the parameter is not present |
|
| 209 | - * @return \JWX\JWT\Parameter\InitializationVectorParameter |
|
| 210 | - */ |
|
| 211 | - public function initializationVector(): Parameter\InitializationVectorParameter |
|
| 212 | - { |
|
| 213 | - return self::_checkType($this->get(Parameter\JWTParameter::P_IV), |
|
| 214 | - Parameter\InitializationVectorParameter::class); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Check whether the JSON web key parameter is present. |
|
| 219 | - * |
|
| 220 | - * @return bool |
|
| 221 | - */ |
|
| 222 | - public function hasJSONWebKey(): bool |
|
| 223 | - { |
|
| 224 | - return $this->has(Parameter\JWTParameter::P_JWK); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Get the JSON web key parameter. |
|
| 229 | - * |
|
| 230 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 231 | - * @throws \LogicException If the parameter is not present |
|
| 232 | - * @return \JWX\JWT\Parameter\JSONWebKeyParameter |
|
| 233 | - */ |
|
| 234 | - public function JSONWebKey(): Parameter\JSONWebKeyParameter |
|
| 235 | - { |
|
| 236 | - return self::_checkType($this->get(Parameter\JWTParameter::P_JWK), |
|
| 237 | - Parameter\JSONWebKeyParameter::class); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Check whether the JWK set URL parameter is present. |
|
| 242 | - * |
|
| 243 | - * @return bool |
|
| 244 | - */ |
|
| 245 | - public function hasJWKSetURL(): bool |
|
| 246 | - { |
|
| 247 | - return $this->has(Parameter\JWTParameter::P_JKU); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Get the JWK set URL parameter. |
|
| 252 | - * |
|
| 253 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 254 | - * @throws \LogicException If the parameter is not present |
|
| 255 | - * @return \JWX\JWT\Parameter\JWKSetURLParameter |
|
| 256 | - */ |
|
| 257 | - public function JWKSetURL(): Parameter\JWKSetURLParameter |
|
| 258 | - { |
|
| 259 | - return self::_checkType($this->get(Parameter\JWTParameter::P_JKU), |
|
| 260 | - Parameter\JWKSetURLParameter::class); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Check whether the key ID parameter is present. |
|
| 265 | - * |
|
| 266 | - * @return bool |
|
| 267 | - */ |
|
| 268 | - public function hasKeyID(): bool |
|
| 269 | - { |
|
| 270 | - return $this->has(Parameter\JWTParameter::P_KID); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Get the key ID parameter. |
|
| 275 | - * |
|
| 276 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 277 | - * @throws \LogicException If the parameter is not present |
|
| 278 | - * @return \JWX\JWT\Parameter\KeyIDParameter |
|
| 279 | - */ |
|
| 280 | - public function keyID(): Parameter\KeyIDParameter |
|
| 281 | - { |
|
| 282 | - return self::_checkType($this->get(Parameter\JWTParameter::P_KID), |
|
| 283 | - Parameter\KeyIDParameter::class); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Check whether the PBES2 count parameter is present. |
|
| 288 | - * |
|
| 289 | - * @return bool |
|
| 290 | - */ |
|
| 291 | - public function hasPBES2Count(): bool |
|
| 292 | - { |
|
| 293 | - return $this->has(Parameter\JWTParameter::P_P2C); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Get the PBES2 count parameter. |
|
| 298 | - * |
|
| 299 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 300 | - * @throws \LogicException If the parameter is not present |
|
| 301 | - * @return \JWX\JWT\Parameter\PBES2CountParameter |
|
| 302 | - */ |
|
| 303 | - public function PBES2Count(): Parameter\PBES2CountParameter |
|
| 304 | - { |
|
| 305 | - return self::_checkType($this->get(Parameter\JWTParameter::P_P2C), |
|
| 306 | - Parameter\PBES2CountParameter::class); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Check whether the PBES2 salt input parameter is present. |
|
| 311 | - * |
|
| 312 | - * @return bool |
|
| 313 | - */ |
|
| 314 | - public function hasPBES2SaltInput(): bool |
|
| 315 | - { |
|
| 316 | - return $this->has(Parameter\JWTParameter::P_P2S); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Get the PBES2 salt input parameter. |
|
| 321 | - * |
|
| 322 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 323 | - * @throws \LogicException If the parameter is not present |
|
| 324 | - * @return \JWX\JWT\Parameter\PBES2SaltInputParameter |
|
| 325 | - */ |
|
| 326 | - public function PBES2SaltInput(): Parameter\PBES2SaltInputParameter |
|
| 327 | - { |
|
| 328 | - return self::_checkType($this->get(Parameter\JWTParameter::P_P2S), |
|
| 329 | - Parameter\PBES2SaltInputParameter::class); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Check whether the type parameter is present. |
|
| 334 | - * |
|
| 335 | - * @return bool |
|
| 336 | - */ |
|
| 337 | - public function hasType(): bool |
|
| 338 | - { |
|
| 339 | - return $this->has(Parameter\JWTParameter::P_TYP); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Get the type parameter. |
|
| 344 | - * |
|
| 345 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 346 | - * @throws \LogicException If the parameter is not present |
|
| 347 | - * @return \JWX\JWT\Parameter\TypeParameter |
|
| 348 | - */ |
|
| 349 | - public function type(): Parameter\TypeParameter |
|
| 350 | - { |
|
| 351 | - return self::_checkType($this->get(Parameter\JWTParameter::P_TYP), |
|
| 352 | - Parameter\TypeParameter::class); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Check whether the X.509 certificate chain parameter is present. |
|
| 357 | - * |
|
| 358 | - * @return bool |
|
| 359 | - */ |
|
| 360 | - public function hasX509CertificateChain(): bool |
|
| 361 | - { |
|
| 362 | - return $this->has(Parameter\JWTParameter::P_X5C); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Get the X.509 certificate chain parameter. |
|
| 367 | - * |
|
| 368 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 369 | - * @throws \LogicException If the parameter is not present |
|
| 370 | - * @return \JWX\JWT\Parameter\X509CertificateChainParameter |
|
| 371 | - */ |
|
| 372 | - public function X509CertificateChain(): Parameter\X509CertificateChainParameter |
|
| 373 | - { |
|
| 374 | - return self::_checkType($this->get(Parameter\JWTParameter::P_X5C), |
|
| 375 | - Parameter\X509CertificateChainParameter::class); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Check whether the X.509 certificate SHA-1 thumbprint parameter is |
|
| 380 | - * present. |
|
| 381 | - * |
|
| 382 | - * @return bool |
|
| 383 | - */ |
|
| 384 | - public function hasX509CertificateSHA1Thumbprint(): bool |
|
| 385 | - { |
|
| 386 | - return $this->has(Parameter\JWTParameter::P_X5T); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Get the X.509 certificate SHA-1 thumbprint parameter. |
|
| 391 | - * |
|
| 392 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 393 | - * @throws \LogicException If the parameter is not present |
|
| 394 | - * @return \JWX\JWT\Parameter\X509CertificateSHA1ThumbprintParameter |
|
| 395 | - */ |
|
| 396 | - public function X509CertificateSHA1Thumbprint(): Parameter\X509CertificateSHA1ThumbprintParameter |
|
| 397 | - { |
|
| 398 | - return self::_checkType($this->get(Parameter\JWTParameter::P_X5T), |
|
| 399 | - Parameter\X509CertificateSHA1ThumbprintParameter::class); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Check whether the X.509 certificate SHA-256 thumbprint parameter is |
|
| 404 | - * present. |
|
| 405 | - * |
|
| 406 | - * @return bool |
|
| 407 | - */ |
|
| 408 | - public function hasX509CertificateSHA256Thumbprint(): bool |
|
| 409 | - { |
|
| 410 | - return $this->has(Parameter\JWTParameter::P_X5TS256); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Get the X.509 certificate SHA-256 thumbprint parameter. |
|
| 415 | - * |
|
| 416 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 417 | - * @throws \LogicException If the parameter is not present |
|
| 418 | - * @return \JWX\JWT\Parameter\X509CertificateSHA256ThumbprintParameter |
|
| 419 | - */ |
|
| 420 | - public function X509CertificateSHA256Thumbprint(): Parameter\X509CertificateSHA256ThumbprintParameter |
|
| 421 | - { |
|
| 422 | - return self::_checkType($this->get(Parameter\JWTParameter::P_X5TS256), |
|
| 423 | - Parameter\X509CertificateSHA256ThumbprintParameter::class); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Check whether the X.509 URL parameter is present. |
|
| 428 | - * |
|
| 429 | - * @return bool |
|
| 430 | - */ |
|
| 431 | - public function hasX509URL(): bool |
|
| 432 | - { |
|
| 433 | - return $this->has(Parameter\JWTParameter::P_X5U); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Get the X.509 URL parameter. |
|
| 438 | - * |
|
| 439 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 440 | - * @throws \LogicException If the parameter is not present |
|
| 441 | - * @return \JWX\JWT\Parameter\X509URLParameter |
|
| 442 | - */ |
|
| 443 | - public function X509URL(): Parameter\X509URLParameter |
|
| 444 | - { |
|
| 445 | - return self::_checkType($this->get(Parameter\JWTParameter::P_X5U), |
|
| 446 | - Parameter\X509URLParameter::class); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Check that the parameter is an instance of the given class. |
|
| 451 | - * |
|
| 452 | - * @param \JWX\JWT\Parameter\JWTParameter $param Parameter |
|
| 453 | - * @param string $cls Class name |
|
| 454 | - * @throws \UnexpectedValueException |
|
| 455 | - * @return \JWX\JWT\Parameter\JWTParameter |
|
| 456 | - */ |
|
| 457 | - private static function _checkType(Parameter\JWTParameter $param, string $cls): Parameter\JWTParameter |
|
| 458 | - { |
|
| 459 | - if (!$param instanceof $cls) { |
|
| 460 | - throw new \UnexpectedValueException( |
|
| 461 | - "$cls expected, got " . get_class($param)); |
|
| 462 | - } |
|
| 463 | - return $param; |
|
| 464 | - } |
|
| 16 | + /** |
|
| 17 | + * Whether parameters are present. |
|
| 18 | + * |
|
| 19 | + * @param string ...$names Parameter names |
|
| 20 | + * @return bool |
|
| 21 | + */ |
|
| 22 | + abstract public function has(string ...$names): bool; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Get a parameter. |
|
| 26 | + * |
|
| 27 | + * @param string $name Parameter name |
|
| 28 | + * @throws \LogicException If the parameter is not present |
|
| 29 | + * @return JWTParameter |
|
| 30 | + */ |
|
| 31 | + abstract public function get(string $name): JWTParameter; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Check whether the algorithm parameter is present. |
|
| 35 | + * |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + public function hasAlgorithm(): bool |
|
| 39 | + { |
|
| 40 | + return $this->has(Parameter\JWTParameter::P_ALG); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Get the algorithm parameter. |
|
| 45 | + * |
|
| 46 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 47 | + * @throws \LogicException If the parameter is not present |
|
| 48 | + * @return \JWX\JWT\Parameter\AlgorithmParameter |
|
| 49 | + */ |
|
| 50 | + public function algorithm(): Parameter\AlgorithmParameter |
|
| 51 | + { |
|
| 52 | + return self::_checkType($this->get(Parameter\JWTParameter::P_ALG), |
|
| 53 | + Parameter\AlgorithmParameter::class); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Check whether the authentication tag parameter is present. |
|
| 58 | + * |
|
| 59 | + * @return bool |
|
| 60 | + */ |
|
| 61 | + public function hasAuthenticationTag(): bool |
|
| 62 | + { |
|
| 63 | + return $this->has(Parameter\JWTParameter::P_TAG); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the authentication tag parameter. |
|
| 68 | + * |
|
| 69 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 70 | + * @throws \LogicException If the parameter is not present |
|
| 71 | + * @return \JWX\JWT\Parameter\AuthenticationTagParameter |
|
| 72 | + */ |
|
| 73 | + public function authenticationTag(): Parameter\AuthenticationTagParameter |
|
| 74 | + { |
|
| 75 | + return self::_checkType($this->get(Parameter\JWTParameter::P_TAG), |
|
| 76 | + Parameter\AuthenticationTagParameter::class); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Check whether the 'base64url-encode payload' parameter is present. |
|
| 81 | + * |
|
| 82 | + * @return bool |
|
| 83 | + */ |
|
| 84 | + public function hasB64Payload(): bool |
|
| 85 | + { |
|
| 86 | + return $this->has(Parameter\JWTParameter::P_B64); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Get the 'base64url-encode payload' parameter. |
|
| 91 | + * |
|
| 92 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 93 | + * @throws \LogicException If the parameter is not present |
|
| 94 | + * @return \JWX\JWT\Parameter\B64PayloadParameter |
|
| 95 | + */ |
|
| 96 | + public function B64Payload(): Parameter\B64PayloadParameter |
|
| 97 | + { |
|
| 98 | + return self::_checkType($this->get(Parameter\JWTParameter::P_B64), |
|
| 99 | + Parameter\B64PayloadParameter::class); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Check whether the compression algorithm parameter is present. |
|
| 104 | + * |
|
| 105 | + * @return bool |
|
| 106 | + */ |
|
| 107 | + public function hasCompressionAlgorithm(): bool |
|
| 108 | + { |
|
| 109 | + return $this->has(Parameter\JWTParameter::P_ZIP); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get the compression algorithm parameter. |
|
| 114 | + * |
|
| 115 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 116 | + * @throws \LogicException If the parameter is not present |
|
| 117 | + * @return \JWX\JWT\Parameter\CompressionAlgorithmParameter |
|
| 118 | + */ |
|
| 119 | + public function compressionAlgorithm(): Parameter\CompressionAlgorithmParameter |
|
| 120 | + { |
|
| 121 | + return self::_checkType($this->get(Parameter\JWTParameter::P_ZIP), |
|
| 122 | + Parameter\CompressionAlgorithmParameter::class); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Check whether the content type parameter is present. |
|
| 127 | + * |
|
| 128 | + * @return bool |
|
| 129 | + */ |
|
| 130 | + public function hasContentType(): bool |
|
| 131 | + { |
|
| 132 | + return $this->has(Parameter\JWTParameter::P_CTY); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Get the content type parameter. |
|
| 137 | + * |
|
| 138 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 139 | + * @throws \LogicException If the parameter is not present |
|
| 140 | + * @return \JWX\JWT\Parameter\ContentTypeParameter |
|
| 141 | + */ |
|
| 142 | + public function contentType(): Parameter\ContentTypeParameter |
|
| 143 | + { |
|
| 144 | + return self::_checkType($this->get(Parameter\JWTParameter::P_CTY), |
|
| 145 | + Parameter\ContentTypeParameter::class); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Check whether the critical parameter is present. |
|
| 150 | + * |
|
| 151 | + * @return bool |
|
| 152 | + */ |
|
| 153 | + public function hasCritical(): bool |
|
| 154 | + { |
|
| 155 | + return $this->has(Parameter\JWTParameter::P_CRIT); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Get the critical parameter. |
|
| 160 | + * |
|
| 161 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 162 | + * @throws \LogicException If the parameter is not present |
|
| 163 | + * @return \JWX\JWT\Parameter\CriticalParameter |
|
| 164 | + */ |
|
| 165 | + public function critical(): Parameter\CriticalParameter |
|
| 166 | + { |
|
| 167 | + return self::_checkType($this->get(Parameter\JWTParameter::P_CRIT), |
|
| 168 | + Parameter\CriticalParameter::class); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Check whether the encryption algorithm parameter is present. |
|
| 173 | + * |
|
| 174 | + * @return bool |
|
| 175 | + */ |
|
| 176 | + public function hasEncryptionAlgorithm(): bool |
|
| 177 | + { |
|
| 178 | + return $this->has(Parameter\JWTParameter::P_ENC); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get the encryption algorithm parameter. |
|
| 183 | + * |
|
| 184 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 185 | + * @throws \LogicException If the parameter is not present |
|
| 186 | + * @return \JWX\JWT\Parameter\EncryptionAlgorithmParameter |
|
| 187 | + */ |
|
| 188 | + public function encryptionAlgorithm(): Parameter\EncryptionAlgorithmParameter |
|
| 189 | + { |
|
| 190 | + return self::_checkType($this->get(Parameter\JWTParameter::P_ENC), |
|
| 191 | + Parameter\EncryptionAlgorithmParameter::class); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Check whether the initialization vector parameter is present. |
|
| 196 | + * |
|
| 197 | + * @return bool |
|
| 198 | + */ |
|
| 199 | + public function hasInitializationVector(): bool |
|
| 200 | + { |
|
| 201 | + return $this->has(Parameter\JWTParameter::P_IV); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Get the initialization vector parameter. |
|
| 206 | + * |
|
| 207 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 208 | + * @throws \LogicException If the parameter is not present |
|
| 209 | + * @return \JWX\JWT\Parameter\InitializationVectorParameter |
|
| 210 | + */ |
|
| 211 | + public function initializationVector(): Parameter\InitializationVectorParameter |
|
| 212 | + { |
|
| 213 | + return self::_checkType($this->get(Parameter\JWTParameter::P_IV), |
|
| 214 | + Parameter\InitializationVectorParameter::class); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Check whether the JSON web key parameter is present. |
|
| 219 | + * |
|
| 220 | + * @return bool |
|
| 221 | + */ |
|
| 222 | + public function hasJSONWebKey(): bool |
|
| 223 | + { |
|
| 224 | + return $this->has(Parameter\JWTParameter::P_JWK); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Get the JSON web key parameter. |
|
| 229 | + * |
|
| 230 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 231 | + * @throws \LogicException If the parameter is not present |
|
| 232 | + * @return \JWX\JWT\Parameter\JSONWebKeyParameter |
|
| 233 | + */ |
|
| 234 | + public function JSONWebKey(): Parameter\JSONWebKeyParameter |
|
| 235 | + { |
|
| 236 | + return self::_checkType($this->get(Parameter\JWTParameter::P_JWK), |
|
| 237 | + Parameter\JSONWebKeyParameter::class); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Check whether the JWK set URL parameter is present. |
|
| 242 | + * |
|
| 243 | + * @return bool |
|
| 244 | + */ |
|
| 245 | + public function hasJWKSetURL(): bool |
|
| 246 | + { |
|
| 247 | + return $this->has(Parameter\JWTParameter::P_JKU); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Get the JWK set URL parameter. |
|
| 252 | + * |
|
| 253 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 254 | + * @throws \LogicException If the parameter is not present |
|
| 255 | + * @return \JWX\JWT\Parameter\JWKSetURLParameter |
|
| 256 | + */ |
|
| 257 | + public function JWKSetURL(): Parameter\JWKSetURLParameter |
|
| 258 | + { |
|
| 259 | + return self::_checkType($this->get(Parameter\JWTParameter::P_JKU), |
|
| 260 | + Parameter\JWKSetURLParameter::class); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Check whether the key ID parameter is present. |
|
| 265 | + * |
|
| 266 | + * @return bool |
|
| 267 | + */ |
|
| 268 | + public function hasKeyID(): bool |
|
| 269 | + { |
|
| 270 | + return $this->has(Parameter\JWTParameter::P_KID); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Get the key ID parameter. |
|
| 275 | + * |
|
| 276 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 277 | + * @throws \LogicException If the parameter is not present |
|
| 278 | + * @return \JWX\JWT\Parameter\KeyIDParameter |
|
| 279 | + */ |
|
| 280 | + public function keyID(): Parameter\KeyIDParameter |
|
| 281 | + { |
|
| 282 | + return self::_checkType($this->get(Parameter\JWTParameter::P_KID), |
|
| 283 | + Parameter\KeyIDParameter::class); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Check whether the PBES2 count parameter is present. |
|
| 288 | + * |
|
| 289 | + * @return bool |
|
| 290 | + */ |
|
| 291 | + public function hasPBES2Count(): bool |
|
| 292 | + { |
|
| 293 | + return $this->has(Parameter\JWTParameter::P_P2C); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Get the PBES2 count parameter. |
|
| 298 | + * |
|
| 299 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 300 | + * @throws \LogicException If the parameter is not present |
|
| 301 | + * @return \JWX\JWT\Parameter\PBES2CountParameter |
|
| 302 | + */ |
|
| 303 | + public function PBES2Count(): Parameter\PBES2CountParameter |
|
| 304 | + { |
|
| 305 | + return self::_checkType($this->get(Parameter\JWTParameter::P_P2C), |
|
| 306 | + Parameter\PBES2CountParameter::class); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Check whether the PBES2 salt input parameter is present. |
|
| 311 | + * |
|
| 312 | + * @return bool |
|
| 313 | + */ |
|
| 314 | + public function hasPBES2SaltInput(): bool |
|
| 315 | + { |
|
| 316 | + return $this->has(Parameter\JWTParameter::P_P2S); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Get the PBES2 salt input parameter. |
|
| 321 | + * |
|
| 322 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 323 | + * @throws \LogicException If the parameter is not present |
|
| 324 | + * @return \JWX\JWT\Parameter\PBES2SaltInputParameter |
|
| 325 | + */ |
|
| 326 | + public function PBES2SaltInput(): Parameter\PBES2SaltInputParameter |
|
| 327 | + { |
|
| 328 | + return self::_checkType($this->get(Parameter\JWTParameter::P_P2S), |
|
| 329 | + Parameter\PBES2SaltInputParameter::class); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Check whether the type parameter is present. |
|
| 334 | + * |
|
| 335 | + * @return bool |
|
| 336 | + */ |
|
| 337 | + public function hasType(): bool |
|
| 338 | + { |
|
| 339 | + return $this->has(Parameter\JWTParameter::P_TYP); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Get the type parameter. |
|
| 344 | + * |
|
| 345 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 346 | + * @throws \LogicException If the parameter is not present |
|
| 347 | + * @return \JWX\JWT\Parameter\TypeParameter |
|
| 348 | + */ |
|
| 349 | + public function type(): Parameter\TypeParameter |
|
| 350 | + { |
|
| 351 | + return self::_checkType($this->get(Parameter\JWTParameter::P_TYP), |
|
| 352 | + Parameter\TypeParameter::class); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Check whether the X.509 certificate chain parameter is present. |
|
| 357 | + * |
|
| 358 | + * @return bool |
|
| 359 | + */ |
|
| 360 | + public function hasX509CertificateChain(): bool |
|
| 361 | + { |
|
| 362 | + return $this->has(Parameter\JWTParameter::P_X5C); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Get the X.509 certificate chain parameter. |
|
| 367 | + * |
|
| 368 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 369 | + * @throws \LogicException If the parameter is not present |
|
| 370 | + * @return \JWX\JWT\Parameter\X509CertificateChainParameter |
|
| 371 | + */ |
|
| 372 | + public function X509CertificateChain(): Parameter\X509CertificateChainParameter |
|
| 373 | + { |
|
| 374 | + return self::_checkType($this->get(Parameter\JWTParameter::P_X5C), |
|
| 375 | + Parameter\X509CertificateChainParameter::class); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Check whether the X.509 certificate SHA-1 thumbprint parameter is |
|
| 380 | + * present. |
|
| 381 | + * |
|
| 382 | + * @return bool |
|
| 383 | + */ |
|
| 384 | + public function hasX509CertificateSHA1Thumbprint(): bool |
|
| 385 | + { |
|
| 386 | + return $this->has(Parameter\JWTParameter::P_X5T); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Get the X.509 certificate SHA-1 thumbprint parameter. |
|
| 391 | + * |
|
| 392 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 393 | + * @throws \LogicException If the parameter is not present |
|
| 394 | + * @return \JWX\JWT\Parameter\X509CertificateSHA1ThumbprintParameter |
|
| 395 | + */ |
|
| 396 | + public function X509CertificateSHA1Thumbprint(): Parameter\X509CertificateSHA1ThumbprintParameter |
|
| 397 | + { |
|
| 398 | + return self::_checkType($this->get(Parameter\JWTParameter::P_X5T), |
|
| 399 | + Parameter\X509CertificateSHA1ThumbprintParameter::class); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Check whether the X.509 certificate SHA-256 thumbprint parameter is |
|
| 404 | + * present. |
|
| 405 | + * |
|
| 406 | + * @return bool |
|
| 407 | + */ |
|
| 408 | + public function hasX509CertificateSHA256Thumbprint(): bool |
|
| 409 | + { |
|
| 410 | + return $this->has(Parameter\JWTParameter::P_X5TS256); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Get the X.509 certificate SHA-256 thumbprint parameter. |
|
| 415 | + * |
|
| 416 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 417 | + * @throws \LogicException If the parameter is not present |
|
| 418 | + * @return \JWX\JWT\Parameter\X509CertificateSHA256ThumbprintParameter |
|
| 419 | + */ |
|
| 420 | + public function X509CertificateSHA256Thumbprint(): Parameter\X509CertificateSHA256ThumbprintParameter |
|
| 421 | + { |
|
| 422 | + return self::_checkType($this->get(Parameter\JWTParameter::P_X5TS256), |
|
| 423 | + Parameter\X509CertificateSHA256ThumbprintParameter::class); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Check whether the X.509 URL parameter is present. |
|
| 428 | + * |
|
| 429 | + * @return bool |
|
| 430 | + */ |
|
| 431 | + public function hasX509URL(): bool |
|
| 432 | + { |
|
| 433 | + return $this->has(Parameter\JWTParameter::P_X5U); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Get the X.509 URL parameter. |
|
| 438 | + * |
|
| 439 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 440 | + * @throws \LogicException If the parameter is not present |
|
| 441 | + * @return \JWX\JWT\Parameter\X509URLParameter |
|
| 442 | + */ |
|
| 443 | + public function X509URL(): Parameter\X509URLParameter |
|
| 444 | + { |
|
| 445 | + return self::_checkType($this->get(Parameter\JWTParameter::P_X5U), |
|
| 446 | + Parameter\X509URLParameter::class); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Check that the parameter is an instance of the given class. |
|
| 451 | + * |
|
| 452 | + * @param \JWX\JWT\Parameter\JWTParameter $param Parameter |
|
| 453 | + * @param string $cls Class name |
|
| 454 | + * @throws \UnexpectedValueException |
|
| 455 | + * @return \JWX\JWT\Parameter\JWTParameter |
|
| 456 | + */ |
|
| 457 | + private static function _checkType(Parameter\JWTParameter $param, string $cls): Parameter\JWTParameter |
|
| 458 | + { |
|
| 459 | + if (!$param instanceof $cls) { |
|
| 460 | + throw new \UnexpectedValueException( |
|
| 461 | + "$cls expected, got " . get_class($param)); |
|
| 462 | + } |
|
| 463 | + return $param; |
|
| 464 | + } |
|
| 465 | 465 | } |
@@ -15,53 +15,53 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class JOSE extends Header |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @param Header ...$headers One or more headers to merge |
|
| 22 | - */ |
|
| 23 | - public function __construct(Header ...$headers) |
|
| 24 | - { |
|
| 25 | - $params = array(); |
|
| 26 | - foreach ($headers as $header) { |
|
| 27 | - foreach ($header->parameters() as $param) { |
|
| 28 | - if (isset($params[$param->name()])) { |
|
| 29 | - throw new \UnexpectedValueException("Duplicate parameter."); |
|
| 30 | - } |
|
| 31 | - $params[$param->name()] = $param; |
|
| 32 | - } |
|
| 33 | - } |
|
| 34 | - parent::__construct(...array_values($params)); |
|
| 35 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @param Header ...$headers One or more headers to merge |
|
| 22 | + */ |
|
| 23 | + public function __construct(Header ...$headers) |
|
| 24 | + { |
|
| 25 | + $params = array(); |
|
| 26 | + foreach ($headers as $header) { |
|
| 27 | + foreach ($header->parameters() as $param) { |
|
| 28 | + if (isset($params[$param->name()])) { |
|
| 29 | + throw new \UnexpectedValueException("Duplicate parameter."); |
|
| 30 | + } |
|
| 31 | + $params[$param->name()] = $param; |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | + parent::__construct(...array_values($params)); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Get self merged with another Header. |
|
| 39 | - * |
|
| 40 | - * @param Header $header |
|
| 41 | - * @return self |
|
| 42 | - */ |
|
| 43 | - public function withHeader(Header $header): self |
|
| 44 | - { |
|
| 45 | - return new self($this, $header); |
|
| 46 | - } |
|
| 37 | + /** |
|
| 38 | + * Get self merged with another Header. |
|
| 39 | + * |
|
| 40 | + * @param Header $header |
|
| 41 | + * @return self |
|
| 42 | + */ |
|
| 43 | + public function withHeader(Header $header): self |
|
| 44 | + { |
|
| 45 | + return new self($this, $header); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Whether JOSE is for a JWS. |
|
| 50 | - * |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - public function isJWS(): bool |
|
| 54 | - { |
|
| 55 | - return $this->hasAlgorithm() && !$this->hasEncryptionAlgorithm(); |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Whether JOSE is for a JWS. |
|
| 50 | + * |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + public function isJWS(): bool |
|
| 54 | + { |
|
| 55 | + return $this->hasAlgorithm() && !$this->hasEncryptionAlgorithm(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Whether JOSE is for a JWE. |
|
| 60 | - * |
|
| 61 | - * @return bool |
|
| 62 | - */ |
|
| 63 | - public function isJWE(): bool |
|
| 64 | - { |
|
| 65 | - return $this->hasEncryptionAlgorithm(); |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * Whether JOSE is for a JWE. |
|
| 60 | + * |
|
| 61 | + * @return bool |
|
| 62 | + */ |
|
| 63 | + public function isJWE(): bool |
|
| 64 | + { |
|
| 65 | + return $this->hasEncryptionAlgorithm(); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -11,153 +11,153 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Header implements \Countable, \IteratorAggregate |
| 13 | 13 | { |
| 14 | - use TypedHeader; |
|
| 14 | + use TypedHeader; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Parameters. |
|
| 18 | - * |
|
| 19 | - * @var JWTParameter[] $_parameters |
|
| 20 | - */ |
|
| 21 | - protected $_parameters; |
|
| 16 | + /** |
|
| 17 | + * Parameters. |
|
| 18 | + * |
|
| 19 | + * @var JWTParameter[] $_parameters |
|
| 20 | + */ |
|
| 21 | + protected $_parameters; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Constructor. |
|
| 25 | - * |
|
| 26 | - * @param JWTParameter ...$params Parameters |
|
| 27 | - */ |
|
| 28 | - public function __construct(JWTParameter ...$params) |
|
| 29 | - { |
|
| 30 | - $this->_parameters = array(); |
|
| 31 | - foreach ($params as $param) { |
|
| 32 | - $this->_parameters[$param->name()] = $param; |
|
| 33 | - } |
|
| 34 | - } |
|
| 23 | + /** |
|
| 24 | + * Constructor. |
|
| 25 | + * |
|
| 26 | + * @param JWTParameter ...$params Parameters |
|
| 27 | + */ |
|
| 28 | + public function __construct(JWTParameter ...$params) |
|
| 29 | + { |
|
| 30 | + $this->_parameters = array(); |
|
| 31 | + foreach ($params as $param) { |
|
| 32 | + $this->_parameters[$param->name()] = $param; |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Initialize from an array representing a JSON object. |
|
| 38 | - * |
|
| 39 | - * @param array $members |
|
| 40 | - * @return self |
|
| 41 | - */ |
|
| 42 | - public static function fromArray(array $members): self |
|
| 43 | - { |
|
| 44 | - $params = array(); |
|
| 45 | - foreach ($members as $name => $value) { |
|
| 46 | - $params[] = JWTParameter::fromNameAndValue($name, $value); |
|
| 47 | - } |
|
| 48 | - return new self(...$params); |
|
| 49 | - } |
|
| 36 | + /** |
|
| 37 | + * Initialize from an array representing a JSON object. |
|
| 38 | + * |
|
| 39 | + * @param array $members |
|
| 40 | + * @return self |
|
| 41 | + */ |
|
| 42 | + public static function fromArray(array $members): self |
|
| 43 | + { |
|
| 44 | + $params = array(); |
|
| 45 | + foreach ($members as $name => $value) { |
|
| 46 | + $params[] = JWTParameter::fromNameAndValue($name, $value); |
|
| 47 | + } |
|
| 48 | + return new self(...$params); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Initialize from a JSON. |
|
| 53 | - * |
|
| 54 | - * @param string $json |
|
| 55 | - * @throws \UnexpectedValueException |
|
| 56 | - * @return self |
|
| 57 | - */ |
|
| 58 | - public static function fromJSON(string $json): self |
|
| 59 | - { |
|
| 60 | - $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
| 61 | - if (!is_array($members)) { |
|
| 62 | - throw new \UnexpectedValueException("Invalid JSON."); |
|
| 63 | - } |
|
| 64 | - return self::fromArray($members); |
|
| 65 | - } |
|
| 51 | + /** |
|
| 52 | + * Initialize from a JSON. |
|
| 53 | + * |
|
| 54 | + * @param string $json |
|
| 55 | + * @throws \UnexpectedValueException |
|
| 56 | + * @return self |
|
| 57 | + */ |
|
| 58 | + public static function fromJSON(string $json): self |
|
| 59 | + { |
|
| 60 | + $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
| 61 | + if (!is_array($members)) { |
|
| 62 | + throw new \UnexpectedValueException("Invalid JSON."); |
|
| 63 | + } |
|
| 64 | + return self::fromArray($members); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Get self with parameters added. |
|
| 69 | - * |
|
| 70 | - * @param JWTParameter ...$param |
|
| 71 | - * @return self |
|
| 72 | - */ |
|
| 73 | - public function withParameters(JWTParameter ...$params): self |
|
| 74 | - { |
|
| 75 | - $obj = clone $this; |
|
| 76 | - foreach ($params as $param) { |
|
| 77 | - $obj->_parameters[$param->name()] = $param; |
|
| 78 | - } |
|
| 79 | - return $obj; |
|
| 80 | - } |
|
| 67 | + /** |
|
| 68 | + * Get self with parameters added. |
|
| 69 | + * |
|
| 70 | + * @param JWTParameter ...$param |
|
| 71 | + * @return self |
|
| 72 | + */ |
|
| 73 | + public function withParameters(JWTParameter ...$params): self |
|
| 74 | + { |
|
| 75 | + $obj = clone $this; |
|
| 76 | + foreach ($params as $param) { |
|
| 77 | + $obj->_parameters[$param->name()] = $param; |
|
| 78 | + } |
|
| 79 | + return $obj; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Get all parameters. |
|
| 84 | - * |
|
| 85 | - * @return JWTParameter[] |
|
| 86 | - */ |
|
| 87 | - public function parameters(): array |
|
| 88 | - { |
|
| 89 | - return $this->_parameters; |
|
| 90 | - } |
|
| 82 | + /** |
|
| 83 | + * Get all parameters. |
|
| 84 | + * |
|
| 85 | + * @return JWTParameter[] |
|
| 86 | + */ |
|
| 87 | + public function parameters(): array |
|
| 88 | + { |
|
| 89 | + return $this->_parameters; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Whether parameters are present. |
|
| 94 | - * |
|
| 95 | - * Returns false if any of the given parameters is not set. |
|
| 96 | - * |
|
| 97 | - * @param string ...$names Parameter names |
|
| 98 | - * @return bool |
|
| 99 | - */ |
|
| 100 | - public function has(string ...$names): bool |
|
| 101 | - { |
|
| 102 | - foreach ($names as $name) { |
|
| 103 | - if (!isset($this->_parameters[$name])) { |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - return true; |
|
| 108 | - } |
|
| 92 | + /** |
|
| 93 | + * Whether parameters are present. |
|
| 94 | + * |
|
| 95 | + * Returns false if any of the given parameters is not set. |
|
| 96 | + * |
|
| 97 | + * @param string ...$names Parameter names |
|
| 98 | + * @return bool |
|
| 99 | + */ |
|
| 100 | + public function has(string ...$names): bool |
|
| 101 | + { |
|
| 102 | + foreach ($names as $name) { |
|
| 103 | + if (!isset($this->_parameters[$name])) { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + return true; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Get a parameter. |
|
| 112 | - * |
|
| 113 | - * @param string $name Parameter name |
|
| 114 | - * @throws \LogicException |
|
| 115 | - * @return JWTParameter |
|
| 116 | - */ |
|
| 117 | - public function get(string $name): JWTParameter |
|
| 118 | - { |
|
| 119 | - if (!$this->has($name)) { |
|
| 120 | - throw new \LogicException("Parameter $name doesn't exists."); |
|
| 121 | - } |
|
| 122 | - return $this->_parameters[$name]; |
|
| 123 | - } |
|
| 110 | + /** |
|
| 111 | + * Get a parameter. |
|
| 112 | + * |
|
| 113 | + * @param string $name Parameter name |
|
| 114 | + * @throws \LogicException |
|
| 115 | + * @return JWTParameter |
|
| 116 | + */ |
|
| 117 | + public function get(string $name): JWTParameter |
|
| 118 | + { |
|
| 119 | + if (!$this->has($name)) { |
|
| 120 | + throw new \LogicException("Parameter $name doesn't exists."); |
|
| 121 | + } |
|
| 122 | + return $this->_parameters[$name]; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Convert to a JSON. |
|
| 127 | - * |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public function toJSON(): string |
|
| 131 | - { |
|
| 132 | - if (empty($this->_parameters)) { |
|
| 133 | - return ""; |
|
| 134 | - } |
|
| 135 | - $data = array(); |
|
| 136 | - foreach ($this->_parameters as $param) { |
|
| 137 | - $data[$param->name()] = $param->value(); |
|
| 138 | - } |
|
| 139 | - return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
| 140 | - } |
|
| 125 | + /** |
|
| 126 | + * Convert to a JSON. |
|
| 127 | + * |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public function toJSON(): string |
|
| 131 | + { |
|
| 132 | + if (empty($this->_parameters)) { |
|
| 133 | + return ""; |
|
| 134 | + } |
|
| 135 | + $data = array(); |
|
| 136 | + foreach ($this->_parameters as $param) { |
|
| 137 | + $data[$param->name()] = $param->value(); |
|
| 138 | + } |
|
| 139 | + return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Get the number of parameters. |
|
| 144 | - * |
|
| 145 | - * @see \Countable::count() |
|
| 146 | - * @return int |
|
| 147 | - */ |
|
| 148 | - public function count(): int |
|
| 149 | - { |
|
| 150 | - return count($this->_parameters); |
|
| 151 | - } |
|
| 142 | + /** |
|
| 143 | + * Get the number of parameters. |
|
| 144 | + * |
|
| 145 | + * @see \Countable::count() |
|
| 146 | + * @return int |
|
| 147 | + */ |
|
| 148 | + public function count(): int |
|
| 149 | + { |
|
| 150 | + return count($this->_parameters); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Get iterator for the parameters. |
|
| 155 | - * |
|
| 156 | - * @see \IteratorAggregate::getIterator() |
|
| 157 | - * @return \ArrayIterator |
|
| 158 | - */ |
|
| 159 | - public function getIterator(): \ArrayIterator |
|
| 160 | - { |
|
| 161 | - return new \ArrayIterator($this->_parameters); |
|
| 162 | - } |
|
| 153 | + /** |
|
| 154 | + * Get iterator for the parameters. |
|
| 155 | + * |
|
| 156 | + * @see \IteratorAggregate::getIterator() |
|
| 157 | + * @return \ArrayIterator |
|
| 158 | + */ |
|
| 159 | + public function getIterator(): \ArrayIterator |
|
| 160 | + { |
|
| 161 | + return new \ArrayIterator($this->_parameters); |
|
| 162 | + } |
|
| 163 | 163 | } |
@@ -26,308 +26,308 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class ValidationContext |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * Reference time. |
|
| 31 | - * |
|
| 32 | - * @var int $_refTime |
|
| 33 | - */ |
|
| 34 | - protected $_refTime; |
|
| 29 | + /** |
|
| 30 | + * Reference time. |
|
| 31 | + * |
|
| 32 | + * @var int $_refTime |
|
| 33 | + */ |
|
| 34 | + protected $_refTime; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Leeway in seconds for the reference time constraints. |
|
| 38 | - * |
|
| 39 | - * @var int $_leeway |
|
| 40 | - */ |
|
| 41 | - protected $_leeway; |
|
| 36 | + /** |
|
| 37 | + * Leeway in seconds for the reference time constraints. |
|
| 38 | + * |
|
| 39 | + * @var int $_leeway |
|
| 40 | + */ |
|
| 41 | + protected $_leeway; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Validation constraints. |
|
| 45 | - * |
|
| 46 | - * @var array $_constraints |
|
| 47 | - */ |
|
| 48 | - protected $_constraints; |
|
| 43 | + /** |
|
| 44 | + * Validation constraints. |
|
| 45 | + * |
|
| 46 | + * @var array $_constraints |
|
| 47 | + */ |
|
| 48 | + protected $_constraints; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Explicitly defined validators for named claims. |
|
| 52 | - * |
|
| 53 | - * @var Validator[] $_validators |
|
| 54 | - */ |
|
| 55 | - protected $_validators; |
|
| 50 | + /** |
|
| 51 | + * Explicitly defined validators for named claims. |
|
| 52 | + * |
|
| 53 | + * @var Validator[] $_validators |
|
| 54 | + */ |
|
| 55 | + protected $_validators; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Set of JSON Web Keys usable for the validation. |
|
| 59 | - * |
|
| 60 | - * @var JWKSet $_keys |
|
| 61 | - */ |
|
| 62 | - protected $_keys; |
|
| 57 | + /** |
|
| 58 | + * Set of JSON Web Keys usable for the validation. |
|
| 59 | + * |
|
| 60 | + * @var JWKSet $_keys |
|
| 61 | + */ |
|
| 62 | + protected $_keys; |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Whether to allow unsecured JWT's, that is, claims without integrity |
|
| 66 | - * protection nor encryption. |
|
| 67 | - * |
|
| 68 | - * @var bool $_allowUnsecured |
|
| 69 | - */ |
|
| 70 | - protected $_allowUnsecured; |
|
| 64 | + /** |
|
| 65 | + * Whether to allow unsecured JWT's, that is, claims without integrity |
|
| 66 | + * protection nor encryption. |
|
| 67 | + * |
|
| 68 | + * @var bool $_allowUnsecured |
|
| 69 | + */ |
|
| 70 | + protected $_allowUnsecured; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Constructor. |
|
| 74 | - * |
|
| 75 | - * @param array $constraints Optional array of constraints for the |
|
| 76 | - * registered claims |
|
| 77 | - * @param JWKSet $keys Optional set of JSON Web Keys used for signature |
|
| 78 | - * validation and/or decryption |
|
| 79 | - */ |
|
| 80 | - public function __construct(array $constraints = null, JWKSet $keys = null) |
|
| 81 | - { |
|
| 82 | - $this->_refTime = time(); |
|
| 83 | - $this->_leeway = 60; |
|
| 84 | - $this->_constraints = $constraints ? $constraints : array(); |
|
| 85 | - $this->_validators = array(); |
|
| 86 | - $this->_keys = $keys ? $keys : new JWKSet(); |
|
| 87 | - $this->_allowUnsecured = false; |
|
| 88 | - } |
|
| 72 | + /** |
|
| 73 | + * Constructor. |
|
| 74 | + * |
|
| 75 | + * @param array $constraints Optional array of constraints for the |
|
| 76 | + * registered claims |
|
| 77 | + * @param JWKSet $keys Optional set of JSON Web Keys used for signature |
|
| 78 | + * validation and/or decryption |
|
| 79 | + */ |
|
| 80 | + public function __construct(array $constraints = null, JWKSet $keys = null) |
|
| 81 | + { |
|
| 82 | + $this->_refTime = time(); |
|
| 83 | + $this->_leeway = 60; |
|
| 84 | + $this->_constraints = $constraints ? $constraints : array(); |
|
| 85 | + $this->_validators = array(); |
|
| 86 | + $this->_keys = $keys ? $keys : new JWKSet(); |
|
| 87 | + $this->_allowUnsecured = false; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Initialize with a single JSON Web Key. |
|
| 92 | - * |
|
| 93 | - * @param JWK $key JSON Web Key |
|
| 94 | - * @param array $constraints Optional constraints |
|
| 95 | - * @return self |
|
| 96 | - */ |
|
| 97 | - public static function fromJWK(JWK $key, array $constraints = null): self |
|
| 98 | - { |
|
| 99 | - return new self($constraints, new JWKSet($key)); |
|
| 100 | - } |
|
| 90 | + /** |
|
| 91 | + * Initialize with a single JSON Web Key. |
|
| 92 | + * |
|
| 93 | + * @param JWK $key JSON Web Key |
|
| 94 | + * @param array $constraints Optional constraints |
|
| 95 | + * @return self |
|
| 96 | + */ |
|
| 97 | + public static function fromJWK(JWK $key, array $constraints = null): self |
|
| 98 | + { |
|
| 99 | + return new self($constraints, new JWKSet($key)); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Get self with the reference time. |
|
| 104 | - * |
|
| 105 | - * @param int|null $ts Unix timestamp |
|
| 106 | - * @return self |
|
| 107 | - */ |
|
| 108 | - public function withReferenceTime($ts): self |
|
| 109 | - { |
|
| 110 | - $obj = clone $this; |
|
| 111 | - $obj->_refTime = $ts; |
|
| 112 | - return $obj; |
|
| 113 | - } |
|
| 102 | + /** |
|
| 103 | + * Get self with the reference time. |
|
| 104 | + * |
|
| 105 | + * @param int|null $ts Unix timestamp |
|
| 106 | + * @return self |
|
| 107 | + */ |
|
| 108 | + public function withReferenceTime($ts): self |
|
| 109 | + { |
|
| 110 | + $obj = clone $this; |
|
| 111 | + $obj->_refTime = $ts; |
|
| 112 | + return $obj; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Check whether the reference time is set. |
|
| 117 | - * |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public function hasReferenceTime(): bool |
|
| 121 | - { |
|
| 122 | - return isset($this->_refTime); |
|
| 123 | - } |
|
| 115 | + /** |
|
| 116 | + * Check whether the reference time is set. |
|
| 117 | + * |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public function hasReferenceTime(): bool |
|
| 121 | + { |
|
| 122 | + return isset($this->_refTime); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Get the reference time. |
|
| 127 | - * |
|
| 128 | - * @throws \LogicException |
|
| 129 | - * @return int |
|
| 130 | - */ |
|
| 131 | - public function referenceTime(): int |
|
| 132 | - { |
|
| 133 | - if (!$this->hasReferenceTime()) { |
|
| 134 | - throw new \LogicException("Reference time not set."); |
|
| 135 | - } |
|
| 136 | - return $this->_refTime; |
|
| 137 | - } |
|
| 125 | + /** |
|
| 126 | + * Get the reference time. |
|
| 127 | + * |
|
| 128 | + * @throws \LogicException |
|
| 129 | + * @return int |
|
| 130 | + */ |
|
| 131 | + public function referenceTime(): int |
|
| 132 | + { |
|
| 133 | + if (!$this->hasReferenceTime()) { |
|
| 134 | + throw new \LogicException("Reference time not set."); |
|
| 135 | + } |
|
| 136 | + return $this->_refTime; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Get self with the reference time leeway. |
|
| 141 | - * |
|
| 142 | - * @param int $seconds |
|
| 143 | - * @return self |
|
| 144 | - */ |
|
| 145 | - public function withLeeway(int $seconds): self |
|
| 146 | - { |
|
| 147 | - $obj = clone $this; |
|
| 148 | - $obj->_leeway = $seconds; |
|
| 149 | - return $obj; |
|
| 150 | - } |
|
| 139 | + /** |
|
| 140 | + * Get self with the reference time leeway. |
|
| 141 | + * |
|
| 142 | + * @param int $seconds |
|
| 143 | + * @return self |
|
| 144 | + */ |
|
| 145 | + public function withLeeway(int $seconds): self |
|
| 146 | + { |
|
| 147 | + $obj = clone $this; |
|
| 148 | + $obj->_leeway = $seconds; |
|
| 149 | + return $obj; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Get the reference time leeway. |
|
| 154 | - * |
|
| 155 | - * @return int |
|
| 156 | - */ |
|
| 157 | - public function leeway(): int |
|
| 158 | - { |
|
| 159 | - return $this->_leeway; |
|
| 160 | - } |
|
| 152 | + /** |
|
| 153 | + * Get the reference time leeway. |
|
| 154 | + * |
|
| 155 | + * @return int |
|
| 156 | + */ |
|
| 157 | + public function leeway(): int |
|
| 158 | + { |
|
| 159 | + return $this->_leeway; |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * Get self with a validation constraint. |
|
| 164 | - * |
|
| 165 | - * If the claim does not provide its own validator, an explicit validator |
|
| 166 | - * must be given. |
|
| 167 | - * |
|
| 168 | - * @param string $name Claim name |
|
| 169 | - * @param mixed $constraint Value to check claim against |
|
| 170 | - * @param Validator|null $validator Optional explicit validator |
|
| 171 | - * @return self |
|
| 172 | - */ |
|
| 173 | - public function withConstraint(string $name, $constraint, |
|
| 174 | - Validator $validator = null): self |
|
| 175 | - { |
|
| 176 | - $obj = clone $this; |
|
| 177 | - $obj->_constraints[$name] = $constraint; |
|
| 178 | - if ($validator) { |
|
| 179 | - $obj->_validators[$name] = $validator; |
|
| 180 | - } |
|
| 181 | - return $obj; |
|
| 182 | - } |
|
| 162 | + /** |
|
| 163 | + * Get self with a validation constraint. |
|
| 164 | + * |
|
| 165 | + * If the claim does not provide its own validator, an explicit validator |
|
| 166 | + * must be given. |
|
| 167 | + * |
|
| 168 | + * @param string $name Claim name |
|
| 169 | + * @param mixed $constraint Value to check claim against |
|
| 170 | + * @param Validator|null $validator Optional explicit validator |
|
| 171 | + * @return self |
|
| 172 | + */ |
|
| 173 | + public function withConstraint(string $name, $constraint, |
|
| 174 | + Validator $validator = null): self |
|
| 175 | + { |
|
| 176 | + $obj = clone $this; |
|
| 177 | + $obj->_constraints[$name] = $constraint; |
|
| 178 | + if ($validator) { |
|
| 179 | + $obj->_validators[$name] = $validator; |
|
| 180 | + } |
|
| 181 | + return $obj; |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Get self with the issuer constraint. |
|
| 186 | - * |
|
| 187 | - * @param string $issuer Issuer name |
|
| 188 | - * @return self |
|
| 189 | - */ |
|
| 190 | - public function withIssuer(string $issuer): self |
|
| 191 | - { |
|
| 192 | - return $this->withConstraint(RegisteredClaim::NAME_ISSUER, $issuer); |
|
| 193 | - } |
|
| 184 | + /** |
|
| 185 | + * Get self with the issuer constraint. |
|
| 186 | + * |
|
| 187 | + * @param string $issuer Issuer name |
|
| 188 | + * @return self |
|
| 189 | + */ |
|
| 190 | + public function withIssuer(string $issuer): self |
|
| 191 | + { |
|
| 192 | + return $this->withConstraint(RegisteredClaim::NAME_ISSUER, $issuer); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Get self with the subject constraint. |
|
| 197 | - * |
|
| 198 | - * @param string $subject Subject name |
|
| 199 | - * @return self |
|
| 200 | - */ |
|
| 201 | - public function withSubject(string $subject): self |
|
| 202 | - { |
|
| 203 | - return $this->withConstraint(RegisteredClaim::NAME_SUBJECT, $subject); |
|
| 204 | - } |
|
| 195 | + /** |
|
| 196 | + * Get self with the subject constraint. |
|
| 197 | + * |
|
| 198 | + * @param string $subject Subject name |
|
| 199 | + * @return self |
|
| 200 | + */ |
|
| 201 | + public function withSubject(string $subject): self |
|
| 202 | + { |
|
| 203 | + return $this->withConstraint(RegisteredClaim::NAME_SUBJECT, $subject); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Get self with the audience constraint. |
|
| 208 | - * |
|
| 209 | - * @param string $audience Audience name |
|
| 210 | - * @return self |
|
| 211 | - */ |
|
| 212 | - public function withAudience(string $audience): self |
|
| 213 | - { |
|
| 214 | - return $this->withConstraint(RegisteredClaim::NAME_AUDIENCE, $audience); |
|
| 215 | - } |
|
| 206 | + /** |
|
| 207 | + * Get self with the audience constraint. |
|
| 208 | + * |
|
| 209 | + * @param string $audience Audience name |
|
| 210 | + * @return self |
|
| 211 | + */ |
|
| 212 | + public function withAudience(string $audience): self |
|
| 213 | + { |
|
| 214 | + return $this->withConstraint(RegisteredClaim::NAME_AUDIENCE, $audience); |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * Get self with the JWT ID constraint. |
|
| 219 | - * |
|
| 220 | - * @param string $id JWT ID |
|
| 221 | - * @return self |
|
| 222 | - */ |
|
| 223 | - public function withID(string $id): self |
|
| 224 | - { |
|
| 225 | - return $this->withConstraint(RegisteredClaim::NAME_JWT_ID, $id); |
|
| 226 | - } |
|
| 217 | + /** |
|
| 218 | + * Get self with the JWT ID constraint. |
|
| 219 | + * |
|
| 220 | + * @param string $id JWT ID |
|
| 221 | + * @return self |
|
| 222 | + */ |
|
| 223 | + public function withID(string $id): self |
|
| 224 | + { |
|
| 225 | + return $this->withConstraint(RegisteredClaim::NAME_JWT_ID, $id); |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Check whether a named constraint is present. |
|
| 230 | - * |
|
| 231 | - * @param string $name Claim name |
|
| 232 | - * @return bool |
|
| 233 | - */ |
|
| 234 | - public function hasConstraint(string $name): bool |
|
| 235 | - { |
|
| 236 | - return isset($this->_constraints[$name]); |
|
| 237 | - } |
|
| 228 | + /** |
|
| 229 | + * Check whether a named constraint is present. |
|
| 230 | + * |
|
| 231 | + * @param string $name Claim name |
|
| 232 | + * @return bool |
|
| 233 | + */ |
|
| 234 | + public function hasConstraint(string $name): bool |
|
| 235 | + { |
|
| 236 | + return isset($this->_constraints[$name]); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * Get a constraint value by the claim name. |
|
| 241 | - * |
|
| 242 | - * @param string $name Claim name |
|
| 243 | - * @throws \LogicException If constraint is not set |
|
| 244 | - * @return mixed Constraint value |
|
| 245 | - */ |
|
| 246 | - public function constraint(string $name) |
|
| 247 | - { |
|
| 248 | - if (!$this->hasConstraint($name)) { |
|
| 249 | - throw new \LogicException("Constraint $name not set."); |
|
| 250 | - } |
|
| 251 | - return $this->_constraints[$name]; |
|
| 252 | - } |
|
| 239 | + /** |
|
| 240 | + * Get a constraint value by the claim name. |
|
| 241 | + * |
|
| 242 | + * @param string $name Claim name |
|
| 243 | + * @throws \LogicException If constraint is not set |
|
| 244 | + * @return mixed Constraint value |
|
| 245 | + */ |
|
| 246 | + public function constraint(string $name) |
|
| 247 | + { |
|
| 248 | + if (!$this->hasConstraint($name)) { |
|
| 249 | + throw new \LogicException("Constraint $name not set."); |
|
| 250 | + } |
|
| 251 | + return $this->_constraints[$name]; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * Check whether a validator is defined for the given claim name. |
|
| 256 | - * |
|
| 257 | - * @param string $name Claim name |
|
| 258 | - * @return bool |
|
| 259 | - */ |
|
| 260 | - public function hasValidator(string $name): bool |
|
| 261 | - { |
|
| 262 | - return isset($this->_validators[$name]); |
|
| 263 | - } |
|
| 254 | + /** |
|
| 255 | + * Check whether a validator is defined for the given claim name. |
|
| 256 | + * |
|
| 257 | + * @param string $name Claim name |
|
| 258 | + * @return bool |
|
| 259 | + */ |
|
| 260 | + public function hasValidator(string $name): bool |
|
| 261 | + { |
|
| 262 | + return isset($this->_validators[$name]); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * Get explicitly defined validator by the claim name. |
|
| 267 | - * |
|
| 268 | - * @param string $name Claim name |
|
| 269 | - * @throws \LogicException If validator is not set |
|
| 270 | - * @return Validator |
|
| 271 | - */ |
|
| 272 | - public function validator(string $name): Validator |
|
| 273 | - { |
|
| 274 | - if (!$this->hasValidator($name)) { |
|
| 275 | - throw new \LogicException("Validator $name not set."); |
|
| 276 | - } |
|
| 277 | - return $this->_validators[$name]; |
|
| 278 | - } |
|
| 265 | + /** |
|
| 266 | + * Get explicitly defined validator by the claim name. |
|
| 267 | + * |
|
| 268 | + * @param string $name Claim name |
|
| 269 | + * @throws \LogicException If validator is not set |
|
| 270 | + * @return Validator |
|
| 271 | + */ |
|
| 272 | + public function validator(string $name): Validator |
|
| 273 | + { |
|
| 274 | + if (!$this->hasValidator($name)) { |
|
| 275 | + throw new \LogicException("Validator $name not set."); |
|
| 276 | + } |
|
| 277 | + return $this->_validators[$name]; |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - /** |
|
| 281 | - * Get a set of JSON Web Keys defined in this context. |
|
| 282 | - * |
|
| 283 | - * @return JWKSet |
|
| 284 | - */ |
|
| 285 | - public function keys(): JWKSet |
|
| 286 | - { |
|
| 287 | - return $this->_keys; |
|
| 288 | - } |
|
| 280 | + /** |
|
| 281 | + * Get a set of JSON Web Keys defined in this context. |
|
| 282 | + * |
|
| 283 | + * @return JWKSet |
|
| 284 | + */ |
|
| 285 | + public function keys(): JWKSet |
|
| 286 | + { |
|
| 287 | + return $this->_keys; |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | - /** |
|
| 291 | - * Get self with 'allow unsecured' flag set. |
|
| 292 | - * |
|
| 293 | - * If the unsecured JWT's are allowed, claims shall be considered valid even |
|
| 294 | - * though they are not signed nor encrypted. |
|
| 295 | - * |
|
| 296 | - * @param bool $allow Whether to allow unsecured JWT's |
|
| 297 | - * @return self |
|
| 298 | - */ |
|
| 299 | - public function withUnsecuredAllowed(bool $allow): self |
|
| 300 | - { |
|
| 301 | - $obj = clone $this; |
|
| 302 | - $obj->_allowUnsecured = $allow; |
|
| 303 | - return $obj; |
|
| 304 | - } |
|
| 290 | + /** |
|
| 291 | + * Get self with 'allow unsecured' flag set. |
|
| 292 | + * |
|
| 293 | + * If the unsecured JWT's are allowed, claims shall be considered valid even |
|
| 294 | + * though they are not signed nor encrypted. |
|
| 295 | + * |
|
| 296 | + * @param bool $allow Whether to allow unsecured JWT's |
|
| 297 | + * @return self |
|
| 298 | + */ |
|
| 299 | + public function withUnsecuredAllowed(bool $allow): self |
|
| 300 | + { |
|
| 301 | + $obj = clone $this; |
|
| 302 | + $obj->_allowUnsecured = $allow; |
|
| 303 | + return $obj; |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - /** |
|
| 307 | - * Check whether the unsecured JWT's are allowed. |
|
| 308 | - * |
|
| 309 | - * @return bool |
|
| 310 | - */ |
|
| 311 | - public function isUnsecuredAllowed(): bool |
|
| 312 | - { |
|
| 313 | - return $this->_allowUnsecured; |
|
| 314 | - } |
|
| 306 | + /** |
|
| 307 | + * Check whether the unsecured JWT's are allowed. |
|
| 308 | + * |
|
| 309 | + * @return bool |
|
| 310 | + */ |
|
| 311 | + public function isUnsecuredAllowed(): bool |
|
| 312 | + { |
|
| 313 | + return $this->_allowUnsecured; |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - /** |
|
| 317 | - * Validate claims. |
|
| 318 | - * |
|
| 319 | - * @param Claims $claims |
|
| 320 | - * @throws ValidationException If any of the claims is not valid |
|
| 321 | - * @return self |
|
| 322 | - */ |
|
| 323 | - public function validate(Claims $claims): self |
|
| 324 | - { |
|
| 325 | - foreach ($claims as $claim) { |
|
| 326 | - if (!$claim->validateWithContext($this)) { |
|
| 327 | - throw new ValidationException( |
|
| 328 | - "Validation of claim '" . $claim->name() . "' failed."); |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - return $this; |
|
| 332 | - } |
|
| 316 | + /** |
|
| 317 | + * Validate claims. |
|
| 318 | + * |
|
| 319 | + * @param Claims $claims |
|
| 320 | + * @throws ValidationException If any of the claims is not valid |
|
| 321 | + * @return self |
|
| 322 | + */ |
|
| 323 | + public function validate(Claims $claims): self |
|
| 324 | + { |
|
| 325 | + foreach ($claims as $claim) { |
|
| 326 | + if (!$claim->validateWithContext($this)) { |
|
| 327 | + throw new ValidationException( |
|
| 328 | + "Validation of claim '" . $claim->name() . "' failed."); |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + return $this; |
|
| 332 | + } |
|
| 333 | 333 | } |
@@ -9,10 +9,10 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | interface EncryptionAlgorithmParameterValue |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Get algorithm type as an 'enc' parameter value. |
|
| 14 | - * |
|
| 15 | - * @return string |
|
| 16 | - */ |
|
| 17 | - public function encryptionAlgorithmParamValue(): string; |
|
| 12 | + /** |
|
| 13 | + * Get algorithm type as an 'enc' parameter value. |
|
| 14 | + * |
|
| 15 | + * @return string |
|
| 16 | + */ |
|
| 17 | + public function encryptionAlgorithmParamValue(): string; |
|
| 18 | 18 | } |
@@ -13,15 +13,15 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class X509URLParameter extends JWTParameter |
| 15 | 15 | { |
| 16 | - use StringParameterValue; |
|
| 16 | + use StringParameterValue; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @param string $uri |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $uri) |
|
| 24 | - { |
|
| 25 | - parent::__construct(self::PARAM_X509_URL, $uri); |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @param string $uri |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $uri) |
|
| 24 | + { |
|
| 25 | + parent::__construct(self::PARAM_X509_URL, $uri); |
|
| 26 | + } |
|
| 27 | 27 | } |