@@ -11,16 +11,16 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class ExponentParameter extends JWKParameter |
| 13 | 13 | { |
| 14 | - use Base64UIntValue; |
|
| 14 | + use Base64UIntValue; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $e Exponent in base64urlUInt encoding |
|
| 20 | - */ |
|
| 21 | - public function __construct($e) |
|
| 22 | - { |
|
| 23 | - $this->_validateEncoding($e); |
|
| 24 | - parent::__construct(self::PARAM_EXPONENT, $e); |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $e Exponent in base64urlUInt encoding |
|
| 20 | + */ |
|
| 21 | + public function __construct($e) |
|
| 22 | + { |
|
| 23 | + $this->_validateEncoding($e); |
|
| 24 | + parent::__construct(self::PARAM_EXPONENT, $e); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -12,15 +12,15 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class OtherPrimesInfoParameter extends JWKParameter |
| 14 | 14 | { |
| 15 | - use ArrayParameterValue; |
|
| 15 | + use ArrayParameterValue; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param array[] ...$primes |
|
| 21 | - */ |
|
| 22 | - public function __construct(...$primes) |
|
| 23 | - { |
|
| 24 | - parent::__construct(self::PARAM_OTHER_PRIMES_INFO, $primes); |
|
| 25 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param array[] ...$primes |
|
| 21 | + */ |
|
| 22 | + public function __construct(...$primes) |
|
| 23 | + { |
|
| 24 | + parent::__construct(self::PARAM_OTHER_PRIMES_INFO, $primes); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -11,16 +11,16 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class SecondFactorCRTExponentParameter extends JWKParameter |
| 13 | 13 | { |
| 14 | - use Base64UIntValue; |
|
| 14 | + use Base64UIntValue; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $dq Second factor CRT exponent in base64urlUInt encoding |
|
| 20 | - */ |
|
| 21 | - public function __construct($dq) |
|
| 22 | - { |
|
| 23 | - $this->_validateEncoding($dq); |
|
| 24 | - parent::__construct(self::PARAM_SECOND_FACTOR_CRT_EXPONENT, $dq); |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $dq Second factor CRT exponent in base64urlUInt encoding |
|
| 20 | + */ |
|
| 21 | + public function __construct($dq) |
|
| 22 | + { |
|
| 23 | + $this->_validateEncoding($dq); |
|
| 24 | + parent::__construct(self::PARAM_SECOND_FACTOR_CRT_EXPONENT, $dq); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -12,127 +12,127 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class JWKParameter extends Parameter |
| 14 | 14 | { |
| 15 | - // registered parameter names |
|
| 16 | - const PARAM_KEY_TYPE = "kty"; |
|
| 17 | - const PARAM_PUBLIC_KEY_USE = "use"; |
|
| 18 | - const PARAM_KEY_OPERATIONS = "key_ops"; |
|
| 19 | - const PARAM_ALGORITHM = "alg"; |
|
| 20 | - const PARAM_KEY_ID = "kid"; |
|
| 21 | - const PARAM_X509_URL = "x5u"; |
|
| 22 | - const PARAM_X509_CERTIFICATE_CHAIN = "x5c"; |
|
| 23 | - const PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT = "x5t"; |
|
| 24 | - const PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT = "x5t#S256"; |
|
| 25 | - const PARAM_CURVE = "crv"; |
|
| 26 | - const PARAM_X_COORDINATE = "x"; |
|
| 27 | - const PARAM_Y_COORDINATE = "y"; |
|
| 28 | - const PARAM_ECC_PRIVATE_KEY = "d"; |
|
| 29 | - const PARAM_MODULUS = "n"; |
|
| 30 | - const PARAM_EXPONENT = "e"; |
|
| 31 | - const PARAM_PRIVATE_EXPONENT = "d"; |
|
| 32 | - const PARAM_FIRST_PRIME_FACTOR = "p"; |
|
| 33 | - const PARAM_SECOND_PRIME_FACTOR = "q"; |
|
| 34 | - const PARAM_FIRST_FACTOR_CRT_EXPONENT = "dp"; |
|
| 35 | - const PARAM_SECOND_FACTOR_CRT_EXPONENT = "dq"; |
|
| 36 | - const PARAM_FIRST_CRT_COEFFICIENT = "qi"; |
|
| 37 | - const PARAM_OTHER_PRIMES_INFO = "oth"; |
|
| 38 | - const PARAM_KEY_VALUE = "k"; |
|
| 15 | + // registered parameter names |
|
| 16 | + const PARAM_KEY_TYPE = "kty"; |
|
| 17 | + const PARAM_PUBLIC_KEY_USE = "use"; |
|
| 18 | + const PARAM_KEY_OPERATIONS = "key_ops"; |
|
| 19 | + const PARAM_ALGORITHM = "alg"; |
|
| 20 | + const PARAM_KEY_ID = "kid"; |
|
| 21 | + const PARAM_X509_URL = "x5u"; |
|
| 22 | + const PARAM_X509_CERTIFICATE_CHAIN = "x5c"; |
|
| 23 | + const PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT = "x5t"; |
|
| 24 | + const PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT = "x5t#S256"; |
|
| 25 | + const PARAM_CURVE = "crv"; |
|
| 26 | + const PARAM_X_COORDINATE = "x"; |
|
| 27 | + const PARAM_Y_COORDINATE = "y"; |
|
| 28 | + const PARAM_ECC_PRIVATE_KEY = "d"; |
|
| 29 | + const PARAM_MODULUS = "n"; |
|
| 30 | + const PARAM_EXPONENT = "e"; |
|
| 31 | + const PARAM_PRIVATE_EXPONENT = "d"; |
|
| 32 | + const PARAM_FIRST_PRIME_FACTOR = "p"; |
|
| 33 | + const PARAM_SECOND_PRIME_FACTOR = "q"; |
|
| 34 | + const PARAM_FIRST_FACTOR_CRT_EXPONENT = "dp"; |
|
| 35 | + const PARAM_SECOND_FACTOR_CRT_EXPONENT = "dq"; |
|
| 36 | + const PARAM_FIRST_CRT_COEFFICIENT = "qi"; |
|
| 37 | + const PARAM_OTHER_PRIMES_INFO = "oth"; |
|
| 38 | + const PARAM_KEY_VALUE = "k"; |
|
| 39 | 39 | |
| 40 | - // shorthand aliases for parameter names |
|
| 41 | - const P_KTY = self::PARAM_KEY_TYPE; |
|
| 42 | - const P_USE = self::PARAM_PUBLIC_KEY_USE; |
|
| 43 | - const P_KEY_OPS = self::PARAM_KEY_OPERATIONS; |
|
| 44 | - const P_ALG = self::PARAM_ALGORITHM; |
|
| 45 | - const P_KID = self::PARAM_KEY_ID; |
|
| 46 | - const P_X5U = self::PARAM_X509_URL; |
|
| 47 | - const P_X5C = self::PARAM_X509_CERTIFICATE_CHAIN; |
|
| 48 | - const P_X5T = self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT; |
|
| 49 | - const P_X5TS256 = self::PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT; |
|
| 50 | - const P_CRV = self::PARAM_CURVE; |
|
| 51 | - const P_X = self::PARAM_X_COORDINATE; |
|
| 52 | - const P_Y = self::PARAM_Y_COORDINATE; |
|
| 53 | - const P_ECC_D = self::PARAM_ECC_PRIVATE_KEY; |
|
| 54 | - const P_N = self::PARAM_MODULUS; |
|
| 55 | - const P_E = self::PARAM_EXPONENT; |
|
| 56 | - const P_RSA_D = self::PARAM_PRIVATE_EXPONENT; |
|
| 57 | - const P_P = self::PARAM_FIRST_PRIME_FACTOR; |
|
| 58 | - const P_Q = self::PARAM_SECOND_PRIME_FACTOR; |
|
| 59 | - const P_DP = self::PARAM_FIRST_FACTOR_CRT_EXPONENT; |
|
| 60 | - const P_DQ = self::PARAM_SECOND_FACTOR_CRT_EXPONENT; |
|
| 61 | - const P_QI = self::PARAM_FIRST_CRT_COEFFICIENT; |
|
| 62 | - const P_OTH = self::PARAM_OTHER_PRIMES_INFO; |
|
| 63 | - const P_K = self::PARAM_KEY_VALUE; |
|
| 40 | + // shorthand aliases for parameter names |
|
| 41 | + const P_KTY = self::PARAM_KEY_TYPE; |
|
| 42 | + const P_USE = self::PARAM_PUBLIC_KEY_USE; |
|
| 43 | + const P_KEY_OPS = self::PARAM_KEY_OPERATIONS; |
|
| 44 | + const P_ALG = self::PARAM_ALGORITHM; |
|
| 45 | + const P_KID = self::PARAM_KEY_ID; |
|
| 46 | + const P_X5U = self::PARAM_X509_URL; |
|
| 47 | + const P_X5C = self::PARAM_X509_CERTIFICATE_CHAIN; |
|
| 48 | + const P_X5T = self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT; |
|
| 49 | + const P_X5TS256 = self::PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT; |
|
| 50 | + const P_CRV = self::PARAM_CURVE; |
|
| 51 | + const P_X = self::PARAM_X_COORDINATE; |
|
| 52 | + const P_Y = self::PARAM_Y_COORDINATE; |
|
| 53 | + const P_ECC_D = self::PARAM_ECC_PRIVATE_KEY; |
|
| 54 | + const P_N = self::PARAM_MODULUS; |
|
| 55 | + const P_E = self::PARAM_EXPONENT; |
|
| 56 | + const P_RSA_D = self::PARAM_PRIVATE_EXPONENT; |
|
| 57 | + const P_P = self::PARAM_FIRST_PRIME_FACTOR; |
|
| 58 | + const P_Q = self::PARAM_SECOND_PRIME_FACTOR; |
|
| 59 | + const P_DP = self::PARAM_FIRST_FACTOR_CRT_EXPONENT; |
|
| 60 | + const P_DQ = self::PARAM_SECOND_FACTOR_CRT_EXPONENT; |
|
| 61 | + const P_QI = self::PARAM_FIRST_CRT_COEFFICIENT; |
|
| 62 | + const P_OTH = self::PARAM_OTHER_PRIMES_INFO; |
|
| 63 | + const P_K = self::PARAM_KEY_VALUE; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Mapping from registered JWK parameter name to class name. |
|
| 67 | - * |
|
| 68 | - * Note that ECC private key and RSA private key cannot be mapped since |
|
| 69 | - * they share the same parameter name 'd'. |
|
| 70 | - * |
|
| 71 | - * @internal |
|
| 72 | - * |
|
| 73 | - * @var array |
|
| 74 | - */ |
|
| 75 | - const MAP_NAME_TO_CLASS = array( |
|
| 76 | - /* @formatter:off */ |
|
| 77 | - self::P_KTY => KeyTypeParameter::class, |
|
| 78 | - self::P_USE => PublicKeyUseParameter::class, |
|
| 79 | - self::P_KEY_OPS => KeyOperationsParameter::class, |
|
| 80 | - self::P_ALG => AlgorithmParameter::class, |
|
| 81 | - self::P_KID => KeyIDParameter::class, |
|
| 82 | - self::P_CRV => CurveParameter::class, |
|
| 83 | - self::P_X => XCoordinateParameter::class, |
|
| 84 | - self::P_Y => YCoordinateParameter::class, |
|
| 85 | - self::P_N => ModulusParameter::class, |
|
| 86 | - self::P_E => ExponentParameter::class, |
|
| 87 | - self::P_P => FirstPrimeFactorParameter::class, |
|
| 88 | - self::P_Q => SecondPrimeFactorParameter::class, |
|
| 89 | - self::P_DP => FirstFactorCRTExponentParameter::class, |
|
| 90 | - self::P_DQ => SecondFactorCRTExponentParameter::class, |
|
| 91 | - self::P_QI => FirstCRTCoefficientParameter::class, |
|
| 92 | - self::P_OTH => OtherPrimesInfoParameter::class, |
|
| 93 | - self::P_K => KeyValueParameter::class |
|
| 94 | - /* @formatter:on */ |
|
| 95 | - ); |
|
| 65 | + /** |
|
| 66 | + * Mapping from registered JWK parameter name to class name. |
|
| 67 | + * |
|
| 68 | + * Note that ECC private key and RSA private key cannot be mapped since |
|
| 69 | + * they share the same parameter name 'd'. |
|
| 70 | + * |
|
| 71 | + * @internal |
|
| 72 | + * |
|
| 73 | + * @var array |
|
| 74 | + */ |
|
| 75 | + const MAP_NAME_TO_CLASS = array( |
|
| 76 | + /* @formatter:off */ |
|
| 77 | + self::P_KTY => KeyTypeParameter::class, |
|
| 78 | + self::P_USE => PublicKeyUseParameter::class, |
|
| 79 | + self::P_KEY_OPS => KeyOperationsParameter::class, |
|
| 80 | + self::P_ALG => AlgorithmParameter::class, |
|
| 81 | + self::P_KID => KeyIDParameter::class, |
|
| 82 | + self::P_CRV => CurveParameter::class, |
|
| 83 | + self::P_X => XCoordinateParameter::class, |
|
| 84 | + self::P_Y => YCoordinateParameter::class, |
|
| 85 | + self::P_N => ModulusParameter::class, |
|
| 86 | + self::P_E => ExponentParameter::class, |
|
| 87 | + self::P_P => FirstPrimeFactorParameter::class, |
|
| 88 | + self::P_Q => SecondPrimeFactorParameter::class, |
|
| 89 | + self::P_DP => FirstFactorCRTExponentParameter::class, |
|
| 90 | + self::P_DQ => SecondFactorCRTExponentParameter::class, |
|
| 91 | + self::P_QI => FirstCRTCoefficientParameter::class, |
|
| 92 | + self::P_OTH => OtherPrimesInfoParameter::class, |
|
| 93 | + self::P_K => KeyValueParameter::class |
|
| 94 | + /* @formatter:on */ |
|
| 95 | + ); |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Constructor. |
|
| 99 | - * |
|
| 100 | - * @param string $name Parameter name |
|
| 101 | - * @param mixed $value Parameter value |
|
| 102 | - */ |
|
| 103 | - public function __construct($name, $value) |
|
| 104 | - { |
|
| 105 | - $this->_name = $name; |
|
| 106 | - $this->_value = $value; |
|
| 107 | - } |
|
| 97 | + /** |
|
| 98 | + * Constructor. |
|
| 99 | + * |
|
| 100 | + * @param string $name Parameter name |
|
| 101 | + * @param mixed $value Parameter value |
|
| 102 | + */ |
|
| 103 | + public function __construct($name, $value) |
|
| 104 | + { |
|
| 105 | + $this->_name = $name; |
|
| 106 | + $this->_value = $value; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Initialize from a name and a value. |
|
| 111 | - * |
|
| 112 | - * Returns a parameter specific object if one is implemented. |
|
| 113 | - * |
|
| 114 | - * @param string $name Parameter name |
|
| 115 | - * @param mixed $value Parameter value |
|
| 116 | - * @return self |
|
| 117 | - */ |
|
| 118 | - public static function fromNameAndValue($name, $value) |
|
| 119 | - { |
|
| 120 | - if (array_key_exists($name, self::MAP_NAME_TO_CLASS)) { |
|
| 121 | - $cls = self::MAP_NAME_TO_CLASS[$name]; |
|
| 122 | - return $cls::fromJSONValue($value); |
|
| 123 | - } |
|
| 124 | - return new self($name, $value); |
|
| 125 | - } |
|
| 109 | + /** |
|
| 110 | + * Initialize from a name and a value. |
|
| 111 | + * |
|
| 112 | + * Returns a parameter specific object if one is implemented. |
|
| 113 | + * |
|
| 114 | + * @param string $name Parameter name |
|
| 115 | + * @param mixed $value Parameter value |
|
| 116 | + * @return self |
|
| 117 | + */ |
|
| 118 | + public static function fromNameAndValue($name, $value) |
|
| 119 | + { |
|
| 120 | + if (array_key_exists($name, self::MAP_NAME_TO_CLASS)) { |
|
| 121 | + $cls = self::MAP_NAME_TO_CLASS[$name]; |
|
| 122 | + return $cls::fromJSONValue($value); |
|
| 123 | + } |
|
| 124 | + return new self($name, $value); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Initialize from a JSON value. |
|
| 129 | - * |
|
| 130 | - * @param mixed $value |
|
| 131 | - * @return self |
|
| 132 | - */ |
|
| 133 | - public static function fromJSONValue($value) |
|
| 134 | - { |
|
| 135 | - throw new \BadMethodCallException( |
|
| 136 | - __FUNCTION__ . " must be implemented in a derived class."); |
|
| 137 | - } |
|
| 127 | + /** |
|
| 128 | + * Initialize from a JSON value. |
|
| 129 | + * |
|
| 130 | + * @param mixed $value |
|
| 131 | + * @return self |
|
| 132 | + */ |
|
| 133 | + public static function fromJSONValue($value) |
|
| 134 | + { |
|
| 135 | + throw new \BadMethodCallException( |
|
| 136 | + __FUNCTION__ . " must be implemented in a derived class."); |
|
| 137 | + } |
|
| 138 | 138 | } |
@@ -11,30 +11,30 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class KeyTypeParameter extends JWKParameter |
| 13 | 13 | { |
| 14 | - use StringParameterValue; |
|
| 14 | + use StringParameterValue; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Octet sequence key type. |
|
| 18 | - */ |
|
| 19 | - const TYPE_OCT = "oct"; |
|
| 16 | + /** |
|
| 17 | + * Octet sequence key type. |
|
| 18 | + */ |
|
| 19 | + const TYPE_OCT = "oct"; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * RSA key type. |
|
| 23 | - */ |
|
| 24 | - const TYPE_RSA = "RSA"; |
|
| 21 | + /** |
|
| 22 | + * RSA key type. |
|
| 23 | + */ |
|
| 24 | + const TYPE_RSA = "RSA"; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Elliptic curve key type. |
|
| 28 | - */ |
|
| 29 | - const TYPE_EC = "EC"; |
|
| 26 | + /** |
|
| 27 | + * Elliptic curve key type. |
|
| 28 | + */ |
|
| 29 | + const TYPE_EC = "EC"; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Constructor. |
|
| 33 | - * |
|
| 34 | - * @param string $type Key type |
|
| 35 | - */ |
|
| 36 | - public function __construct($type) |
|
| 37 | - { |
|
| 38 | - parent::__construct(self::PARAM_KEY_TYPE, $type); |
|
| 39 | - } |
|
| 31 | + /** |
|
| 32 | + * Constructor. |
|
| 33 | + * |
|
| 34 | + * @param string $type Key type |
|
| 35 | + */ |
|
| 36 | + public function __construct($type) |
|
| 37 | + { |
|
| 38 | + parent::__construct(self::PARAM_KEY_TYPE, $type); |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -11,16 +11,16 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class PrivateExponentParameter extends JWKParameter |
| 13 | 13 | { |
| 14 | - use Base64UIntValue; |
|
| 14 | + use Base64UIntValue; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $d Private exponent in base64urlUInt encoding |
|
| 20 | - */ |
|
| 21 | - public function __construct($d) |
|
| 22 | - { |
|
| 23 | - $this->_validateEncoding($d); |
|
| 24 | - parent::__construct(self::PARAM_PRIVATE_EXPONENT, $d); |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $d Private exponent in base64urlUInt encoding |
|
| 20 | + */ |
|
| 21 | + public function __construct($d) |
|
| 22 | + { |
|
| 23 | + $this->_validateEncoding($d); |
|
| 24 | + parent::__construct(self::PARAM_PRIVATE_EXPONENT, $d); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -15,39 +15,39 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class PublicKeyJWK extends JWK |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Convert public key to PEM. |
|
| 20 | - * |
|
| 21 | - * @return \Sop\CryptoEncoding\PEM |
|
| 22 | - */ |
|
| 23 | - abstract public function toPEM(); |
|
| 18 | + /** |
|
| 19 | + * Convert public key to PEM. |
|
| 20 | + * |
|
| 21 | + * @return \Sop\CryptoEncoding\PEM |
|
| 22 | + */ |
|
| 23 | + abstract public function toPEM(); |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Initialize from a PublicKey object. |
|
| 27 | - * |
|
| 28 | - * @param PublicKey $pub_key Public key |
|
| 29 | - * @throws \UnexpectedValueException |
|
| 30 | - * @return self |
|
| 31 | - */ |
|
| 32 | - public static function fromPublicKey(PublicKey $pub_key) |
|
| 33 | - { |
|
| 34 | - if ($pub_key instanceof RSAPublicKey) { |
|
| 35 | - return RSAPublicKeyJWK::fromRSAPublicKey($pub_key); |
|
| 36 | - } |
|
| 37 | - if ($pub_key instanceof ECPublicKey) { |
|
| 38 | - return ECPublicKeyJWK::fromECPublicKey($pub_key); |
|
| 39 | - } |
|
| 40 | - throw new \UnexpectedValueException("Unsupported public key."); |
|
| 41 | - } |
|
| 25 | + /** |
|
| 26 | + * Initialize from a PublicKey object. |
|
| 27 | + * |
|
| 28 | + * @param PublicKey $pub_key Public key |
|
| 29 | + * @throws \UnexpectedValueException |
|
| 30 | + * @return self |
|
| 31 | + */ |
|
| 32 | + public static function fromPublicKey(PublicKey $pub_key) |
|
| 33 | + { |
|
| 34 | + if ($pub_key instanceof RSAPublicKey) { |
|
| 35 | + return RSAPublicKeyJWK::fromRSAPublicKey($pub_key); |
|
| 36 | + } |
|
| 37 | + if ($pub_key instanceof ECPublicKey) { |
|
| 38 | + return ECPublicKeyJWK::fromECPublicKey($pub_key); |
|
| 39 | + } |
|
| 40 | + throw new \UnexpectedValueException("Unsupported public key."); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Initialize from a PublicKeyInfo object. |
|
| 45 | - * |
|
| 46 | - * @param PublicKeyInfo $pki Public key info |
|
| 47 | - * @return self |
|
| 48 | - */ |
|
| 49 | - public static function fromPublicKeyInfo(PublicKeyInfo $pki) |
|
| 50 | - { |
|
| 51 | - return self::fromPublicKey($pki->publicKey()); |
|
| 52 | - } |
|
| 43 | + /** |
|
| 44 | + * Initialize from a PublicKeyInfo object. |
|
| 45 | + * |
|
| 46 | + * @param PublicKeyInfo $pki Public key info |
|
| 47 | + * @return self |
|
| 48 | + */ |
|
| 49 | + public static function fromPublicKeyInfo(PublicKeyInfo $pki) |
|
| 50 | + { |
|
| 51 | + return self::fromPublicKey($pki->publicKey()); |
|
| 52 | + } |
|
| 53 | 53 | } |
@@ -15,46 +15,46 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class PrivateKeyJWK extends JWK |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get the public key component of the asymmetric key pair. |
|
| 20 | - * |
|
| 21 | - * @return PublicKeyJWK |
|
| 22 | - */ |
|
| 23 | - abstract public function publicKey(); |
|
| 18 | + /** |
|
| 19 | + * Get the public key component of the asymmetric key pair. |
|
| 20 | + * |
|
| 21 | + * @return PublicKeyJWK |
|
| 22 | + */ |
|
| 23 | + abstract public function publicKey(); |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Convert private key to PEM. |
|
| 27 | - * |
|
| 28 | - * @return \Sop\CryptoEncoding\PEM |
|
| 29 | - */ |
|
| 30 | - abstract public function toPEM(); |
|
| 25 | + /** |
|
| 26 | + * Convert private key to PEM. |
|
| 27 | + * |
|
| 28 | + * @return \Sop\CryptoEncoding\PEM |
|
| 29 | + */ |
|
| 30 | + abstract public function toPEM(); |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Initialize from a PrivateKey object. |
|
| 34 | - * |
|
| 35 | - * @param PrivateKey $priv_key Private key |
|
| 36 | - * @throws \UnexpectedValueException |
|
| 37 | - * @return self |
|
| 38 | - */ |
|
| 39 | - public static function fromPrivateKey(PrivateKey $priv_key) |
|
| 40 | - { |
|
| 41 | - if ($priv_key instanceof RSAPrivateKey) { |
|
| 42 | - return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key); |
|
| 43 | - } |
|
| 44 | - if ($priv_key instanceof ECPrivateKey) { |
|
| 45 | - return ECPrivateKeyJWK::fromECPrivateKey($priv_key); |
|
| 46 | - } |
|
| 47 | - throw new \UnexpectedValueException("Unsupported private key."); |
|
| 48 | - } |
|
| 32 | + /** |
|
| 33 | + * Initialize from a PrivateKey object. |
|
| 34 | + * |
|
| 35 | + * @param PrivateKey $priv_key Private key |
|
| 36 | + * @throws \UnexpectedValueException |
|
| 37 | + * @return self |
|
| 38 | + */ |
|
| 39 | + public static function fromPrivateKey(PrivateKey $priv_key) |
|
| 40 | + { |
|
| 41 | + if ($priv_key instanceof RSAPrivateKey) { |
|
| 42 | + return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key); |
|
| 43 | + } |
|
| 44 | + if ($priv_key instanceof ECPrivateKey) { |
|
| 45 | + return ECPrivateKeyJWK::fromECPrivateKey($priv_key); |
|
| 46 | + } |
|
| 47 | + throw new \UnexpectedValueException("Unsupported private key."); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Initialize from a PrivateKeyInfo object. |
|
| 52 | - * |
|
| 53 | - * @param PrivateKeyInfo $pki PrivateKeyInfo |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function fromPrivateKeyInfo(PrivateKeyInfo $pki) |
|
| 57 | - { |
|
| 58 | - return self::fromPrivateKey($pki->privateKey()); |
|
| 59 | - } |
|
| 50 | + /** |
|
| 51 | + * Initialize from a PrivateKeyInfo object. |
|
| 52 | + * |
|
| 53 | + * @param PrivateKeyInfo $pki PrivateKeyInfo |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function fromPrivateKeyInfo(PrivateKeyInfo $pki) |
|
| 57 | + { |
|
| 58 | + return self::fromPrivateKey($pki->privateKey()); |
|
| 59 | + } |
|
| 60 | 60 | } |
@@ -24,417 +24,417 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class JWT |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * Type identifier for the signed JWT. |
|
| 29 | - * |
|
| 30 | - * @internal |
|
| 31 | - * |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 34 | - const TYPE_JWS = 0; |
|
| 27 | + /** |
|
| 28 | + * Type identifier for the signed JWT. |
|
| 29 | + * |
|
| 30 | + * @internal |
|
| 31 | + * |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | + const TYPE_JWS = 0; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Type identifier for the encrypted JWT. |
|
| 38 | - * |
|
| 39 | - * @internal |
|
| 40 | - * |
|
| 41 | - * @var int |
|
| 42 | - */ |
|
| 43 | - const TYPE_JWE = 1; |
|
| 36 | + /** |
|
| 37 | + * Type identifier for the encrypted JWT. |
|
| 38 | + * |
|
| 39 | + * @internal |
|
| 40 | + * |
|
| 41 | + * @var int |
|
| 42 | + */ |
|
| 43 | + const TYPE_JWE = 1; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * JWT parts. |
|
| 47 | - * |
|
| 48 | - * @var string[] $_parts |
|
| 49 | - */ |
|
| 50 | - protected $_parts; |
|
| 45 | + /** |
|
| 46 | + * JWT parts. |
|
| 47 | + * |
|
| 48 | + * @var string[] $_parts |
|
| 49 | + */ |
|
| 50 | + protected $_parts; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * JWT type. |
|
| 54 | - * |
|
| 55 | - * @var int $_type |
|
| 56 | - */ |
|
| 57 | - protected $_type; |
|
| 52 | + /** |
|
| 53 | + * JWT type. |
|
| 54 | + * |
|
| 55 | + * @var int $_type |
|
| 56 | + */ |
|
| 57 | + protected $_type; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Constructor. |
|
| 61 | - * |
|
| 62 | - * @param string $token JWT string |
|
| 63 | - * @throws \UnexpectedValueException |
|
| 64 | - */ |
|
| 65 | - public function __construct($token) |
|
| 66 | - { |
|
| 67 | - $this->_parts = explode(".", $token); |
|
| 68 | - switch (count($this->_parts)) { |
|
| 69 | - case 3: |
|
| 70 | - $this->_type = self::TYPE_JWS; |
|
| 71 | - break; |
|
| 72 | - case 5: |
|
| 73 | - $this->_type = self::TYPE_JWE; |
|
| 74 | - break; |
|
| 75 | - default: |
|
| 76 | - throw new \UnexpectedValueException("Not a JWT token."); |
|
| 77 | - } |
|
| 78 | - } |
|
| 59 | + /** |
|
| 60 | + * Constructor. |
|
| 61 | + * |
|
| 62 | + * @param string $token JWT string |
|
| 63 | + * @throws \UnexpectedValueException |
|
| 64 | + */ |
|
| 65 | + public function __construct($token) |
|
| 66 | + { |
|
| 67 | + $this->_parts = explode(".", $token); |
|
| 68 | + switch (count($this->_parts)) { |
|
| 69 | + case 3: |
|
| 70 | + $this->_type = self::TYPE_JWS; |
|
| 71 | + break; |
|
| 72 | + case 5: |
|
| 73 | + $this->_type = self::TYPE_JWE; |
|
| 74 | + break; |
|
| 75 | + default: |
|
| 76 | + throw new \UnexpectedValueException("Not a JWT token."); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Convert claims set to an unsecured JWT. |
|
| 82 | - * |
|
| 83 | - * Unsecured JWT is not signed nor encrypted neither integrity protected, |
|
| 84 | - * and should thus be handled with care! |
|
| 85 | - * |
|
| 86 | - * @link https://tools.ietf.org/html/rfc7519#section-6 |
|
| 87 | - * @param Claims $claims Claims set |
|
| 88 | - * @param Header|null $header Optional header |
|
| 89 | - * @throws \RuntimeException For generic errors |
|
| 90 | - * @return self |
|
| 91 | - */ |
|
| 92 | - public static function unsecuredFromClaims(Claims $claims, |
|
| 93 | - Header $header = null) |
|
| 94 | - { |
|
| 95 | - return self::signedFromClaims($claims, new NoneAlgorithm(), $header); |
|
| 96 | - } |
|
| 80 | + /** |
|
| 81 | + * Convert claims set to an unsecured JWT. |
|
| 82 | + * |
|
| 83 | + * Unsecured JWT is not signed nor encrypted neither integrity protected, |
|
| 84 | + * and should thus be handled with care! |
|
| 85 | + * |
|
| 86 | + * @link https://tools.ietf.org/html/rfc7519#section-6 |
|
| 87 | + * @param Claims $claims Claims set |
|
| 88 | + * @param Header|null $header Optional header |
|
| 89 | + * @throws \RuntimeException For generic errors |
|
| 90 | + * @return self |
|
| 91 | + */ |
|
| 92 | + public static function unsecuredFromClaims(Claims $claims, |
|
| 93 | + Header $header = null) |
|
| 94 | + { |
|
| 95 | + return self::signedFromClaims($claims, new NoneAlgorithm(), $header); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Convert claims set to a signed JWS token. |
|
| 100 | - * |
|
| 101 | - * @param Claims $claims Claims set |
|
| 102 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
| 103 | - * @param Header|null $header Optional header |
|
| 104 | - * @throws \RuntimeException For generic errors |
|
| 105 | - * @return self |
|
| 106 | - */ |
|
| 107 | - public static function signedFromClaims(Claims $claims, |
|
| 108 | - SignatureAlgorithm $algo, Header $header = null) |
|
| 109 | - { |
|
| 110 | - $payload = $claims->toJSON(); |
|
| 111 | - $jws = JWS::sign($payload, $algo, $header); |
|
| 112 | - return new self($jws->toCompact()); |
|
| 113 | - } |
|
| 98 | + /** |
|
| 99 | + * Convert claims set to a signed JWS token. |
|
| 100 | + * |
|
| 101 | + * @param Claims $claims Claims set |
|
| 102 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
| 103 | + * @param Header|null $header Optional header |
|
| 104 | + * @throws \RuntimeException For generic errors |
|
| 105 | + * @return self |
|
| 106 | + */ |
|
| 107 | + public static function signedFromClaims(Claims $claims, |
|
| 108 | + SignatureAlgorithm $algo, Header $header = null) |
|
| 109 | + { |
|
| 110 | + $payload = $claims->toJSON(); |
|
| 111 | + $jws = JWS::sign($payload, $algo, $header); |
|
| 112 | + return new self($jws->toCompact()); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Convert claims set to an encrypted JWE token. |
|
| 117 | - * |
|
| 118 | - * @param Claims $claims Claims set |
|
| 119 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 120 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 121 | - * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
| 122 | - * @param Header|null $header Optional header |
|
| 123 | - * @throws \RuntimeException For generic errors |
|
| 124 | - * @return self |
|
| 125 | - */ |
|
| 126 | - public static function encryptedFromClaims(Claims $claims, |
|
| 127 | - KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
| 128 | - CompressionAlgorithm $zip_algo = null, Header $header = null) |
|
| 129 | - { |
|
| 130 | - $payload = $claims->toJSON(); |
|
| 131 | - $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
| 132 | - return new self($jwe->toCompact()); |
|
| 133 | - } |
|
| 115 | + /** |
|
| 116 | + * Convert claims set to an encrypted JWE token. |
|
| 117 | + * |
|
| 118 | + * @param Claims $claims Claims set |
|
| 119 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 120 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 121 | + * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
| 122 | + * @param Header|null $header Optional header |
|
| 123 | + * @throws \RuntimeException For generic errors |
|
| 124 | + * @return self |
|
| 125 | + */ |
|
| 126 | + public static function encryptedFromClaims(Claims $claims, |
|
| 127 | + KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
| 128 | + CompressionAlgorithm $zip_algo = null, Header $header = null) |
|
| 129 | + { |
|
| 130 | + $payload = $claims->toJSON(); |
|
| 131 | + $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
| 132 | + return new self($jwe->toCompact()); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Get claims from the JWT. |
|
| 137 | - * |
|
| 138 | - * Claims shall be validated according to given validation context. |
|
| 139 | - * Validation context must contain all the necessary keys for the signature |
|
| 140 | - * validation and/or content decryption. |
|
| 141 | - * |
|
| 142 | - * If validation context contains only one key, it shall be used explicitly. |
|
| 143 | - * If multiple keys are provided, they must contain a JWK ID parameter for |
|
| 144 | - * the key identification. |
|
| 145 | - * |
|
| 146 | - * @param ValidationContext $ctx |
|
| 147 | - * @throws ValidationException If signature is invalid, or decryption fails, |
|
| 148 | - * or claims validation fails. |
|
| 149 | - * @throws \RuntimeException For generic errors |
|
| 150 | - * @return Claims |
|
| 151 | - */ |
|
| 152 | - public function claims(ValidationContext $ctx) |
|
| 153 | - { |
|
| 154 | - // check signature or decrypt depending on the JWT type. |
|
| 155 | - if ($this->isJWS()) { |
|
| 156 | - $payload = self::_validatedPayloadFromJWS($this->JWS(), $ctx); |
|
| 157 | - } else { |
|
| 158 | - $payload = self::_validatedPayloadFromJWE($this->JWE(), $ctx); |
|
| 159 | - } |
|
| 160 | - // if JWT contains a nested token |
|
| 161 | - if ($this->isNested()) { |
|
| 162 | - return $this->_claimsFromNestedPayload($payload, $ctx); |
|
| 163 | - } |
|
| 164 | - // decode claims and validate |
|
| 165 | - $claims = Claims::fromJSON($payload); |
|
| 166 | - $ctx->validate($claims); |
|
| 167 | - return $claims; |
|
| 168 | - } |
|
| 135 | + /** |
|
| 136 | + * Get claims from the JWT. |
|
| 137 | + * |
|
| 138 | + * Claims shall be validated according to given validation context. |
|
| 139 | + * Validation context must contain all the necessary keys for the signature |
|
| 140 | + * validation and/or content decryption. |
|
| 141 | + * |
|
| 142 | + * If validation context contains only one key, it shall be used explicitly. |
|
| 143 | + * If multiple keys are provided, they must contain a JWK ID parameter for |
|
| 144 | + * the key identification. |
|
| 145 | + * |
|
| 146 | + * @param ValidationContext $ctx |
|
| 147 | + * @throws ValidationException If signature is invalid, or decryption fails, |
|
| 148 | + * or claims validation fails. |
|
| 149 | + * @throws \RuntimeException For generic errors |
|
| 150 | + * @return Claims |
|
| 151 | + */ |
|
| 152 | + public function claims(ValidationContext $ctx) |
|
| 153 | + { |
|
| 154 | + // check signature or decrypt depending on the JWT type. |
|
| 155 | + if ($this->isJWS()) { |
|
| 156 | + $payload = self::_validatedPayloadFromJWS($this->JWS(), $ctx); |
|
| 157 | + } else { |
|
| 158 | + $payload = self::_validatedPayloadFromJWE($this->JWE(), $ctx); |
|
| 159 | + } |
|
| 160 | + // if JWT contains a nested token |
|
| 161 | + if ($this->isNested()) { |
|
| 162 | + return $this->_claimsFromNestedPayload($payload, $ctx); |
|
| 163 | + } |
|
| 164 | + // decode claims and validate |
|
| 165 | + $claims = Claims::fromJSON($payload); |
|
| 166 | + $ctx->validate($claims); |
|
| 167 | + return $claims; |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Sign self producing a nested JWT. |
|
| 172 | - * |
|
| 173 | - * Note that if JWT is to be signed and encrypted, it should be done in |
|
| 174 | - * sign-then-encrypt order. Please refer to links for security information. |
|
| 175 | - * |
|
| 176 | - * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
| 177 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
| 178 | - * @param Header|null $header Optional header |
|
| 179 | - * @throws \RuntimeException For generic errors |
|
| 180 | - * @return self |
|
| 181 | - */ |
|
| 182 | - public function signNested(SignatureAlgorithm $algo, Header $header = null) |
|
| 183 | - { |
|
| 184 | - if (!isset($header)) { |
|
| 185 | - $header = new Header(); |
|
| 186 | - } |
|
| 187 | - // add JWT content type parameter |
|
| 188 | - $header = $header->withParameters( |
|
| 189 | - new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
| 190 | - $jws = JWS::sign($this->token(), $algo, $header); |
|
| 191 | - return new self($jws->toCompact()); |
|
| 192 | - } |
|
| 170 | + /** |
|
| 171 | + * Sign self producing a nested JWT. |
|
| 172 | + * |
|
| 173 | + * Note that if JWT is to be signed and encrypted, it should be done in |
|
| 174 | + * sign-then-encrypt order. Please refer to links for security information. |
|
| 175 | + * |
|
| 176 | + * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
| 177 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
| 178 | + * @param Header|null $header Optional header |
|
| 179 | + * @throws \RuntimeException For generic errors |
|
| 180 | + * @return self |
|
| 181 | + */ |
|
| 182 | + public function signNested(SignatureAlgorithm $algo, Header $header = null) |
|
| 183 | + { |
|
| 184 | + if (!isset($header)) { |
|
| 185 | + $header = new Header(); |
|
| 186 | + } |
|
| 187 | + // add JWT content type parameter |
|
| 188 | + $header = $header->withParameters( |
|
| 189 | + new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
| 190 | + $jws = JWS::sign($this->token(), $algo, $header); |
|
| 191 | + return new self($jws->toCompact()); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Encrypt self producing a nested JWT. |
|
| 196 | - * |
|
| 197 | - * This JWT should be a JWS, that is, the order of nesting should be |
|
| 198 | - * sign-then-encrypt. |
|
| 199 | - * |
|
| 200 | - * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
| 201 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 202 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 203 | - * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
| 204 | - * @param Header|null $header Optional header |
|
| 205 | - * @throws \RuntimeException For generic errors |
|
| 206 | - * @return self |
|
| 207 | - */ |
|
| 208 | - public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
| 209 | - ContentEncryptionAlgorithm $enc_algo, |
|
| 210 | - CompressionAlgorithm $zip_algo = null, Header $header = null) |
|
| 211 | - { |
|
| 212 | - if (!isset($header)) { |
|
| 213 | - $header = new Header(); |
|
| 214 | - } |
|
| 215 | - // add JWT content type parameter |
|
| 216 | - $header = $header->withParameters( |
|
| 217 | - new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
| 218 | - $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
| 219 | - $header); |
|
| 220 | - return new self($jwe->toCompact()); |
|
| 221 | - } |
|
| 194 | + /** |
|
| 195 | + * Encrypt self producing a nested JWT. |
|
| 196 | + * |
|
| 197 | + * This JWT should be a JWS, that is, the order of nesting should be |
|
| 198 | + * sign-then-encrypt. |
|
| 199 | + * |
|
| 200 | + * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
| 201 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 202 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 203 | + * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
| 204 | + * @param Header|null $header Optional header |
|
| 205 | + * @throws \RuntimeException For generic errors |
|
| 206 | + * @return self |
|
| 207 | + */ |
|
| 208 | + public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
| 209 | + ContentEncryptionAlgorithm $enc_algo, |
|
| 210 | + CompressionAlgorithm $zip_algo = null, Header $header = null) |
|
| 211 | + { |
|
| 212 | + if (!isset($header)) { |
|
| 213 | + $header = new Header(); |
|
| 214 | + } |
|
| 215 | + // add JWT content type parameter |
|
| 216 | + $header = $header->withParameters( |
|
| 217 | + new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
| 218 | + $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
| 219 | + $header); |
|
| 220 | + return new self($jwe->toCompact()); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * Whether JWT is a JWS. |
|
| 225 | - * |
|
| 226 | - * @return bool |
|
| 227 | - */ |
|
| 228 | - public function isJWS() |
|
| 229 | - { |
|
| 230 | - return $this->_type == self::TYPE_JWS; |
|
| 231 | - } |
|
| 223 | + /** |
|
| 224 | + * Whether JWT is a JWS. |
|
| 225 | + * |
|
| 226 | + * @return bool |
|
| 227 | + */ |
|
| 228 | + public function isJWS() |
|
| 229 | + { |
|
| 230 | + return $this->_type == self::TYPE_JWS; |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * Get JWT as a JWS. |
|
| 235 | - * |
|
| 236 | - * @throws \LogicException |
|
| 237 | - * @return JWS |
|
| 238 | - */ |
|
| 239 | - public function JWS() |
|
| 240 | - { |
|
| 241 | - if (!$this->isJWS()) { |
|
| 242 | - throw new \LogicException("Not a JWS."); |
|
| 243 | - } |
|
| 244 | - return JWS::fromParts($this->_parts); |
|
| 245 | - } |
|
| 233 | + /** |
|
| 234 | + * Get JWT as a JWS. |
|
| 235 | + * |
|
| 236 | + * @throws \LogicException |
|
| 237 | + * @return JWS |
|
| 238 | + */ |
|
| 239 | + public function JWS() |
|
| 240 | + { |
|
| 241 | + if (!$this->isJWS()) { |
|
| 242 | + throw new \LogicException("Not a JWS."); |
|
| 243 | + } |
|
| 244 | + return JWS::fromParts($this->_parts); |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * Whether JWT is a JWE. |
|
| 249 | - * |
|
| 250 | - * @return bool |
|
| 251 | - */ |
|
| 252 | - public function isJWE() |
|
| 253 | - { |
|
| 254 | - return $this->_type == self::TYPE_JWE; |
|
| 255 | - } |
|
| 247 | + /** |
|
| 248 | + * Whether JWT is a JWE. |
|
| 249 | + * |
|
| 250 | + * @return bool |
|
| 251 | + */ |
|
| 252 | + public function isJWE() |
|
| 253 | + { |
|
| 254 | + return $this->_type == self::TYPE_JWE; |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - /** |
|
| 258 | - * Get JWT as a JWE. |
|
| 259 | - * |
|
| 260 | - * @throws \LogicException |
|
| 261 | - * @return JWE |
|
| 262 | - */ |
|
| 263 | - public function JWE() |
|
| 264 | - { |
|
| 265 | - if (!$this->isJWE()) { |
|
| 266 | - throw new \LogicException("Not a JWE."); |
|
| 267 | - } |
|
| 268 | - return JWE::fromParts($this->_parts); |
|
| 269 | - } |
|
| 257 | + /** |
|
| 258 | + * Get JWT as a JWE. |
|
| 259 | + * |
|
| 260 | + * @throws \LogicException |
|
| 261 | + * @return JWE |
|
| 262 | + */ |
|
| 263 | + public function JWE() |
|
| 264 | + { |
|
| 265 | + if (!$this->isJWE()) { |
|
| 266 | + throw new \LogicException("Not a JWE."); |
|
| 267 | + } |
|
| 268 | + return JWE::fromParts($this->_parts); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Check whether JWT contains another nested JWT. |
|
| 273 | - * |
|
| 274 | - * @return bool |
|
| 275 | - */ |
|
| 276 | - public function isNested() |
|
| 277 | - { |
|
| 278 | - $header = $this->header(); |
|
| 279 | - if (!$header->hasContentType()) { |
|
| 280 | - return false; |
|
| 281 | - } |
|
| 282 | - $cty = $header->contentType()->value(); |
|
| 283 | - if ($cty != ContentTypeParameter::TYPE_JWT) { |
|
| 284 | - return false; |
|
| 285 | - } |
|
| 286 | - return true; |
|
| 287 | - } |
|
| 271 | + /** |
|
| 272 | + * Check whether JWT contains another nested JWT. |
|
| 273 | + * |
|
| 274 | + * @return bool |
|
| 275 | + */ |
|
| 276 | + public function isNested() |
|
| 277 | + { |
|
| 278 | + $header = $this->header(); |
|
| 279 | + if (!$header->hasContentType()) { |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 282 | + $cty = $header->contentType()->value(); |
|
| 283 | + if ($cty != ContentTypeParameter::TYPE_JWT) { |
|
| 284 | + return false; |
|
| 285 | + } |
|
| 286 | + return true; |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * Check whether JWT is unsecured, that is, it's neither integrity protected |
|
| 291 | - * nor encrypted. |
|
| 292 | - * |
|
| 293 | - * @return bool |
|
| 294 | - */ |
|
| 295 | - public function isUnsecured() |
|
| 296 | - { |
|
| 297 | - // encrypted JWT shall be considered secure |
|
| 298 | - if ($this->isJWE()) { |
|
| 299 | - return false; |
|
| 300 | - } |
|
| 301 | - // check whether JWS is unsecured |
|
| 302 | - return $this->JWS()->isUnsecured(); |
|
| 303 | - } |
|
| 289 | + /** |
|
| 290 | + * Check whether JWT is unsecured, that is, it's neither integrity protected |
|
| 291 | + * nor encrypted. |
|
| 292 | + * |
|
| 293 | + * @return bool |
|
| 294 | + */ |
|
| 295 | + public function isUnsecured() |
|
| 296 | + { |
|
| 297 | + // encrypted JWT shall be considered secure |
|
| 298 | + if ($this->isJWE()) { |
|
| 299 | + return false; |
|
| 300 | + } |
|
| 301 | + // check whether JWS is unsecured |
|
| 302 | + return $this->JWS()->isUnsecured(); |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | - /** |
|
| 306 | - * Get JWT header. |
|
| 307 | - * |
|
| 308 | - * @return JOSE |
|
| 309 | - */ |
|
| 310 | - public function header() |
|
| 311 | - { |
|
| 312 | - $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
| 313 | - return new JOSE($header); |
|
| 314 | - } |
|
| 305 | + /** |
|
| 306 | + * Get JWT header. |
|
| 307 | + * |
|
| 308 | + * @return JOSE |
|
| 309 | + */ |
|
| 310 | + public function header() |
|
| 311 | + { |
|
| 312 | + $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
| 313 | + return new JOSE($header); |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - /** |
|
| 317 | - * Get JWT as a string. |
|
| 318 | - * |
|
| 319 | - * @return string |
|
| 320 | - */ |
|
| 321 | - public function token() |
|
| 322 | - { |
|
| 323 | - return implode(".", $this->_parts); |
|
| 324 | - } |
|
| 316 | + /** |
|
| 317 | + * Get JWT as a string. |
|
| 318 | + * |
|
| 319 | + * @return string |
|
| 320 | + */ |
|
| 321 | + public function token() |
|
| 322 | + { |
|
| 323 | + return implode(".", $this->_parts); |
|
| 324 | + } |
|
| 325 | 325 | |
| 326 | - /** |
|
| 327 | - * Get claims from a nested payload. |
|
| 328 | - * |
|
| 329 | - * @param string $payload JWT payload |
|
| 330 | - * @param ValidationContext $ctx Validation context |
|
| 331 | - * @return Claims |
|
| 332 | - */ |
|
| 333 | - private function _claimsFromNestedPayload($payload, ValidationContext $ctx) |
|
| 334 | - { |
|
| 335 | - $jwt = new JWT($payload); |
|
| 336 | - // if this token secured, allow nested tokens to be unsecured. |
|
| 337 | - if (!$this->isUnsecured()) { |
|
| 338 | - $ctx = $ctx->withUnsecuredAllowed(true); |
|
| 339 | - } |
|
| 340 | - return $jwt->claims($ctx); |
|
| 341 | - } |
|
| 326 | + /** |
|
| 327 | + * Get claims from a nested payload. |
|
| 328 | + * |
|
| 329 | + * @param string $payload JWT payload |
|
| 330 | + * @param ValidationContext $ctx Validation context |
|
| 331 | + * @return Claims |
|
| 332 | + */ |
|
| 333 | + private function _claimsFromNestedPayload($payload, ValidationContext $ctx) |
|
| 334 | + { |
|
| 335 | + $jwt = new JWT($payload); |
|
| 336 | + // if this token secured, allow nested tokens to be unsecured. |
|
| 337 | + if (!$this->isUnsecured()) { |
|
| 338 | + $ctx = $ctx->withUnsecuredAllowed(true); |
|
| 339 | + } |
|
| 340 | + return $jwt->claims($ctx); |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - /** |
|
| 344 | - * Get validated payload from JWS. |
|
| 345 | - * |
|
| 346 | - * @param JWS $jws JWS |
|
| 347 | - * @param ValidationContext $ctx Validation context |
|
| 348 | - * @throws ValidationException If signature validation fails |
|
| 349 | - * @return string |
|
| 350 | - */ |
|
| 351 | - private static function _validatedPayloadFromJWS(JWS $jws, |
|
| 352 | - ValidationContext $ctx) |
|
| 353 | - { |
|
| 354 | - // if JWS is unsecured |
|
| 355 | - if ($jws->isUnsecured()) { |
|
| 356 | - return self::_validatedPayloadFromUnsecuredJWS($jws, $ctx); |
|
| 357 | - } |
|
| 358 | - return self::_validatedPayloadFromSignedJWS($jws, $ctx->keys()); |
|
| 359 | - } |
|
| 343 | + /** |
|
| 344 | + * Get validated payload from JWS. |
|
| 345 | + * |
|
| 346 | + * @param JWS $jws JWS |
|
| 347 | + * @param ValidationContext $ctx Validation context |
|
| 348 | + * @throws ValidationException If signature validation fails |
|
| 349 | + * @return string |
|
| 350 | + */ |
|
| 351 | + private static function _validatedPayloadFromJWS(JWS $jws, |
|
| 352 | + ValidationContext $ctx) |
|
| 353 | + { |
|
| 354 | + // if JWS is unsecured |
|
| 355 | + if ($jws->isUnsecured()) { |
|
| 356 | + return self::_validatedPayloadFromUnsecuredJWS($jws, $ctx); |
|
| 357 | + } |
|
| 358 | + return self::_validatedPayloadFromSignedJWS($jws, $ctx->keys()); |
|
| 359 | + } |
|
| 360 | 360 | |
| 361 | - /** |
|
| 362 | - * Get validated payload from an unsecured JWS. |
|
| 363 | - * |
|
| 364 | - * @param JWS $jws JWS |
|
| 365 | - * @param ValidationContext $ctx Validation context |
|
| 366 | - * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
|
| 367 | - * token is malformed |
|
| 368 | - * @return string |
|
| 369 | - */ |
|
| 370 | - private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
| 371 | - ValidationContext $ctx) |
|
| 372 | - { |
|
| 373 | - if (!$ctx->isUnsecuredAllowed()) { |
|
| 374 | - throw new ValidationException("Unsecured JWS not allowed."); |
|
| 375 | - } |
|
| 376 | - if (!$jws->validate(new NoneAlgorithm())) { |
|
| 377 | - throw new ValidationException("Malformed unsecured token."); |
|
| 378 | - } |
|
| 379 | - return $jws->payload(); |
|
| 380 | - } |
|
| 361 | + /** |
|
| 362 | + * Get validated payload from an unsecured JWS. |
|
| 363 | + * |
|
| 364 | + * @param JWS $jws JWS |
|
| 365 | + * @param ValidationContext $ctx Validation context |
|
| 366 | + * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
|
| 367 | + * token is malformed |
|
| 368 | + * @return string |
|
| 369 | + */ |
|
| 370 | + private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
| 371 | + ValidationContext $ctx) |
|
| 372 | + { |
|
| 373 | + if (!$ctx->isUnsecuredAllowed()) { |
|
| 374 | + throw new ValidationException("Unsecured JWS not allowed."); |
|
| 375 | + } |
|
| 376 | + if (!$jws->validate(new NoneAlgorithm())) { |
|
| 377 | + throw new ValidationException("Malformed unsecured token."); |
|
| 378 | + } |
|
| 379 | + return $jws->payload(); |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | - /** |
|
| 383 | - * Get validated payload from a signed JWS. |
|
| 384 | - * |
|
| 385 | - * @param JWS $jws JWS |
|
| 386 | - * @param JWKSet $keys Set of allowed keys for the signature validation |
|
| 387 | - * @throws ValidationException If validation fails |
|
| 388 | - * @return string |
|
| 389 | - */ |
|
| 390 | - private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys) |
|
| 391 | - { |
|
| 392 | - try { |
|
| 393 | - // explicitly defined key |
|
| 394 | - if (1 == count($keys)) { |
|
| 395 | - $valid = $jws->validateWithJWK($keys->first()); |
|
| 396 | - } else { |
|
| 397 | - $valid = $jws->validateWithJWKSet($keys); |
|
| 398 | - } |
|
| 399 | - } catch (\RuntimeException $e) { |
|
| 400 | - throw new ValidationException("JWS validation failed.", null, $e); |
|
| 401 | - } |
|
| 402 | - if (!$valid) { |
|
| 403 | - throw new ValidationException("JWS signature is invalid."); |
|
| 404 | - } |
|
| 405 | - return $jws->payload(); |
|
| 406 | - } |
|
| 382 | + /** |
|
| 383 | + * Get validated payload from a signed JWS. |
|
| 384 | + * |
|
| 385 | + * @param JWS $jws JWS |
|
| 386 | + * @param JWKSet $keys Set of allowed keys for the signature validation |
|
| 387 | + * @throws ValidationException If validation fails |
|
| 388 | + * @return string |
|
| 389 | + */ |
|
| 390 | + private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys) |
|
| 391 | + { |
|
| 392 | + try { |
|
| 393 | + // explicitly defined key |
|
| 394 | + if (1 == count($keys)) { |
|
| 395 | + $valid = $jws->validateWithJWK($keys->first()); |
|
| 396 | + } else { |
|
| 397 | + $valid = $jws->validateWithJWKSet($keys); |
|
| 398 | + } |
|
| 399 | + } catch (\RuntimeException $e) { |
|
| 400 | + throw new ValidationException("JWS validation failed.", null, $e); |
|
| 401 | + } |
|
| 402 | + if (!$valid) { |
|
| 403 | + throw new ValidationException("JWS signature is invalid."); |
|
| 404 | + } |
|
| 405 | + return $jws->payload(); |
|
| 406 | + } |
|
| 407 | 407 | |
| 408 | - /** |
|
| 409 | - * Get validated payload from an encrypted JWE. |
|
| 410 | - * |
|
| 411 | - * @param JWE $jwe JWE |
|
| 412 | - * @param ValidationContext $ctx Validation context |
|
| 413 | - * @throws ValidationException If decryption fails |
|
| 414 | - * @return string |
|
| 415 | - */ |
|
| 416 | - private static function _validatedPayloadFromJWE(JWE $jwe, |
|
| 417 | - ValidationContext $ctx) |
|
| 418 | - { |
|
| 419 | - try { |
|
| 420 | - $keys = $ctx->keys(); |
|
| 421 | - // explicitly defined key |
|
| 422 | - if (1 == count($keys)) { |
|
| 423 | - return $jwe->decryptWithJWK($keys->first()); |
|
| 424 | - } |
|
| 425 | - return $jwe->decryptWithJWKSet($keys); |
|
| 426 | - } catch (\RuntimeException $e) { |
|
| 427 | - throw new ValidationException("JWE validation failed.", null, $e); |
|
| 428 | - } |
|
| 429 | - } |
|
| 408 | + /** |
|
| 409 | + * Get validated payload from an encrypted JWE. |
|
| 410 | + * |
|
| 411 | + * @param JWE $jwe JWE |
|
| 412 | + * @param ValidationContext $ctx Validation context |
|
| 413 | + * @throws ValidationException If decryption fails |
|
| 414 | + * @return string |
|
| 415 | + */ |
|
| 416 | + private static function _validatedPayloadFromJWE(JWE $jwe, |
|
| 417 | + ValidationContext $ctx) |
|
| 418 | + { |
|
| 419 | + try { |
|
| 420 | + $keys = $ctx->keys(); |
|
| 421 | + // explicitly defined key |
|
| 422 | + if (1 == count($keys)) { |
|
| 423 | + return $jwe->decryptWithJWK($keys->first()); |
|
| 424 | + } |
|
| 425 | + return $jwe->decryptWithJWKSet($keys); |
|
| 426 | + } catch (\RuntimeException $e) { |
|
| 427 | + throw new ValidationException("JWE validation failed.", null, $e); |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | - /** |
|
| 432 | - * Convert JWT to string. |
|
| 433 | - * |
|
| 434 | - * @return string |
|
| 435 | - */ |
|
| 436 | - public function __toString() |
|
| 437 | - { |
|
| 438 | - return $this->token(); |
|
| 439 | - } |
|
| 431 | + /** |
|
| 432 | + * Convert JWT to string. |
|
| 433 | + * |
|
| 434 | + * @return string |
|
| 435 | + */ |
|
| 436 | + public function __toString() |
|
| 437 | + { |
|
| 438 | + return $this->token(); |
|
| 439 | + } |
|
| 440 | 440 | } |
@@ -66,14 +66,14 @@ |
||
| 66 | 66 | { |
| 67 | 67 | $this->_parts = explode(".", $token); |
| 68 | 68 | switch (count($this->_parts)) { |
| 69 | - case 3: |
|
| 70 | - $this->_type = self::TYPE_JWS; |
|
| 71 | - break; |
|
| 72 | - case 5: |
|
| 73 | - $this->_type = self::TYPE_JWE; |
|
| 74 | - break; |
|
| 75 | - default: |
|
| 76 | - throw new \UnexpectedValueException("Not a JWT token."); |
|
| 69 | + case 3: |
|
| 70 | + $this->_type = self::TYPE_JWS; |
|
| 71 | + break; |
|
| 72 | + case 5: |
|
| 73 | + $this->_type = self::TYPE_JWE; |
|
| 74 | + break; |
|
| 75 | + default: |
|
| 76 | + throw new \UnexpectedValueException("Not a JWT token."); |
|
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |