@@ -13,25 +13,25 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class B64PayloadParameter extends JWTParameter |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param bool $flag |
|
| 20 | - */ |
|
| 21 | - public function __construct(bool $flag) |
|
| 22 | - { |
|
| 23 | - parent::__construct(self::PARAM_BASE64URL_ENCODE_PAYLOAD, $flag); |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param bool $flag |
|
| 20 | + */ |
|
| 21 | + public function __construct(bool $flag) |
|
| 22 | + { |
|
| 23 | + parent::__construct(self::PARAM_BASE64URL_ENCODE_PAYLOAD, $flag); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Initialize from a JSON value. |
|
| 28 | - * |
|
| 29 | - * @param bool $value |
|
| 30 | - * |
|
| 31 | - * @return self |
|
| 32 | - */ |
|
| 33 | - public static function fromJSONValue($value): Parameter |
|
| 34 | - { |
|
| 35 | - return new self(boolval($value)); |
|
| 36 | - } |
|
| 26 | + /** |
|
| 27 | + * Initialize from a JSON value. |
|
| 28 | + * |
|
| 29 | + * @param bool $value |
|
| 30 | + * |
|
| 31 | + * @return self |
|
| 32 | + */ |
|
| 33 | + public static function fromJSONValue($value): Parameter |
|
| 34 | + { |
|
| 35 | + return new self(boolval($value)); |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -14,121 +14,121 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class JWTParameter extends Parameter |
| 16 | 16 | { |
| 17 | - // registered parameter names |
|
| 18 | - const PARAM_ALGORITHM = 'alg'; |
|
| 19 | - const PARAM_JWK_SET_URL = 'jku'; |
|
| 20 | - const PARAM_JSON_WEB_KEY = 'jwk'; |
|
| 21 | - const PARAM_KEY_ID = 'kid'; |
|
| 22 | - const PARAM_X509_URL = 'x5u'; |
|
| 23 | - const PARAM_X509_CERTIFICATE_CHAIN = 'x5c'; |
|
| 24 | - const PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT = 'x5t'; |
|
| 25 | - const PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT = 'x5t#S256'; |
|
| 26 | - const PARAM_TYPE = 'typ'; |
|
| 27 | - const PARAM_CONTENT_TYPE = 'cty'; |
|
| 28 | - const PARAM_CRITICAL = 'crit'; |
|
| 29 | - const PARAM_ENCRYPTION_ALGORITHM = 'enc'; |
|
| 30 | - const PARAM_COMPRESSION_ALGORITHM = 'zip'; |
|
| 31 | - const PARAM_EPHEMERAL_PUBLIC_KEY = 'epk'; |
|
| 32 | - const PARAM_AGREEMENT_PARTYUINFO = 'apu'; |
|
| 33 | - const PARAM_AGREEMENT_PARTYVINFO = 'apv'; |
|
| 34 | - const PARAM_INITIALIZATION_VECTOR = 'iv'; |
|
| 35 | - const PARAM_AUTHENTICATION_TAG = 'tag'; |
|
| 36 | - const PARAM_PBES2_SALT_INPUT = 'p2s'; |
|
| 37 | - const PARAM_PBES2_COUNT = 'p2c'; |
|
| 38 | - const PARAM_BASE64URL_ENCODE_PAYLOAD = 'b64'; |
|
| 17 | + // registered parameter names |
|
| 18 | + const PARAM_ALGORITHM = 'alg'; |
|
| 19 | + const PARAM_JWK_SET_URL = 'jku'; |
|
| 20 | + const PARAM_JSON_WEB_KEY = 'jwk'; |
|
| 21 | + const PARAM_KEY_ID = 'kid'; |
|
| 22 | + const PARAM_X509_URL = 'x5u'; |
|
| 23 | + const PARAM_X509_CERTIFICATE_CHAIN = 'x5c'; |
|
| 24 | + const PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT = 'x5t'; |
|
| 25 | + const PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT = 'x5t#S256'; |
|
| 26 | + const PARAM_TYPE = 'typ'; |
|
| 27 | + const PARAM_CONTENT_TYPE = 'cty'; |
|
| 28 | + const PARAM_CRITICAL = 'crit'; |
|
| 29 | + const PARAM_ENCRYPTION_ALGORITHM = 'enc'; |
|
| 30 | + const PARAM_COMPRESSION_ALGORITHM = 'zip'; |
|
| 31 | + const PARAM_EPHEMERAL_PUBLIC_KEY = 'epk'; |
|
| 32 | + const PARAM_AGREEMENT_PARTYUINFO = 'apu'; |
|
| 33 | + const PARAM_AGREEMENT_PARTYVINFO = 'apv'; |
|
| 34 | + const PARAM_INITIALIZATION_VECTOR = 'iv'; |
|
| 35 | + const PARAM_AUTHENTICATION_TAG = 'tag'; |
|
| 36 | + const PARAM_PBES2_SALT_INPUT = 'p2s'; |
|
| 37 | + const PARAM_PBES2_COUNT = 'p2c'; |
|
| 38 | + const PARAM_BASE64URL_ENCODE_PAYLOAD = 'b64'; |
|
| 39 | 39 | |
| 40 | - // shorthand aliases for parameter names |
|
| 41 | - const P_ALG = self::PARAM_ALGORITHM; |
|
| 42 | - const P_JKU = self::PARAM_JWK_SET_URL; |
|
| 43 | - const P_JWK = self::PARAM_JSON_WEB_KEY; |
|
| 44 | - const P_KID = self::PARAM_KEY_ID; |
|
| 45 | - const P_X5U = self::PARAM_X509_URL; |
|
| 46 | - const P_X5C = self::PARAM_X509_CERTIFICATE_CHAIN; |
|
| 47 | - const P_X5T = self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT; |
|
| 48 | - const P_X5TS256 = self::PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT; |
|
| 49 | - const P_TYP = self::PARAM_TYPE; |
|
| 50 | - const P_CTY = self::PARAM_CONTENT_TYPE; |
|
| 51 | - const P_CRIT = self::PARAM_CRITICAL; |
|
| 52 | - const P_ENC = self::PARAM_ENCRYPTION_ALGORITHM; |
|
| 53 | - const P_ZIP = self::PARAM_COMPRESSION_ALGORITHM; |
|
| 54 | - const P_EPK = self::PARAM_EPHEMERAL_PUBLIC_KEY; |
|
| 55 | - const P_APU = self::PARAM_AGREEMENT_PARTYUINFO; |
|
| 56 | - const P_APV = self::PARAM_AGREEMENT_PARTYVINFO; |
|
| 57 | - const P_IV = self::PARAM_INITIALIZATION_VECTOR; |
|
| 58 | - const P_TAG = self::PARAM_AUTHENTICATION_TAG; |
|
| 59 | - const P_P2S = self::PARAM_PBES2_SALT_INPUT; |
|
| 60 | - const P_P2C = self::PARAM_PBES2_COUNT; |
|
| 61 | - const P_B64 = self::PARAM_BASE64URL_ENCODE_PAYLOAD; |
|
| 40 | + // shorthand aliases for parameter names |
|
| 41 | + const P_ALG = self::PARAM_ALGORITHM; |
|
| 42 | + const P_JKU = self::PARAM_JWK_SET_URL; |
|
| 43 | + const P_JWK = self::PARAM_JSON_WEB_KEY; |
|
| 44 | + const P_KID = self::PARAM_KEY_ID; |
|
| 45 | + const P_X5U = self::PARAM_X509_URL; |
|
| 46 | + const P_X5C = self::PARAM_X509_CERTIFICATE_CHAIN; |
|
| 47 | + const P_X5T = self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT; |
|
| 48 | + const P_X5TS256 = self::PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT; |
|
| 49 | + const P_TYP = self::PARAM_TYPE; |
|
| 50 | + const P_CTY = self::PARAM_CONTENT_TYPE; |
|
| 51 | + const P_CRIT = self::PARAM_CRITICAL; |
|
| 52 | + const P_ENC = self::PARAM_ENCRYPTION_ALGORITHM; |
|
| 53 | + const P_ZIP = self::PARAM_COMPRESSION_ALGORITHM; |
|
| 54 | + const P_EPK = self::PARAM_EPHEMERAL_PUBLIC_KEY; |
|
| 55 | + const P_APU = self::PARAM_AGREEMENT_PARTYUINFO; |
|
| 56 | + const P_APV = self::PARAM_AGREEMENT_PARTYVINFO; |
|
| 57 | + const P_IV = self::PARAM_INITIALIZATION_VECTOR; |
|
| 58 | + const P_TAG = self::PARAM_AUTHENTICATION_TAG; |
|
| 59 | + const P_P2S = self::PARAM_PBES2_SALT_INPUT; |
|
| 60 | + const P_P2C = self::PARAM_PBES2_COUNT; |
|
| 61 | + const P_B64 = self::PARAM_BASE64URL_ENCODE_PAYLOAD; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Mapping from registered JWT parameter name to class name. |
|
| 65 | - * |
|
| 66 | - * @internal |
|
| 67 | - * |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - const MAP_NAME_TO_CLASS = [ |
|
| 71 | - self::P_ALG => AlgorithmParameter::class, |
|
| 72 | - self::P_JKU => JWKSetURLParameter::class, |
|
| 73 | - self::P_JWK => JSONWebKeyParameter::class, |
|
| 74 | - self::P_KID => KeyIDParameter::class, |
|
| 75 | - self::P_X5U => X509URLParameter::class, |
|
| 76 | - self::P_X5C => X509CertificateChainParameter::class, |
|
| 77 | - self::P_X5T => X509CertificateSHA1ThumbprintParameter::class, |
|
| 78 | - self::P_X5TS256 => X509CertificateSHA256ThumbprintParameter::class, |
|
| 79 | - self::P_TYP => TypeParameter::class, |
|
| 80 | - self::P_CTY => ContentTypeParameter::class, |
|
| 81 | - self::P_CRIT => CriticalParameter::class, |
|
| 82 | - self::P_ENC => EncryptionAlgorithmParameter::class, |
|
| 83 | - self::P_ZIP => CompressionAlgorithmParameter::class, |
|
| 84 | - self::P_IV => InitializationVectorParameter::class, |
|
| 85 | - self::P_TAG => AuthenticationTagParameter::class, |
|
| 86 | - self::P_P2S => PBES2SaltInputParameter::class, |
|
| 87 | - self::P_P2C => PBES2CountParameter::class, |
|
| 88 | - self::P_B64 => B64PayloadParameter::class, |
|
| 89 | - ]; |
|
| 63 | + /** |
|
| 64 | + * Mapping from registered JWT parameter name to class name. |
|
| 65 | + * |
|
| 66 | + * @internal |
|
| 67 | + * |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + const MAP_NAME_TO_CLASS = [ |
|
| 71 | + self::P_ALG => AlgorithmParameter::class, |
|
| 72 | + self::P_JKU => JWKSetURLParameter::class, |
|
| 73 | + self::P_JWK => JSONWebKeyParameter::class, |
|
| 74 | + self::P_KID => KeyIDParameter::class, |
|
| 75 | + self::P_X5U => X509URLParameter::class, |
|
| 76 | + self::P_X5C => X509CertificateChainParameter::class, |
|
| 77 | + self::P_X5T => X509CertificateSHA1ThumbprintParameter::class, |
|
| 78 | + self::P_X5TS256 => X509CertificateSHA256ThumbprintParameter::class, |
|
| 79 | + self::P_TYP => TypeParameter::class, |
|
| 80 | + self::P_CTY => ContentTypeParameter::class, |
|
| 81 | + self::P_CRIT => CriticalParameter::class, |
|
| 82 | + self::P_ENC => EncryptionAlgorithmParameter::class, |
|
| 83 | + self::P_ZIP => CompressionAlgorithmParameter::class, |
|
| 84 | + self::P_IV => InitializationVectorParameter::class, |
|
| 85 | + self::P_TAG => AuthenticationTagParameter::class, |
|
| 86 | + self::P_P2S => PBES2SaltInputParameter::class, |
|
| 87 | + self::P_P2C => PBES2CountParameter::class, |
|
| 88 | + self::P_B64 => B64PayloadParameter::class, |
|
| 89 | + ]; |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Constructor. |
|
| 93 | - * |
|
| 94 | - * @param string $name Parameter name |
|
| 95 | - * @param mixed $value Parameter value |
|
| 96 | - */ |
|
| 97 | - public function __construct(string $name, $value) |
|
| 98 | - { |
|
| 99 | - $this->_name = $name; |
|
| 100 | - $this->_value = $value; |
|
| 101 | - } |
|
| 91 | + /** |
|
| 92 | + * Constructor. |
|
| 93 | + * |
|
| 94 | + * @param string $name Parameter name |
|
| 95 | + * @param mixed $value Parameter value |
|
| 96 | + */ |
|
| 97 | + public function __construct(string $name, $value) |
|
| 98 | + { |
|
| 99 | + $this->_name = $name; |
|
| 100 | + $this->_value = $value; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Initialize from a name and a value. |
|
| 105 | - * |
|
| 106 | - * Returns a parameter specific object if one is implemented. |
|
| 107 | - * |
|
| 108 | - * @param string $name Parameter name |
|
| 109 | - * @param mixed $value Parameter value |
|
| 110 | - * |
|
| 111 | - * @return self |
|
| 112 | - */ |
|
| 113 | - public static function fromNameAndValue(string $name, $value): self |
|
| 114 | - { |
|
| 115 | - if (array_key_exists($name, self::MAP_NAME_TO_CLASS)) { |
|
| 116 | - $cls = self::MAP_NAME_TO_CLASS[$name]; |
|
| 117 | - return $cls::fromJSONValue($value); |
|
| 118 | - } |
|
| 119 | - return new self($name, $value); |
|
| 120 | - } |
|
| 103 | + /** |
|
| 104 | + * Initialize from a name and a value. |
|
| 105 | + * |
|
| 106 | + * Returns a parameter specific object if one is implemented. |
|
| 107 | + * |
|
| 108 | + * @param string $name Parameter name |
|
| 109 | + * @param mixed $value Parameter value |
|
| 110 | + * |
|
| 111 | + * @return self |
|
| 112 | + */ |
|
| 113 | + public static function fromNameAndValue(string $name, $value): self |
|
| 114 | + { |
|
| 115 | + if (array_key_exists($name, self::MAP_NAME_TO_CLASS)) { |
|
| 116 | + $cls = self::MAP_NAME_TO_CLASS[$name]; |
|
| 117 | + return $cls::fromJSONValue($value); |
|
| 118 | + } |
|
| 119 | + return new self($name, $value); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Initialize from a JSON value. |
|
| 124 | - * |
|
| 125 | - * @param mixed $value |
|
| 126 | - * |
|
| 127 | - * @return JWTParameter |
|
| 128 | - */ |
|
| 129 | - public static function fromJSONValue($value): Parameter |
|
| 130 | - { |
|
| 131 | - throw new \BadMethodCallException( |
|
| 132 | - __FUNCTION__ . ' must be implemented in a derived class.'); |
|
| 133 | - } |
|
| 122 | + /** |
|
| 123 | + * Initialize from a JSON value. |
|
| 124 | + * |
|
| 125 | + * @param mixed $value |
|
| 126 | + * |
|
| 127 | + * @return JWTParameter |
|
| 128 | + */ |
|
| 129 | + public static function fromJSONValue($value): Parameter |
|
| 130 | + { |
|
| 131 | + throw new \BadMethodCallException( |
|
| 132 | + __FUNCTION__ . ' must be implemented in a derived class.'); |
|
| 133 | + } |
|
| 134 | 134 | } |
@@ -13,38 +13,38 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class PBES2SaltInputParameter extends JWTParameter |
| 15 | 15 | { |
| 16 | - use Base64URLValue; |
|
| 16 | + use Base64URLValue; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @param string $salt Base64url encoded salt input value |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $salt) |
|
| 24 | - { |
|
| 25 | - $this->_validateEncoding($salt); |
|
| 26 | - parent::__construct(self::PARAM_PBES2_SALT_INPUT, $salt); |
|
| 27 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @param string $salt Base64url encoded salt input value |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $salt) |
|
| 24 | + { |
|
| 25 | + $this->_validateEncoding($salt); |
|
| 26 | + parent::__construct(self::PARAM_PBES2_SALT_INPUT, $salt); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Get salt input value. |
|
| 31 | - * |
|
| 32 | - * @return string |
|
| 33 | - */ |
|
| 34 | - public function saltInput(): string |
|
| 35 | - { |
|
| 36 | - return $this->string(); |
|
| 37 | - } |
|
| 29 | + /** |
|
| 30 | + * Get salt input value. |
|
| 31 | + * |
|
| 32 | + * @return string |
|
| 33 | + */ |
|
| 34 | + public function saltInput(): string |
|
| 35 | + { |
|
| 36 | + return $this->string(); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Get computed salt value. |
|
| 41 | - * |
|
| 42 | - * @param AlgorithmParameter $algo |
|
| 43 | - * |
|
| 44 | - * @return string |
|
| 45 | - */ |
|
| 46 | - public function salt(AlgorithmParameter $algo): string |
|
| 47 | - { |
|
| 48 | - return $algo->value() . "\0" . $this->saltInput(); |
|
| 49 | - } |
|
| 39 | + /** |
|
| 40 | + * Get computed salt value. |
|
| 41 | + * |
|
| 42 | + * @param AlgorithmParameter $algo |
|
| 43 | + * |
|
| 44 | + * @return string |
|
| 45 | + */ |
|
| 46 | + public function salt(AlgorithmParameter $algo): string |
|
| 47 | + { |
|
| 48 | + return $algo->value() . "\0" . $this->saltInput(); |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -13,28 +13,28 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class EncryptionAlgorithmParameter extends JWTParameter |
| 15 | 15 | { |
| 16 | - use StringParameterValue; |
|
| 16 | + use StringParameterValue; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @param string $algo Algorithm name |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $algo) |
|
| 24 | - { |
|
| 25 | - parent::__construct(self::PARAM_ENCRYPTION_ALGORITHM, $algo); |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @param string $algo Algorithm name |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $algo) |
|
| 24 | + { |
|
| 25 | + parent::__construct(self::PARAM_ENCRYPTION_ALGORITHM, $algo); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Initialize from EncryptionAlgorithmParameterValue. |
|
| 30 | - * |
|
| 31 | - * @param EncryptionAlgorithmParameterValue $value |
|
| 32 | - * |
|
| 33 | - * @return self |
|
| 34 | - */ |
|
| 35 | - public static function fromAlgorithm( |
|
| 36 | - EncryptionAlgorithmParameterValue $value): JWTParameter |
|
| 37 | - { |
|
| 38 | - return new self($value->encryptionAlgorithmParamValue()); |
|
| 39 | - } |
|
| 28 | + /** |
|
| 29 | + * Initialize from EncryptionAlgorithmParameterValue. |
|
| 30 | + * |
|
| 31 | + * @param EncryptionAlgorithmParameterValue $value |
|
| 32 | + * |
|
| 33 | + * @return self |
|
| 34 | + */ |
|
| 35 | + public static function fromAlgorithm( |
|
| 36 | + EncryptionAlgorithmParameterValue $value): JWTParameter |
|
| 37 | + { |
|
| 38 | + return new self($value->encryptionAlgorithmParamValue()); |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -13,28 +13,28 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class CompressionAlgorithmParameter extends JWTParameter |
| 15 | 15 | { |
| 16 | - use StringParameterValue; |
|
| 16 | + use StringParameterValue; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @param string $algo |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $algo) |
|
| 24 | - { |
|
| 25 | - parent::__construct(self::PARAM_COMPRESSION_ALGORITHM, $algo); |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @param string $algo |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $algo) |
|
| 24 | + { |
|
| 25 | + parent::__construct(self::PARAM_COMPRESSION_ALGORITHM, $algo); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Initialize from CompressionAlgorithmParameterValue. |
|
| 30 | - * |
|
| 31 | - * @param CompressionAlgorithmParameterValue $value |
|
| 32 | - * |
|
| 33 | - * @return self |
|
| 34 | - */ |
|
| 35 | - public static function fromAlgorithm( |
|
| 36 | - CompressionAlgorithmParameterValue $value): JWTParameter |
|
| 37 | - { |
|
| 38 | - return new self($value->compressionParamValue()); |
|
| 39 | - } |
|
| 28 | + /** |
|
| 29 | + * Initialize from CompressionAlgorithmParameterValue. |
|
| 30 | + * |
|
| 31 | + * @param CompressionAlgorithmParameterValue $value |
|
| 32 | + * |
|
| 33 | + * @return self |
|
| 34 | + */ |
|
| 35 | + public static function fromAlgorithm( |
|
| 36 | + CompressionAlgorithmParameterValue $value): JWTParameter |
|
| 37 | + { |
|
| 38 | + return new self($value->compressionParamValue()); |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -13,22 +13,22 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ContentTypeParameter extends JWTParameter |
| 15 | 15 | { |
| 16 | - use StringParameterValue; |
|
| 16 | + use StringParameterValue; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Content type for the nested JWT. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - const TYPE_JWT = 'JWT'; |
|
| 18 | + /** |
|
| 19 | + * Content type for the nested JWT. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + const TYPE_JWT = 'JWT'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Constructor. |
|
| 27 | - * |
|
| 28 | - * @param string $type |
|
| 29 | - */ |
|
| 30 | - public function __construct(string $type) |
|
| 31 | - { |
|
| 32 | - parent::__construct(self::PARAM_CONTENT_TYPE, $type); |
|
| 33 | - } |
|
| 25 | + /** |
|
| 26 | + * Constructor. |
|
| 27 | + * |
|
| 28 | + * @param string $type |
|
| 29 | + */ |
|
| 30 | + public function __construct(string $type) |
|
| 31 | + { |
|
| 32 | + parent::__construct(self::PARAM_CONTENT_TYPE, $type); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -14,34 +14,34 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class JSONWebKeyParameter extends JWTParameter |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param JWK $jwk |
|
| 21 | - */ |
|
| 22 | - public function __construct(JWK $jwk) |
|
| 23 | - { |
|
| 24 | - parent::__construct(self::PARAM_JSON_WEB_KEY, $jwk->toArray()); |
|
| 25 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param JWK $jwk |
|
| 21 | + */ |
|
| 22 | + public function __construct(JWK $jwk) |
|
| 23 | + { |
|
| 24 | + parent::__construct(self::PARAM_JSON_WEB_KEY, $jwk->toArray()); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - public static function fromJSONValue($value): Parameter |
|
| 31 | - { |
|
| 32 | - if (!is_array($value)) { |
|
| 33 | - throw new \UnexpectedValueException('jwk must be an array.'); |
|
| 34 | - } |
|
| 35 | - return new static(JWK::fromArray($value)); |
|
| 36 | - } |
|
| 27 | + /** |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + public static function fromJSONValue($value): Parameter |
|
| 31 | + { |
|
| 32 | + if (!is_array($value)) { |
|
| 33 | + throw new \UnexpectedValueException('jwk must be an array.'); |
|
| 34 | + } |
|
| 35 | + return new static(JWK::fromArray($value)); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Get value as a JWK. |
|
| 40 | - * |
|
| 41 | - * @return JWK |
|
| 42 | - */ |
|
| 43 | - public function jwk(): JWK |
|
| 44 | - { |
|
| 45 | - return JWK::fromArray($this->_value); |
|
| 46 | - } |
|
| 38 | + /** |
|
| 39 | + * Get value as a JWK. |
|
| 40 | + * |
|
| 41 | + * @return JWK |
|
| 42 | + */ |
|
| 43 | + public function jwk(): JWK |
|
| 44 | + { |
|
| 45 | + return JWK::fromArray($this->_value); |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -13,52 +13,52 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class CriticalParameter extends JWTParameter |
| 15 | 15 | { |
| 16 | - use ArrayParameterValue; |
|
| 16 | + use ArrayParameterValue; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @param string ...$names |
|
| 22 | - */ |
|
| 23 | - public function __construct(string ...$names) |
|
| 24 | - { |
|
| 25 | - parent::__construct(self::PARAM_CRITICAL, $names); |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @param string ...$names |
|
| 22 | + */ |
|
| 23 | + public function __construct(string ...$names) |
|
| 24 | + { |
|
| 25 | + parent::__construct(self::PARAM_CRITICAL, $names); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Get self with parameter name added. |
|
| 30 | - * |
|
| 31 | - * @param string $name |
|
| 32 | - * |
|
| 33 | - * @return self |
|
| 34 | - */ |
|
| 35 | - public function withParamName(string $name): self |
|
| 36 | - { |
|
| 37 | - $obj = clone $this; |
|
| 38 | - $obj->_value[] = $name; |
|
| 39 | - $obj->_value = array_values(array_unique($obj->_value)); |
|
| 40 | - return $obj; |
|
| 41 | - } |
|
| 28 | + /** |
|
| 29 | + * Get self with parameter name added. |
|
| 30 | + * |
|
| 31 | + * @param string $name |
|
| 32 | + * |
|
| 33 | + * @return self |
|
| 34 | + */ |
|
| 35 | + public function withParamName(string $name): self |
|
| 36 | + { |
|
| 37 | + $obj = clone $this; |
|
| 38 | + $obj->_value[] = $name; |
|
| 39 | + $obj->_value = array_values(array_unique($obj->_value)); |
|
| 40 | + return $obj; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Check whether given parameter name is critical. |
|
| 45 | - * |
|
| 46 | - * @param string $name |
|
| 47 | - * |
|
| 48 | - * @return bool |
|
| 49 | - */ |
|
| 50 | - public function has(string $name): bool |
|
| 51 | - { |
|
| 52 | - return false !== array_search($name, $this->_value); |
|
| 53 | - } |
|
| 43 | + /** |
|
| 44 | + * Check whether given parameter name is critical. |
|
| 45 | + * |
|
| 46 | + * @param string $name |
|
| 47 | + * |
|
| 48 | + * @return bool |
|
| 49 | + */ |
|
| 50 | + public function has(string $name): bool |
|
| 51 | + { |
|
| 52 | + return false !== array_search($name, $this->_value); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get critical header parameter names. |
|
| 57 | - * |
|
| 58 | - * @return string[] |
|
| 59 | - */ |
|
| 60 | - public function names(): array |
|
| 61 | - { |
|
| 62 | - return $this->_value; |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * Get critical header parameter names. |
|
| 57 | + * |
|
| 58 | + * @return string[] |
|
| 59 | + */ |
|
| 60 | + public function names(): array |
|
| 61 | + { |
|
| 62 | + return $this->_value; |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -26,445 +26,445 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class JWT |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * Type identifier for the signed JWT. |
|
| 31 | - * |
|
| 32 | - * @internal |
|
| 33 | - * |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - const TYPE_JWS = 0; |
|
| 29 | + /** |
|
| 30 | + * Type identifier for the signed JWT. |
|
| 31 | + * |
|
| 32 | + * @internal |
|
| 33 | + * |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + const TYPE_JWS = 0; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Type identifier for the encrypted JWT. |
|
| 40 | - * |
|
| 41 | - * @internal |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - const TYPE_JWE = 1; |
|
| 38 | + /** |
|
| 39 | + * Type identifier for the encrypted JWT. |
|
| 40 | + * |
|
| 41 | + * @internal |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + const TYPE_JWE = 1; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * JWT parts. |
|
| 49 | - * |
|
| 50 | - * @var string[] |
|
| 51 | - */ |
|
| 52 | - protected $_parts; |
|
| 47 | + /** |
|
| 48 | + * JWT parts. |
|
| 49 | + * |
|
| 50 | + * @var string[] |
|
| 51 | + */ |
|
| 52 | + protected $_parts; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * JWT type. |
|
| 56 | - * |
|
| 57 | - * @var int |
|
| 58 | - */ |
|
| 59 | - protected $_type; |
|
| 54 | + /** |
|
| 55 | + * JWT type. |
|
| 56 | + * |
|
| 57 | + * @var int |
|
| 58 | + */ |
|
| 59 | + protected $_type; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Constructor. |
|
| 63 | - * |
|
| 64 | - * @param string $token JWT string |
|
| 65 | - * |
|
| 66 | - * @throws \UnexpectedValueException |
|
| 67 | - */ |
|
| 68 | - public function __construct(string $token) |
|
| 69 | - { |
|
| 70 | - $this->_parts = explode('.', $token); |
|
| 71 | - switch (count($this->_parts)) { |
|
| 72 | - case 3: |
|
| 73 | - $this->_type = self::TYPE_JWS; |
|
| 74 | - break; |
|
| 75 | - case 5: |
|
| 76 | - $this->_type = self::TYPE_JWE; |
|
| 77 | - break; |
|
| 78 | - default: |
|
| 79 | - throw new \UnexpectedValueException('Not a JWT token.'); |
|
| 80 | - } |
|
| 81 | - } |
|
| 61 | + /** |
|
| 62 | + * Constructor. |
|
| 63 | + * |
|
| 64 | + * @param string $token JWT string |
|
| 65 | + * |
|
| 66 | + * @throws \UnexpectedValueException |
|
| 67 | + */ |
|
| 68 | + public function __construct(string $token) |
|
| 69 | + { |
|
| 70 | + $this->_parts = explode('.', $token); |
|
| 71 | + switch (count($this->_parts)) { |
|
| 72 | + case 3: |
|
| 73 | + $this->_type = self::TYPE_JWS; |
|
| 74 | + break; |
|
| 75 | + case 5: |
|
| 76 | + $this->_type = self::TYPE_JWE; |
|
| 77 | + break; |
|
| 78 | + default: |
|
| 79 | + throw new \UnexpectedValueException('Not a JWT token.'); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Convert JWT to string. |
|
| 85 | - * |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public function __toString(): string |
|
| 89 | - { |
|
| 90 | - return $this->token(); |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * Convert JWT to string. |
|
| 85 | + * |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public function __toString(): string |
|
| 89 | + { |
|
| 90 | + return $this->token(); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Convert claims set to an unsecured JWT. |
|
| 95 | - * |
|
| 96 | - * Unsecured JWT is not signed nor encrypted neither integrity protected, |
|
| 97 | - * and should thus be handled with care! |
|
| 98 | - * |
|
| 99 | - * @see https://tools.ietf.org/html/rfc7519#section-6 |
|
| 100 | - * |
|
| 101 | - * @param Claims $claims Claims set |
|
| 102 | - * @param null|Header $header Optional header |
|
| 103 | - * |
|
| 104 | - * @throws \RuntimeException For generic errors |
|
| 105 | - * |
|
| 106 | - * @return self |
|
| 107 | - */ |
|
| 108 | - public static function unsecuredFromClaims(Claims $claims, |
|
| 109 | - ?Header $header = null): self |
|
| 110 | - { |
|
| 111 | - return self::signedFromClaims($claims, new NoneAlgorithm(), $header); |
|
| 112 | - } |
|
| 93 | + /** |
|
| 94 | + * Convert claims set to an unsecured JWT. |
|
| 95 | + * |
|
| 96 | + * Unsecured JWT is not signed nor encrypted neither integrity protected, |
|
| 97 | + * and should thus be handled with care! |
|
| 98 | + * |
|
| 99 | + * @see https://tools.ietf.org/html/rfc7519#section-6 |
|
| 100 | + * |
|
| 101 | + * @param Claims $claims Claims set |
|
| 102 | + * @param null|Header $header Optional header |
|
| 103 | + * |
|
| 104 | + * @throws \RuntimeException For generic errors |
|
| 105 | + * |
|
| 106 | + * @return self |
|
| 107 | + */ |
|
| 108 | + public static function unsecuredFromClaims(Claims $claims, |
|
| 109 | + ?Header $header = null): self |
|
| 110 | + { |
|
| 111 | + return self::signedFromClaims($claims, new NoneAlgorithm(), $header); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Convert claims set to a signed JWS token. |
|
| 116 | - * |
|
| 117 | - * @param Claims $claims Claims set |
|
| 118 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
| 119 | - * @param null|Header $header Optional header |
|
| 120 | - * |
|
| 121 | - * @throws \RuntimeException For generic errors |
|
| 122 | - * |
|
| 123 | - * @return self |
|
| 124 | - */ |
|
| 125 | - public static function signedFromClaims(Claims $claims, |
|
| 126 | - SignatureAlgorithm $algo, ?Header $header = null): self |
|
| 127 | - { |
|
| 128 | - $payload = $claims->toJSON(); |
|
| 129 | - $jws = JWS::sign($payload, $algo, $header); |
|
| 130 | - return new self($jws->toCompact()); |
|
| 131 | - } |
|
| 114 | + /** |
|
| 115 | + * Convert claims set to a signed JWS token. |
|
| 116 | + * |
|
| 117 | + * @param Claims $claims Claims set |
|
| 118 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
| 119 | + * @param null|Header $header Optional header |
|
| 120 | + * |
|
| 121 | + * @throws \RuntimeException For generic errors |
|
| 122 | + * |
|
| 123 | + * @return self |
|
| 124 | + */ |
|
| 125 | + public static function signedFromClaims(Claims $claims, |
|
| 126 | + SignatureAlgorithm $algo, ?Header $header = null): self |
|
| 127 | + { |
|
| 128 | + $payload = $claims->toJSON(); |
|
| 129 | + $jws = JWS::sign($payload, $algo, $header); |
|
| 130 | + return new self($jws->toCompact()); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Convert claims set to an encrypted JWE token. |
|
| 135 | - * |
|
| 136 | - * @param Claims $claims Claims set |
|
| 137 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 138 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 139 | - * @param null|CompressionAlgorithm $zip_algo Optional compression algorithm |
|
| 140 | - * @param null|Header $header Optional header |
|
| 141 | - * |
|
| 142 | - * @throws \RuntimeException For generic errors |
|
| 143 | - * |
|
| 144 | - * @return self |
|
| 145 | - */ |
|
| 146 | - public static function encryptedFromClaims(Claims $claims, |
|
| 147 | - KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
| 148 | - ?CompressionAlgorithm $zip_algo = null, ?Header $header = null): self |
|
| 149 | - { |
|
| 150 | - $payload = $claims->toJSON(); |
|
| 151 | - $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
| 152 | - return new self($jwe->toCompact()); |
|
| 153 | - } |
|
| 133 | + /** |
|
| 134 | + * Convert claims set to an encrypted JWE token. |
|
| 135 | + * |
|
| 136 | + * @param Claims $claims Claims set |
|
| 137 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 138 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 139 | + * @param null|CompressionAlgorithm $zip_algo Optional compression algorithm |
|
| 140 | + * @param null|Header $header Optional header |
|
| 141 | + * |
|
| 142 | + * @throws \RuntimeException For generic errors |
|
| 143 | + * |
|
| 144 | + * @return self |
|
| 145 | + */ |
|
| 146 | + public static function encryptedFromClaims(Claims $claims, |
|
| 147 | + KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
| 148 | + ?CompressionAlgorithm $zip_algo = null, ?Header $header = null): self |
|
| 149 | + { |
|
| 150 | + $payload = $claims->toJSON(); |
|
| 151 | + $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
| 152 | + return new self($jwe->toCompact()); |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * Get claims from the JWT. |
|
| 157 | - * |
|
| 158 | - * Claims shall be validated according to given validation context. |
|
| 159 | - * Validation context must contain all the necessary keys for the signature |
|
| 160 | - * validation and/or content decryption. |
|
| 161 | - * |
|
| 162 | - * If validation context contains only one key, it shall be used explicitly. |
|
| 163 | - * If multiple keys are provided, they must contain a JWK ID parameter for |
|
| 164 | - * the key identification. |
|
| 165 | - * |
|
| 166 | - * @param ValidationContext $ctx |
|
| 167 | - * |
|
| 168 | - * @throws ValidationException if signature is invalid, or decryption fails, |
|
| 169 | - * or claims validation fails |
|
| 170 | - * @throws \RuntimeException For generic errors |
|
| 171 | - * |
|
| 172 | - * @return Claims |
|
| 173 | - */ |
|
| 174 | - public function claims(ValidationContext $ctx): Claims |
|
| 175 | - { |
|
| 176 | - // check signature or decrypt depending on the JWT type. |
|
| 177 | - if ($this->isJWS()) { |
|
| 178 | - $payload = self::_validatedPayloadFromJWS($this->JWS(), $ctx); |
|
| 179 | - } else { |
|
| 180 | - $payload = self::_validatedPayloadFromJWE($this->JWE(), $ctx); |
|
| 181 | - } |
|
| 182 | - // if JWT contains a nested token |
|
| 183 | - if ($this->isNested()) { |
|
| 184 | - return $this->_claimsFromNestedPayload($payload, $ctx); |
|
| 185 | - } |
|
| 186 | - // decode claims and validate |
|
| 187 | - $claims = Claims::fromJSON($payload); |
|
| 188 | - $ctx->validate($claims); |
|
| 189 | - return $claims; |
|
| 190 | - } |
|
| 155 | + /** |
|
| 156 | + * Get claims from the JWT. |
|
| 157 | + * |
|
| 158 | + * Claims shall be validated according to given validation context. |
|
| 159 | + * Validation context must contain all the necessary keys for the signature |
|
| 160 | + * validation and/or content decryption. |
|
| 161 | + * |
|
| 162 | + * If validation context contains only one key, it shall be used explicitly. |
|
| 163 | + * If multiple keys are provided, they must contain a JWK ID parameter for |
|
| 164 | + * the key identification. |
|
| 165 | + * |
|
| 166 | + * @param ValidationContext $ctx |
|
| 167 | + * |
|
| 168 | + * @throws ValidationException if signature is invalid, or decryption fails, |
|
| 169 | + * or claims validation fails |
|
| 170 | + * @throws \RuntimeException For generic errors |
|
| 171 | + * |
|
| 172 | + * @return Claims |
|
| 173 | + */ |
|
| 174 | + public function claims(ValidationContext $ctx): Claims |
|
| 175 | + { |
|
| 176 | + // check signature or decrypt depending on the JWT type. |
|
| 177 | + if ($this->isJWS()) { |
|
| 178 | + $payload = self::_validatedPayloadFromJWS($this->JWS(), $ctx); |
|
| 179 | + } else { |
|
| 180 | + $payload = self::_validatedPayloadFromJWE($this->JWE(), $ctx); |
|
| 181 | + } |
|
| 182 | + // if JWT contains a nested token |
|
| 183 | + if ($this->isNested()) { |
|
| 184 | + return $this->_claimsFromNestedPayload($payload, $ctx); |
|
| 185 | + } |
|
| 186 | + // decode claims and validate |
|
| 187 | + $claims = Claims::fromJSON($payload); |
|
| 188 | + $ctx->validate($claims); |
|
| 189 | + return $claims; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * Sign self producing a nested JWT. |
|
| 194 | - * |
|
| 195 | - * Note that if JWT is to be signed and encrypted, it should be done in |
|
| 196 | - * sign-then-encrypt order. Please refer to links for security information. |
|
| 197 | - * |
|
| 198 | - * @see https://tools.ietf.org/html/rfc7519#section-11.2 |
|
| 199 | - * |
|
| 200 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
| 201 | - * @param null|Header $header Optional header |
|
| 202 | - * |
|
| 203 | - * @throws \RuntimeException For generic errors |
|
| 204 | - * |
|
| 205 | - * @return self |
|
| 206 | - */ |
|
| 207 | - public function signNested(SignatureAlgorithm $algo, ?Header $header = null): self |
|
| 208 | - { |
|
| 209 | - if (!isset($header)) { |
|
| 210 | - $header = new Header(); |
|
| 211 | - } |
|
| 212 | - // add JWT content type parameter |
|
| 213 | - $header = $header->withParameters( |
|
| 214 | - new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
| 215 | - $jws = JWS::sign($this->token(), $algo, $header); |
|
| 216 | - return new self($jws->toCompact()); |
|
| 217 | - } |
|
| 192 | + /** |
|
| 193 | + * Sign self producing a nested JWT. |
|
| 194 | + * |
|
| 195 | + * Note that if JWT is to be signed and encrypted, it should be done in |
|
| 196 | + * sign-then-encrypt order. Please refer to links for security information. |
|
| 197 | + * |
|
| 198 | + * @see https://tools.ietf.org/html/rfc7519#section-11.2 |
|
| 199 | + * |
|
| 200 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
| 201 | + * @param null|Header $header Optional header |
|
| 202 | + * |
|
| 203 | + * @throws \RuntimeException For generic errors |
|
| 204 | + * |
|
| 205 | + * @return self |
|
| 206 | + */ |
|
| 207 | + public function signNested(SignatureAlgorithm $algo, ?Header $header = null): self |
|
| 208 | + { |
|
| 209 | + if (!isset($header)) { |
|
| 210 | + $header = new Header(); |
|
| 211 | + } |
|
| 212 | + // add JWT content type parameter |
|
| 213 | + $header = $header->withParameters( |
|
| 214 | + new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
| 215 | + $jws = JWS::sign($this->token(), $algo, $header); |
|
| 216 | + return new self($jws->toCompact()); |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * Encrypt self producing a nested JWT. |
|
| 221 | - * |
|
| 222 | - * This JWT should be a JWS, that is, the order of nesting should be |
|
| 223 | - * sign-then-encrypt. |
|
| 224 | - * |
|
| 225 | - * @see https://tools.ietf.org/html/rfc7519#section-11.2 |
|
| 226 | - * |
|
| 227 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 228 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 229 | - * @param null|CompressionAlgorithm $zip_algo Optional compression algorithm |
|
| 230 | - * @param null|Header $header Optional header |
|
| 231 | - * |
|
| 232 | - * @throws \RuntimeException For generic errors |
|
| 233 | - * |
|
| 234 | - * @return self |
|
| 235 | - */ |
|
| 236 | - public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
| 237 | - ContentEncryptionAlgorithm $enc_algo, |
|
| 238 | - ?CompressionAlgorithm $zip_algo = null, ?Header $header = null): self |
|
| 239 | - { |
|
| 240 | - if (!isset($header)) { |
|
| 241 | - $header = new Header(); |
|
| 242 | - } |
|
| 243 | - // add JWT content type parameter |
|
| 244 | - $header = $header->withParameters( |
|
| 245 | - new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
| 246 | - $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
| 247 | - $header); |
|
| 248 | - return new self($jwe->toCompact()); |
|
| 249 | - } |
|
| 219 | + /** |
|
| 220 | + * Encrypt self producing a nested JWT. |
|
| 221 | + * |
|
| 222 | + * This JWT should be a JWS, that is, the order of nesting should be |
|
| 223 | + * sign-then-encrypt. |
|
| 224 | + * |
|
| 225 | + * @see https://tools.ietf.org/html/rfc7519#section-11.2 |
|
| 226 | + * |
|
| 227 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 228 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 229 | + * @param null|CompressionAlgorithm $zip_algo Optional compression algorithm |
|
| 230 | + * @param null|Header $header Optional header |
|
| 231 | + * |
|
| 232 | + * @throws \RuntimeException For generic errors |
|
| 233 | + * |
|
| 234 | + * @return self |
|
| 235 | + */ |
|
| 236 | + public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
| 237 | + ContentEncryptionAlgorithm $enc_algo, |
|
| 238 | + ?CompressionAlgorithm $zip_algo = null, ?Header $header = null): self |
|
| 239 | + { |
|
| 240 | + if (!isset($header)) { |
|
| 241 | + $header = new Header(); |
|
| 242 | + } |
|
| 243 | + // add JWT content type parameter |
|
| 244 | + $header = $header->withParameters( |
|
| 245 | + new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
| 246 | + $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
| 247 | + $header); |
|
| 248 | + return new self($jwe->toCompact()); |
|
| 249 | + } |
|
| 250 | 250 | |
| 251 | - /** |
|
| 252 | - * Whether JWT is a JWS. |
|
| 253 | - * |
|
| 254 | - * @return bool |
|
| 255 | - */ |
|
| 256 | - public function isJWS(): bool |
|
| 257 | - { |
|
| 258 | - return self::TYPE_JWS === $this->_type; |
|
| 259 | - } |
|
| 251 | + /** |
|
| 252 | + * Whether JWT is a JWS. |
|
| 253 | + * |
|
| 254 | + * @return bool |
|
| 255 | + */ |
|
| 256 | + public function isJWS(): bool |
|
| 257 | + { |
|
| 258 | + return self::TYPE_JWS === $this->_type; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Get JWT as a JWS. |
|
| 263 | - * |
|
| 264 | - * @throws \LogicException |
|
| 265 | - * |
|
| 266 | - * @return JWS |
|
| 267 | - */ |
|
| 268 | - public function JWS(): JWS |
|
| 269 | - { |
|
| 270 | - if (!$this->isJWS()) { |
|
| 271 | - throw new \LogicException('Not a JWS.'); |
|
| 272 | - } |
|
| 273 | - return JWS::fromParts($this->_parts); |
|
| 274 | - } |
|
| 261 | + /** |
|
| 262 | + * Get JWT as a JWS. |
|
| 263 | + * |
|
| 264 | + * @throws \LogicException |
|
| 265 | + * |
|
| 266 | + * @return JWS |
|
| 267 | + */ |
|
| 268 | + public function JWS(): JWS |
|
| 269 | + { |
|
| 270 | + if (!$this->isJWS()) { |
|
| 271 | + throw new \LogicException('Not a JWS.'); |
|
| 272 | + } |
|
| 273 | + return JWS::fromParts($this->_parts); |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - /** |
|
| 277 | - * Whether JWT is a JWE. |
|
| 278 | - * |
|
| 279 | - * @return bool |
|
| 280 | - */ |
|
| 281 | - public function isJWE(): bool |
|
| 282 | - { |
|
| 283 | - return self::TYPE_JWE === $this->_type; |
|
| 284 | - } |
|
| 276 | + /** |
|
| 277 | + * Whether JWT is a JWE. |
|
| 278 | + * |
|
| 279 | + * @return bool |
|
| 280 | + */ |
|
| 281 | + public function isJWE(): bool |
|
| 282 | + { |
|
| 283 | + return self::TYPE_JWE === $this->_type; |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * Get JWT as a JWE. |
|
| 288 | - * |
|
| 289 | - * @throws \LogicException |
|
| 290 | - * |
|
| 291 | - * @return JWE |
|
| 292 | - */ |
|
| 293 | - public function JWE(): JWE |
|
| 294 | - { |
|
| 295 | - if (!$this->isJWE()) { |
|
| 296 | - throw new \LogicException('Not a JWE.'); |
|
| 297 | - } |
|
| 298 | - return JWE::fromParts($this->_parts); |
|
| 299 | - } |
|
| 286 | + /** |
|
| 287 | + * Get JWT as a JWE. |
|
| 288 | + * |
|
| 289 | + * @throws \LogicException |
|
| 290 | + * |
|
| 291 | + * @return JWE |
|
| 292 | + */ |
|
| 293 | + public function JWE(): JWE |
|
| 294 | + { |
|
| 295 | + if (!$this->isJWE()) { |
|
| 296 | + throw new \LogicException('Not a JWE.'); |
|
| 297 | + } |
|
| 298 | + return JWE::fromParts($this->_parts); |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * Check whether JWT contains another nested JWT. |
|
| 303 | - * |
|
| 304 | - * @return bool |
|
| 305 | - */ |
|
| 306 | - public function isNested(): bool |
|
| 307 | - { |
|
| 308 | - $header = $this->header(); |
|
| 309 | - if (!$header->hasContentType()) { |
|
| 310 | - return false; |
|
| 311 | - } |
|
| 312 | - $cty = $header->contentType()->value(); |
|
| 313 | - if (ContentTypeParameter::TYPE_JWT !== $cty) { |
|
| 314 | - return false; |
|
| 315 | - } |
|
| 316 | - return true; |
|
| 317 | - } |
|
| 301 | + /** |
|
| 302 | + * Check whether JWT contains another nested JWT. |
|
| 303 | + * |
|
| 304 | + * @return bool |
|
| 305 | + */ |
|
| 306 | + public function isNested(): bool |
|
| 307 | + { |
|
| 308 | + $header = $this->header(); |
|
| 309 | + if (!$header->hasContentType()) { |
|
| 310 | + return false; |
|
| 311 | + } |
|
| 312 | + $cty = $header->contentType()->value(); |
|
| 313 | + if (ContentTypeParameter::TYPE_JWT !== $cty) { |
|
| 314 | + return false; |
|
| 315 | + } |
|
| 316 | + return true; |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | - /** |
|
| 320 | - * Check whether JWT is unsecured, that is, it's neither integrity protected |
|
| 321 | - * nor encrypted. |
|
| 322 | - * |
|
| 323 | - * @return bool |
|
| 324 | - */ |
|
| 325 | - public function isUnsecured(): bool |
|
| 326 | - { |
|
| 327 | - // encrypted JWT shall be considered secure |
|
| 328 | - if ($this->isJWE()) { |
|
| 329 | - return false; |
|
| 330 | - } |
|
| 331 | - // check whether JWS is unsecured |
|
| 332 | - return $this->JWS()->isUnsecured(); |
|
| 333 | - } |
|
| 319 | + /** |
|
| 320 | + * Check whether JWT is unsecured, that is, it's neither integrity protected |
|
| 321 | + * nor encrypted. |
|
| 322 | + * |
|
| 323 | + * @return bool |
|
| 324 | + */ |
|
| 325 | + public function isUnsecured(): bool |
|
| 326 | + { |
|
| 327 | + // encrypted JWT shall be considered secure |
|
| 328 | + if ($this->isJWE()) { |
|
| 329 | + return false; |
|
| 330 | + } |
|
| 331 | + // check whether JWS is unsecured |
|
| 332 | + return $this->JWS()->isUnsecured(); |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | - /** |
|
| 336 | - * Get JWT header. |
|
| 337 | - * |
|
| 338 | - * @return JOSE |
|
| 339 | - */ |
|
| 340 | - public function header(): JOSE |
|
| 341 | - { |
|
| 342 | - $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
| 343 | - return new JOSE($header); |
|
| 344 | - } |
|
| 335 | + /** |
|
| 336 | + * Get JWT header. |
|
| 337 | + * |
|
| 338 | + * @return JOSE |
|
| 339 | + */ |
|
| 340 | + public function header(): JOSE |
|
| 341 | + { |
|
| 342 | + $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
| 343 | + return new JOSE($header); |
|
| 344 | + } |
|
| 345 | 345 | |
| 346 | - /** |
|
| 347 | - * Get JWT as a string. |
|
| 348 | - * |
|
| 349 | - * @return string |
|
| 350 | - */ |
|
| 351 | - public function token(): string |
|
| 352 | - { |
|
| 353 | - return implode('.', $this->_parts); |
|
| 354 | - } |
|
| 346 | + /** |
|
| 347 | + * Get JWT as a string. |
|
| 348 | + * |
|
| 349 | + * @return string |
|
| 350 | + */ |
|
| 351 | + public function token(): string |
|
| 352 | + { |
|
| 353 | + return implode('.', $this->_parts); |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | - /** |
|
| 357 | - * Get claims from a nested payload. |
|
| 358 | - * |
|
| 359 | - * @param string $payload JWT payload |
|
| 360 | - * @param ValidationContext $ctx Validation context |
|
| 361 | - * |
|
| 362 | - * @return Claims |
|
| 363 | - */ |
|
| 364 | - private function _claimsFromNestedPayload(string $payload, |
|
| 365 | - ValidationContext $ctx): Claims |
|
| 366 | - { |
|
| 367 | - $jwt = new JWT($payload); |
|
| 368 | - // if this token secured, allow nested tokens to be unsecured. |
|
| 369 | - if (!$this->isUnsecured()) { |
|
| 370 | - $ctx = $ctx->withUnsecuredAllowed(true); |
|
| 371 | - } |
|
| 372 | - return $jwt->claims($ctx); |
|
| 373 | - } |
|
| 356 | + /** |
|
| 357 | + * Get claims from a nested payload. |
|
| 358 | + * |
|
| 359 | + * @param string $payload JWT payload |
|
| 360 | + * @param ValidationContext $ctx Validation context |
|
| 361 | + * |
|
| 362 | + * @return Claims |
|
| 363 | + */ |
|
| 364 | + private function _claimsFromNestedPayload(string $payload, |
|
| 365 | + ValidationContext $ctx): Claims |
|
| 366 | + { |
|
| 367 | + $jwt = new JWT($payload); |
|
| 368 | + // if this token secured, allow nested tokens to be unsecured. |
|
| 369 | + if (!$this->isUnsecured()) { |
|
| 370 | + $ctx = $ctx->withUnsecuredAllowed(true); |
|
| 371 | + } |
|
| 372 | + return $jwt->claims($ctx); |
|
| 373 | + } |
|
| 374 | 374 | |
| 375 | - /** |
|
| 376 | - * Get validated payload from JWS. |
|
| 377 | - * |
|
| 378 | - * @param JWS $jws JWS |
|
| 379 | - * @param ValidationContext $ctx Validation context |
|
| 380 | - * |
|
| 381 | - * @throws ValidationException If signature validation fails |
|
| 382 | - * |
|
| 383 | - * @return string |
|
| 384 | - */ |
|
| 385 | - private static function _validatedPayloadFromJWS(JWS $jws, |
|
| 386 | - ValidationContext $ctx): string |
|
| 387 | - { |
|
| 388 | - // if JWS is unsecured |
|
| 389 | - if ($jws->isUnsecured()) { |
|
| 390 | - return self::_validatedPayloadFromUnsecuredJWS($jws, $ctx); |
|
| 391 | - } |
|
| 392 | - return self::_validatedPayloadFromSignedJWS($jws, $ctx->keys()); |
|
| 393 | - } |
|
| 375 | + /** |
|
| 376 | + * Get validated payload from JWS. |
|
| 377 | + * |
|
| 378 | + * @param JWS $jws JWS |
|
| 379 | + * @param ValidationContext $ctx Validation context |
|
| 380 | + * |
|
| 381 | + * @throws ValidationException If signature validation fails |
|
| 382 | + * |
|
| 383 | + * @return string |
|
| 384 | + */ |
|
| 385 | + private static function _validatedPayloadFromJWS(JWS $jws, |
|
| 386 | + ValidationContext $ctx): string |
|
| 387 | + { |
|
| 388 | + // if JWS is unsecured |
|
| 389 | + if ($jws->isUnsecured()) { |
|
| 390 | + return self::_validatedPayloadFromUnsecuredJWS($jws, $ctx); |
|
| 391 | + } |
|
| 392 | + return self::_validatedPayloadFromSignedJWS($jws, $ctx->keys()); |
|
| 393 | + } |
|
| 394 | 394 | |
| 395 | - /** |
|
| 396 | - * Get validated payload from an unsecured JWS. |
|
| 397 | - * |
|
| 398 | - * @param JWS $jws JWS |
|
| 399 | - * @param ValidationContext $ctx Validation context |
|
| 400 | - * |
|
| 401 | - * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
|
| 402 | - * token is malformed |
|
| 403 | - * |
|
| 404 | - * @return string |
|
| 405 | - */ |
|
| 406 | - private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
| 407 | - ValidationContext $ctx): string |
|
| 408 | - { |
|
| 409 | - if (!$ctx->isUnsecuredAllowed()) { |
|
| 410 | - throw new ValidationException('Unsecured JWS not allowed.'); |
|
| 411 | - } |
|
| 412 | - if (!$jws->validate(new NoneAlgorithm())) { |
|
| 413 | - throw new ValidationException('Malformed unsecured token.'); |
|
| 414 | - } |
|
| 415 | - return $jws->payload(); |
|
| 416 | - } |
|
| 395 | + /** |
|
| 396 | + * Get validated payload from an unsecured JWS. |
|
| 397 | + * |
|
| 398 | + * @param JWS $jws JWS |
|
| 399 | + * @param ValidationContext $ctx Validation context |
|
| 400 | + * |
|
| 401 | + * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
|
| 402 | + * token is malformed |
|
| 403 | + * |
|
| 404 | + * @return string |
|
| 405 | + */ |
|
| 406 | + private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
| 407 | + ValidationContext $ctx): string |
|
| 408 | + { |
|
| 409 | + if (!$ctx->isUnsecuredAllowed()) { |
|
| 410 | + throw new ValidationException('Unsecured JWS not allowed.'); |
|
| 411 | + } |
|
| 412 | + if (!$jws->validate(new NoneAlgorithm())) { |
|
| 413 | + throw new ValidationException('Malformed unsecured token.'); |
|
| 414 | + } |
|
| 415 | + return $jws->payload(); |
|
| 416 | + } |
|
| 417 | 417 | |
| 418 | - /** |
|
| 419 | - * Get validated payload from a signed JWS. |
|
| 420 | - * |
|
| 421 | - * @param JWS $jws JWS |
|
| 422 | - * @param JWKSet $keys Set of allowed keys for the signature validation |
|
| 423 | - * |
|
| 424 | - * @throws ValidationException If validation fails |
|
| 425 | - * |
|
| 426 | - * @return string |
|
| 427 | - */ |
|
| 428 | - private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys): string |
|
| 429 | - { |
|
| 430 | - try { |
|
| 431 | - // explicitly defined key |
|
| 432 | - if (1 === count($keys)) { |
|
| 433 | - $valid = $jws->validateWithJWK($keys->first()); |
|
| 434 | - } else { |
|
| 435 | - $valid = $jws->validateWithJWKSet($keys); |
|
| 436 | - } |
|
| 437 | - } catch (\RuntimeException $e) { |
|
| 438 | - throw new ValidationException('JWS validation failed.', 0, $e); |
|
| 439 | - } |
|
| 440 | - if (!$valid) { |
|
| 441 | - throw new ValidationException('JWS signature is invalid.'); |
|
| 442 | - } |
|
| 443 | - return $jws->payload(); |
|
| 444 | - } |
|
| 418 | + /** |
|
| 419 | + * Get validated payload from a signed JWS. |
|
| 420 | + * |
|
| 421 | + * @param JWS $jws JWS |
|
| 422 | + * @param JWKSet $keys Set of allowed keys for the signature validation |
|
| 423 | + * |
|
| 424 | + * @throws ValidationException If validation fails |
|
| 425 | + * |
|
| 426 | + * @return string |
|
| 427 | + */ |
|
| 428 | + private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys): string |
|
| 429 | + { |
|
| 430 | + try { |
|
| 431 | + // explicitly defined key |
|
| 432 | + if (1 === count($keys)) { |
|
| 433 | + $valid = $jws->validateWithJWK($keys->first()); |
|
| 434 | + } else { |
|
| 435 | + $valid = $jws->validateWithJWKSet($keys); |
|
| 436 | + } |
|
| 437 | + } catch (\RuntimeException $e) { |
|
| 438 | + throw new ValidationException('JWS validation failed.', 0, $e); |
|
| 439 | + } |
|
| 440 | + if (!$valid) { |
|
| 441 | + throw new ValidationException('JWS signature is invalid.'); |
|
| 442 | + } |
|
| 443 | + return $jws->payload(); |
|
| 444 | + } |
|
| 445 | 445 | |
| 446 | - /** |
|
| 447 | - * Get validated payload from an encrypted JWE. |
|
| 448 | - * |
|
| 449 | - * @param JWE $jwe JWE |
|
| 450 | - * @param ValidationContext $ctx Validation context |
|
| 451 | - * |
|
| 452 | - * @throws ValidationException If decryption fails |
|
| 453 | - * |
|
| 454 | - * @return string |
|
| 455 | - */ |
|
| 456 | - private static function _validatedPayloadFromJWE(JWE $jwe, |
|
| 457 | - ValidationContext $ctx): string |
|
| 458 | - { |
|
| 459 | - try { |
|
| 460 | - $keys = $ctx->keys(); |
|
| 461 | - // explicitly defined key |
|
| 462 | - if (1 === count($keys)) { |
|
| 463 | - return $jwe->decryptWithJWK($keys->first()); |
|
| 464 | - } |
|
| 465 | - return $jwe->decryptWithJWKSet($keys); |
|
| 466 | - } catch (\RuntimeException $e) { |
|
| 467 | - throw new ValidationException('JWE validation failed.', 0, $e); |
|
| 468 | - } |
|
| 469 | - } |
|
| 446 | + /** |
|
| 447 | + * Get validated payload from an encrypted JWE. |
|
| 448 | + * |
|
| 449 | + * @param JWE $jwe JWE |
|
| 450 | + * @param ValidationContext $ctx Validation context |
|
| 451 | + * |
|
| 452 | + * @throws ValidationException If decryption fails |
|
| 453 | + * |
|
| 454 | + * @return string |
|
| 455 | + */ |
|
| 456 | + private static function _validatedPayloadFromJWE(JWE $jwe, |
|
| 457 | + ValidationContext $ctx): string |
|
| 458 | + { |
|
| 459 | + try { |
|
| 460 | + $keys = $ctx->keys(); |
|
| 461 | + // explicitly defined key |
|
| 462 | + if (1 === count($keys)) { |
|
| 463 | + return $jwe->decryptWithJWK($keys->first()); |
|
| 464 | + } |
|
| 465 | + return $jwe->decryptWithJWKSet($keys); |
|
| 466 | + } catch (\RuntimeException $e) { |
|
| 467 | + throw new ValidationException('JWE validation failed.', 0, $e); |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | 470 | } |