@@ -7,93 +7,93 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | trait NumericDateClaim |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Constructor. |
|
| 12 | - * |
|
| 13 | - * @param int $timestamp Unix timestamp |
|
| 14 | - */ |
|
| 15 | - abstract public function __construct($timestamp); |
|
| 10 | + /** |
|
| 11 | + * Constructor. |
|
| 12 | + * |
|
| 13 | + * @param int $timestamp Unix timestamp |
|
| 14 | + */ |
|
| 15 | + abstract public function __construct($timestamp); |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Get the parameter value. |
|
| 19 | - * |
|
| 20 | - * @return string |
|
| 21 | - */ |
|
| 22 | - abstract public function value(); |
|
| 17 | + /** |
|
| 18 | + * Get the parameter value. |
|
| 19 | + * |
|
| 20 | + * @return string |
|
| 21 | + */ |
|
| 22 | + abstract public function value(); |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Initialize instance from date/time string. |
|
| 26 | - * |
|
| 27 | - * @param string $time <code>strtotime</code> compatible time string |
|
| 28 | - * @param string $tz Default timezone |
|
| 29 | - * @throws \RuntimeException |
|
| 30 | - * @return static |
|
| 31 | - */ |
|
| 32 | - public static function fromString($time, $tz = "UTC") |
|
| 33 | - { |
|
| 34 | - try { |
|
| 35 | - $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 36 | - return new static($dt->getTimestamp()); |
|
| 37 | - } catch (\Exception $e) { |
|
| 38 | - throw new \RuntimeException( |
|
| 39 | - "Failed to create DateTime: " . |
|
| 40 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 41 | - } |
|
| 42 | - } |
|
| 24 | + /** |
|
| 25 | + * Initialize instance from date/time string. |
|
| 26 | + * |
|
| 27 | + * @param string $time <code>strtotime</code> compatible time string |
|
| 28 | + * @param string $tz Default timezone |
|
| 29 | + * @throws \RuntimeException |
|
| 30 | + * @return static |
|
| 31 | + */ |
|
| 32 | + public static function fromString($time, $tz = "UTC") |
|
| 33 | + { |
|
| 34 | + try { |
|
| 35 | + $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 36 | + return new static($dt->getTimestamp()); |
|
| 37 | + } catch (\Exception $e) { |
|
| 38 | + throw new \RuntimeException( |
|
| 39 | + "Failed to create DateTime: " . |
|
| 40 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Get date as a unix timestamp. |
|
| 46 | - * |
|
| 47 | - * @return int |
|
| 48 | - */ |
|
| 49 | - public function timestamp() |
|
| 50 | - { |
|
| 51 | - return (int) $this->value(); |
|
| 52 | - } |
|
| 44 | + /** |
|
| 45 | + * Get date as a unix timestamp. |
|
| 46 | + * |
|
| 47 | + * @return int |
|
| 48 | + */ |
|
| 49 | + public function timestamp() |
|
| 50 | + { |
|
| 51 | + return (int) $this->value(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Get date as a datetime object. |
|
| 56 | - * |
|
| 57 | - * @param string $tz Timezone |
|
| 58 | - * @throws \RuntimeException |
|
| 59 | - * @return \DateTimeImmutable |
|
| 60 | - */ |
|
| 61 | - public function dateTime($tz = "UTC") |
|
| 62 | - { |
|
| 63 | - $dt = \DateTimeImmutable::createFromFormat("!U", $this->value(), |
|
| 64 | - self::_createTimeZone($tz)); |
|
| 65 | - if (false === $dt) { |
|
| 66 | - throw new \RuntimeException( |
|
| 67 | - "Failed to create DateTime: " . |
|
| 68 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 69 | - } |
|
| 70 | - return $dt; |
|
| 71 | - } |
|
| 54 | + /** |
|
| 55 | + * Get date as a datetime object. |
|
| 56 | + * |
|
| 57 | + * @param string $tz Timezone |
|
| 58 | + * @throws \RuntimeException |
|
| 59 | + * @return \DateTimeImmutable |
|
| 60 | + */ |
|
| 61 | + public function dateTime($tz = "UTC") |
|
| 62 | + { |
|
| 63 | + $dt = \DateTimeImmutable::createFromFormat("!U", $this->value(), |
|
| 64 | + self::_createTimeZone($tz)); |
|
| 65 | + if (false === $dt) { |
|
| 66 | + throw new \RuntimeException( |
|
| 67 | + "Failed to create DateTime: " . |
|
| 68 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 69 | + } |
|
| 70 | + return $dt; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Create DateTimeZone object from string. |
|
| 75 | - * |
|
| 76 | - * @param string $tz |
|
| 77 | - * @throws \UnexpectedValueException |
|
| 78 | - * @return \DateTimeZone |
|
| 79 | - */ |
|
| 80 | - private static function _createTimeZone($tz) |
|
| 81 | - { |
|
| 82 | - try { |
|
| 83 | - return new \DateTimeZone($tz); |
|
| 84 | - } catch (\Exception $e) { |
|
| 85 | - throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 86 | - } |
|
| 87 | - } |
|
| 73 | + /** |
|
| 74 | + * Create DateTimeZone object from string. |
|
| 75 | + * |
|
| 76 | + * @param string $tz |
|
| 77 | + * @throws \UnexpectedValueException |
|
| 78 | + * @return \DateTimeZone |
|
| 79 | + */ |
|
| 80 | + private static function _createTimeZone($tz) |
|
| 81 | + { |
|
| 82 | + try { |
|
| 83 | + return new \DateTimeZone($tz); |
|
| 84 | + } catch (\Exception $e) { |
|
| 85 | + throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Get last error caused by DateTimeImmutable. |
|
| 91 | - * |
|
| 92 | - * @return string |
|
| 93 | - */ |
|
| 94 | - private static function _getLastDateTimeImmutableErrorsStr() |
|
| 95 | - { |
|
| 96 | - $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 97 | - return implode(", ", $errors); |
|
| 98 | - } |
|
| 89 | + /** |
|
| 90 | + * Get last error caused by DateTimeImmutable. |
|
| 91 | + * |
|
| 92 | + * @return string |
|
| 93 | + */ |
|
| 94 | + private static function _getLastDateTimeImmutableErrorsStr() |
|
| 95 | + { |
|
| 96 | + $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 97 | + return implode(", ", $errors); |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -9,36 +9,36 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | trait ReferenceTimeValidation |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Validate the claim against given constraint. |
|
| 14 | - * |
|
| 15 | - * @param mixed $constraint |
|
| 16 | - * @return bool |
|
| 17 | - */ |
|
| 18 | - abstract public function validate($constraint); |
|
| 12 | + /** |
|
| 13 | + * Validate the claim against given constraint. |
|
| 14 | + * |
|
| 15 | + * @param mixed $constraint |
|
| 16 | + * @return bool |
|
| 17 | + */ |
|
| 18 | + abstract public function validate($constraint); |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Override default Claim validation. |
|
| 22 | - * |
|
| 23 | - * Uses reference time of the validation context as a constraint. |
|
| 24 | - * |
|
| 25 | - * @see \JWX\JWT\Claim\Claim::validateWithContext() |
|
| 26 | - * @param ValidationContext $ctx |
|
| 27 | - * @return bool |
|
| 28 | - */ |
|
| 29 | - public function validateWithContext(ValidationContext $ctx) |
|
| 30 | - { |
|
| 31 | - if ($ctx->hasReferenceTime()) { |
|
| 32 | - // try to validate with leeway added |
|
| 33 | - if ($this->validate($ctx->referenceTime() + $ctx->leeway())) { |
|
| 34 | - return true; |
|
| 35 | - } |
|
| 36 | - // try to validate with leeway substracted |
|
| 37 | - if ($this->validate($ctx->referenceTime() - $ctx->leeway())) { |
|
| 38 | - return true; |
|
| 39 | - } |
|
| 40 | - return false; |
|
| 41 | - } |
|
| 42 | - return true; |
|
| 43 | - } |
|
| 20 | + /** |
|
| 21 | + * Override default Claim validation. |
|
| 22 | + * |
|
| 23 | + * Uses reference time of the validation context as a constraint. |
|
| 24 | + * |
|
| 25 | + * @see \JWX\JWT\Claim\Claim::validateWithContext() |
|
| 26 | + * @param ValidationContext $ctx |
|
| 27 | + * @return bool |
|
| 28 | + */ |
|
| 29 | + public function validateWithContext(ValidationContext $ctx) |
|
| 30 | + { |
|
| 31 | + if ($ctx->hasReferenceTime()) { |
|
| 32 | + // try to validate with leeway added |
|
| 33 | + if ($this->validate($ctx->referenceTime() + $ctx->leeway())) { |
|
| 34 | + return true; |
|
| 35 | + } |
|
| 36 | + // try to validate with leeway substracted |
|
| 37 | + if ($this->validate($ctx->referenceTime() - $ctx->leeway())) { |
|
| 38 | + return true; |
|
| 39 | + } |
|
| 40 | + return false; |
|
| 41 | + } |
|
| 42 | + return true; |
|
| 43 | + } |
|
| 44 | 44 | } |
@@ -7,184 +7,184 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | trait TypedClaims |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Check whether the claim is present. |
|
| 12 | - * |
|
| 13 | - * @param string $name Claim name |
|
| 14 | - * @return bool |
|
| 15 | - */ |
|
| 16 | - abstract public function has($name); |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Get the claim by name. |
|
| 20 | - * |
|
| 21 | - * @param string $name Claim name |
|
| 22 | - * @return Claim |
|
| 23 | - */ |
|
| 24 | - abstract public function get($name); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Check whether the issuer claim is present. |
|
| 28 | - * |
|
| 29 | - * @return bool |
|
| 30 | - */ |
|
| 31 | - public function hasIssuer() |
|
| 32 | - { |
|
| 33 | - return $this->has(RegisteredClaim::NAME_ISSUER); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Get the issuer claim. |
|
| 38 | - * |
|
| 39 | - * @return IssuerClaim |
|
| 40 | - */ |
|
| 41 | - public function issuer() |
|
| 42 | - { |
|
| 43 | - return self::_checkType($this->get(RegisteredClaim::NAME_ISSUER), |
|
| 44 | - IssuerClaim::class); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Check whether the subject claim is present. |
|
| 49 | - * |
|
| 50 | - * @return bool |
|
| 51 | - */ |
|
| 52 | - public function hasSubject() |
|
| 53 | - { |
|
| 54 | - return $this->has(RegisteredClaim::NAME_SUBJECT); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Get the subject claim. |
|
| 59 | - * |
|
| 60 | - * @return SubjectClaim |
|
| 61 | - */ |
|
| 62 | - public function subject() |
|
| 63 | - { |
|
| 64 | - return self::_checkType($this->get(RegisteredClaim::NAME_SUBJECT), |
|
| 65 | - SubjectClaim::class); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Check whether the audience claim is present. |
|
| 70 | - * |
|
| 71 | - * @return bool |
|
| 72 | - */ |
|
| 73 | - public function hasAudience() |
|
| 74 | - { |
|
| 75 | - return $this->has(RegisteredClaim::NAME_AUDIENCE); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get the audience claim. |
|
| 80 | - * |
|
| 81 | - * @return AudienceClaim |
|
| 82 | - */ |
|
| 83 | - public function audience() |
|
| 84 | - { |
|
| 85 | - return self::_checkType($this->get(RegisteredClaim::NAME_AUDIENCE), |
|
| 86 | - AudienceClaim::class); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Check whether the expiration time claim is present. |
|
| 91 | - * |
|
| 92 | - * @return bool |
|
| 93 | - */ |
|
| 94 | - public function hasExpirationTime() |
|
| 95 | - { |
|
| 96 | - return $this->has(RegisteredClaim::NAME_EXPIRATION_TIME); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Get the expiration time claim. |
|
| 101 | - * |
|
| 102 | - * @return ExpirationTimeClaim |
|
| 103 | - */ |
|
| 104 | - public function expirationTime() |
|
| 105 | - { |
|
| 106 | - return self::_checkType( |
|
| 107 | - $this->get(RegisteredClaim::NAME_EXPIRATION_TIME), |
|
| 108 | - ExpirationTimeClaim::class); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Check whether the not before claim is present. |
|
| 113 | - * |
|
| 114 | - * @return bool |
|
| 115 | - */ |
|
| 116 | - public function hasNotBefore() |
|
| 117 | - { |
|
| 118 | - return $this->has(RegisteredClaim::NAME_NOT_BEFORE); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get the not before claim. |
|
| 123 | - * |
|
| 124 | - * @return NotBeforeClaim |
|
| 125 | - */ |
|
| 126 | - public function notBefore() |
|
| 127 | - { |
|
| 128 | - return self::_checkType($this->get(RegisteredClaim::NAME_NOT_BEFORE), |
|
| 129 | - NotBeforeClaim::class); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Check whether the issued at claim is present. |
|
| 134 | - * |
|
| 135 | - * @return bool |
|
| 136 | - */ |
|
| 137 | - public function hasIssuedAt() |
|
| 138 | - { |
|
| 139 | - return $this->has(RegisteredClaim::NAME_ISSUED_AT); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Get the issued at claim. |
|
| 144 | - * |
|
| 145 | - * @return IssuedAtClaim |
|
| 146 | - */ |
|
| 147 | - public function issuedAt() |
|
| 148 | - { |
|
| 149 | - return self::_checkType($this->get(RegisteredClaim::NAME_ISSUED_AT), |
|
| 150 | - IssuedAtClaim::class); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Check whether the JWT ID claim is present. |
|
| 155 | - * |
|
| 156 | - * @return bool |
|
| 157 | - */ |
|
| 158 | - public function hasJWTID() |
|
| 159 | - { |
|
| 160 | - return $this->has(RegisteredClaim::NAME_JWT_ID); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Get the JWT ID claim. |
|
| 165 | - * |
|
| 166 | - * @return JWTIDClaim |
|
| 167 | - */ |
|
| 168 | - public function JWTID() |
|
| 169 | - { |
|
| 170 | - return self::_checkType($this->get(RegisteredClaim::NAME_JWT_ID), |
|
| 171 | - JWTIDClaim::class); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Check that the claim is an instance of the given class. |
|
| 176 | - * |
|
| 177 | - * @param Claim $claim Claim object |
|
| 178 | - * @param string $cls Class name |
|
| 179 | - * @throws \UnexpectedValueException |
|
| 180 | - * @return Claim |
|
| 181 | - */ |
|
| 182 | - private static function _checkType(Claim $claim, $cls) |
|
| 183 | - { |
|
| 184 | - if (!$claim instanceof $cls) { |
|
| 185 | - throw new \UnexpectedValueException( |
|
| 186 | - "$cls expected, got " . get_class($claim)); |
|
| 187 | - } |
|
| 188 | - return $claim; |
|
| 189 | - } |
|
| 10 | + /** |
|
| 11 | + * Check whether the claim is present. |
|
| 12 | + * |
|
| 13 | + * @param string $name Claim name |
|
| 14 | + * @return bool |
|
| 15 | + */ |
|
| 16 | + abstract public function has($name); |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Get the claim by name. |
|
| 20 | + * |
|
| 21 | + * @param string $name Claim name |
|
| 22 | + * @return Claim |
|
| 23 | + */ |
|
| 24 | + abstract public function get($name); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Check whether the issuer claim is present. |
|
| 28 | + * |
|
| 29 | + * @return bool |
|
| 30 | + */ |
|
| 31 | + public function hasIssuer() |
|
| 32 | + { |
|
| 33 | + return $this->has(RegisteredClaim::NAME_ISSUER); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Get the issuer claim. |
|
| 38 | + * |
|
| 39 | + * @return IssuerClaim |
|
| 40 | + */ |
|
| 41 | + public function issuer() |
|
| 42 | + { |
|
| 43 | + return self::_checkType($this->get(RegisteredClaim::NAME_ISSUER), |
|
| 44 | + IssuerClaim::class); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Check whether the subject claim is present. |
|
| 49 | + * |
|
| 50 | + * @return bool |
|
| 51 | + */ |
|
| 52 | + public function hasSubject() |
|
| 53 | + { |
|
| 54 | + return $this->has(RegisteredClaim::NAME_SUBJECT); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Get the subject claim. |
|
| 59 | + * |
|
| 60 | + * @return SubjectClaim |
|
| 61 | + */ |
|
| 62 | + public function subject() |
|
| 63 | + { |
|
| 64 | + return self::_checkType($this->get(RegisteredClaim::NAME_SUBJECT), |
|
| 65 | + SubjectClaim::class); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Check whether the audience claim is present. |
|
| 70 | + * |
|
| 71 | + * @return bool |
|
| 72 | + */ |
|
| 73 | + public function hasAudience() |
|
| 74 | + { |
|
| 75 | + return $this->has(RegisteredClaim::NAME_AUDIENCE); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get the audience claim. |
|
| 80 | + * |
|
| 81 | + * @return AudienceClaim |
|
| 82 | + */ |
|
| 83 | + public function audience() |
|
| 84 | + { |
|
| 85 | + return self::_checkType($this->get(RegisteredClaim::NAME_AUDIENCE), |
|
| 86 | + AudienceClaim::class); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Check whether the expiration time claim is present. |
|
| 91 | + * |
|
| 92 | + * @return bool |
|
| 93 | + */ |
|
| 94 | + public function hasExpirationTime() |
|
| 95 | + { |
|
| 96 | + return $this->has(RegisteredClaim::NAME_EXPIRATION_TIME); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Get the expiration time claim. |
|
| 101 | + * |
|
| 102 | + * @return ExpirationTimeClaim |
|
| 103 | + */ |
|
| 104 | + public function expirationTime() |
|
| 105 | + { |
|
| 106 | + return self::_checkType( |
|
| 107 | + $this->get(RegisteredClaim::NAME_EXPIRATION_TIME), |
|
| 108 | + ExpirationTimeClaim::class); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Check whether the not before claim is present. |
|
| 113 | + * |
|
| 114 | + * @return bool |
|
| 115 | + */ |
|
| 116 | + public function hasNotBefore() |
|
| 117 | + { |
|
| 118 | + return $this->has(RegisteredClaim::NAME_NOT_BEFORE); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get the not before claim. |
|
| 123 | + * |
|
| 124 | + * @return NotBeforeClaim |
|
| 125 | + */ |
|
| 126 | + public function notBefore() |
|
| 127 | + { |
|
| 128 | + return self::_checkType($this->get(RegisteredClaim::NAME_NOT_BEFORE), |
|
| 129 | + NotBeforeClaim::class); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Check whether the issued at claim is present. |
|
| 134 | + * |
|
| 135 | + * @return bool |
|
| 136 | + */ |
|
| 137 | + public function hasIssuedAt() |
|
| 138 | + { |
|
| 139 | + return $this->has(RegisteredClaim::NAME_ISSUED_AT); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Get the issued at claim. |
|
| 144 | + * |
|
| 145 | + * @return IssuedAtClaim |
|
| 146 | + */ |
|
| 147 | + public function issuedAt() |
|
| 148 | + { |
|
| 149 | + return self::_checkType($this->get(RegisteredClaim::NAME_ISSUED_AT), |
|
| 150 | + IssuedAtClaim::class); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Check whether the JWT ID claim is present. |
|
| 155 | + * |
|
| 156 | + * @return bool |
|
| 157 | + */ |
|
| 158 | + public function hasJWTID() |
|
| 159 | + { |
|
| 160 | + return $this->has(RegisteredClaim::NAME_JWT_ID); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Get the JWT ID claim. |
|
| 165 | + * |
|
| 166 | + * @return JWTIDClaim |
|
| 167 | + */ |
|
| 168 | + public function JWTID() |
|
| 169 | + { |
|
| 170 | + return self::_checkType($this->get(RegisteredClaim::NAME_JWT_ID), |
|
| 171 | + JWTIDClaim::class); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Check that the claim is an instance of the given class. |
|
| 176 | + * |
|
| 177 | + * @param Claim $claim Claim object |
|
| 178 | + * @param string $cls Class name |
|
| 179 | + * @throws \UnexpectedValueException |
|
| 180 | + * @return Claim |
|
| 181 | + */ |
|
| 182 | + private static function _checkType(Claim $claim, $cls) |
|
| 183 | + { |
|
| 184 | + if (!$claim instanceof $cls) { |
|
| 185 | + throw new \UnexpectedValueException( |
|
| 186 | + "$cls expected, got " . get_class($claim)); |
|
| 187 | + } |
|
| 188 | + return $claim; |
|
| 189 | + } |
|
| 190 | 190 | } |
@@ -11,17 +11,17 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class X509CertificateSHA1ThumbprintParameter extends JWTParameter |
| 13 | 13 | { |
| 14 | - use Base64URLValue; |
|
| 14 | + use Base64URLValue; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $thumbprint Base64url encoded SHA-1 hash |
|
| 20 | - */ |
|
| 21 | - public function __construct($thumbprint) |
|
| 22 | - { |
|
| 23 | - $this->_validateEncoding($thumbprint); |
|
| 24 | - parent::__construct(self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT, |
|
| 25 | - (string) $thumbprint); |
|
| 26 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $thumbprint Base64url encoded SHA-1 hash |
|
| 20 | + */ |
|
| 21 | + public function __construct($thumbprint) |
|
| 22 | + { |
|
| 23 | + $this->_validateEncoding($thumbprint); |
|
| 24 | + parent::__construct(self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT, |
|
| 25 | + (string) $thumbprint); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -11,26 +11,26 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class InitializationVectorParameter extends JWTParameter |
| 13 | 13 | { |
| 14 | - use Base64URLValue; |
|
| 14 | + use Base64URLValue; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $iv Base64url encoded initialization vector |
|
| 20 | - */ |
|
| 21 | - public function __construct($iv) |
|
| 22 | - { |
|
| 23 | - $this->_validateEncoding($iv); |
|
| 24 | - parent::__construct(self::PARAM_INITIALIZATION_VECTOR, (string) $iv); |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $iv Base64url encoded initialization vector |
|
| 20 | + */ |
|
| 21 | + public function __construct($iv) |
|
| 22 | + { |
|
| 23 | + $this->_validateEncoding($iv); |
|
| 24 | + parent::__construct(self::PARAM_INITIALIZATION_VECTOR, (string) $iv); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Get the initialization vector. |
|
| 29 | - * |
|
| 30 | - * @return string |
|
| 31 | - */ |
|
| 32 | - public function initializationVector() |
|
| 33 | - { |
|
| 34 | - return $this->string(); |
|
| 35 | - } |
|
| 27 | + /** |
|
| 28 | + * Get the initialization vector. |
|
| 29 | + * |
|
| 30 | + * @return string |
|
| 31 | + */ |
|
| 32 | + public function initializationVector() |
|
| 33 | + { |
|
| 34 | + return $this->string(); |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -9,24 +9,24 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class PBES2CountParameter extends JWTParameter |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Constructor. |
|
| 14 | - * |
|
| 15 | - * @param int $count |
|
| 16 | - */ |
|
| 17 | - public function __construct($count) |
|
| 18 | - { |
|
| 19 | - parent::__construct(self::PARAM_PBES2_COUNT, intval($count)); |
|
| 20 | - } |
|
| 12 | + /** |
|
| 13 | + * Constructor. |
|
| 14 | + * |
|
| 15 | + * @param int $count |
|
| 16 | + */ |
|
| 17 | + public function __construct($count) |
|
| 18 | + { |
|
| 19 | + parent::__construct(self::PARAM_PBES2_COUNT, intval($count)); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Initialize from a JSON value. |
|
| 24 | - * |
|
| 25 | - * @param int $value |
|
| 26 | - * @return self |
|
| 27 | - */ |
|
| 28 | - public static function fromJSONValue($value) |
|
| 29 | - { |
|
| 30 | - return new self(intval($value)); |
|
| 31 | - } |
|
| 22 | + /** |
|
| 23 | + * Initialize from a JSON value. |
|
| 24 | + * |
|
| 25 | + * @param int $value |
|
| 26 | + * @return self |
|
| 27 | + */ |
|
| 28 | + public static function fromJSONValue($value) |
|
| 29 | + { |
|
| 30 | + return new self(intval($value)); |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -7,10 +7,10 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | interface EncryptionAlgorithmParameterValue |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Get algorithm type as an 'enc' parameter value. |
|
| 12 | - * |
|
| 13 | - * @return string |
|
| 14 | - */ |
|
| 15 | - public function encryptionAlgorithmParamValue(); |
|
| 10 | + /** |
|
| 11 | + * Get algorithm type as an 'enc' parameter value. |
|
| 12 | + * |
|
| 13 | + * @return string |
|
| 14 | + */ |
|
| 15 | + public function encryptionAlgorithmParamValue(); |
|
| 16 | 16 | } |
@@ -12,21 +12,21 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class X509CertificateChainParameter extends JWTParameter |
| 14 | 14 | { |
| 15 | - use ArrayParameterValue; |
|
| 15 | + use ArrayParameterValue; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string ...$certs Base64 encoded DER certificates |
|
| 21 | - */ |
|
| 22 | - public function __construct(...$certs) |
|
| 23 | - { |
|
| 24 | - foreach ($certs as $cert) { |
|
| 25 | - if (!Base64::isValid($cert)) { |
|
| 26 | - throw new \UnexpectedValueException( |
|
| 27 | - "Certificate must be base64 encoded."); |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - parent::__construct(self::PARAM_X509_CERTIFICATE_CHAIN, $certs); |
|
| 31 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string ...$certs Base64 encoded DER certificates |
|
| 21 | + */ |
|
| 22 | + public function __construct(...$certs) |
|
| 23 | + { |
|
| 24 | + foreach ($certs as $cert) { |
|
| 25 | + if (!Base64::isValid($cert)) { |
|
| 26 | + throw new \UnexpectedValueException( |
|
| 27 | + "Certificate must be base64 encoded."); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + parent::__construct(self::PARAM_X509_CERTIFICATE_CHAIN, $certs); |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -11,22 +11,22 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class ContentTypeParameter extends JWTParameter |
| 13 | 13 | { |
| 14 | - use StringParameterValue; |
|
| 14 | + use StringParameterValue; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Content type for the nested JWT. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - const TYPE_JWT = "JWT"; |
|
| 16 | + /** |
|
| 17 | + * Content type for the nested JWT. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + const TYPE_JWT = "JWT"; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Constructor. |
|
| 25 | - * |
|
| 26 | - * @param string $type |
|
| 27 | - */ |
|
| 28 | - public function __construct($type) |
|
| 29 | - { |
|
| 30 | - parent::__construct(self::PARAM_CONTENT_TYPE, (string) $type); |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Constructor. |
|
| 25 | + * |
|
| 26 | + * @param string $type |
|
| 27 | + */ |
|
| 28 | + public function __construct($type) |
|
| 29 | + { |
|
| 30 | + parent::__construct(self::PARAM_CONTENT_TYPE, (string) $type); |
|
| 31 | + } |
|
| 32 | 32 | } |