@@ -15,89 +15,89 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class SignatureAlgorithmFactory |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Header. |
|
| 20 | - * |
|
| 21 | - * @var Header $_header |
|
| 22 | - */ |
|
| 23 | - protected $_header; |
|
| 18 | + /** |
|
| 19 | + * Header. |
|
| 20 | + * |
|
| 21 | + * @var Header $_header |
|
| 22 | + */ |
|
| 23 | + protected $_header; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Mapping from algorithm name to class name. |
|
| 27 | - * |
|
| 28 | - * @internal |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - const MAP_ALGO_TO_CLASS = array( |
|
| 33 | - /* @formatter:off */ |
|
| 34 | - JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 35 | - JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 36 | - JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 37 | - JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 38 | - JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 39 | - JWA::ALGO_RS512 => RS512Algorithm::class, |
|
| 40 | - JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 41 | - JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 42 | - JWA::ALGO_ES512 => ES512Algorithm::class |
|
| 43 | - /* @formatter:on */ |
|
| 44 | - ); |
|
| 25 | + /** |
|
| 26 | + * Mapping from algorithm name to class name. |
|
| 27 | + * |
|
| 28 | + * @internal |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + const MAP_ALGO_TO_CLASS = array( |
|
| 33 | + /* @formatter:off */ |
|
| 34 | + JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 35 | + JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 36 | + JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 37 | + JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 38 | + JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 39 | + JWA::ALGO_RS512 => RS512Algorithm::class, |
|
| 40 | + JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 41 | + JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 42 | + JWA::ALGO_ES512 => ES512Algorithm::class |
|
| 43 | + /* @formatter:on */ |
|
| 44 | + ); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Constructor. |
|
| 48 | - * |
|
| 49 | - * @param Header $header |
|
| 50 | - */ |
|
| 51 | - public function __construct(Header $header) |
|
| 52 | - { |
|
| 53 | - $this->_header = $header; |
|
| 54 | - } |
|
| 46 | + /** |
|
| 47 | + * Constructor. |
|
| 48 | + * |
|
| 49 | + * @param Header $header |
|
| 50 | + */ |
|
| 51 | + public function __construct(Header $header) |
|
| 52 | + { |
|
| 53 | + $this->_header = $header; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Get signature algorithm by given JWK. |
|
| 58 | - * |
|
| 59 | - * @param JWK $jwk |
|
| 60 | - * @return SignatureAlgorithm |
|
| 61 | - */ |
|
| 62 | - public function algoByKey(JWK $jwk): SignatureAlgorithm |
|
| 63 | - { |
|
| 64 | - $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
| 65 | - $cls = self::_algoClassByName($alg); |
|
| 66 | - return $cls::fromJWK($jwk, $this->_header); |
|
| 67 | - } |
|
| 56 | + /** |
|
| 57 | + * Get signature algorithm by given JWK. |
|
| 58 | + * |
|
| 59 | + * @param JWK $jwk |
|
| 60 | + * @return SignatureAlgorithm |
|
| 61 | + */ |
|
| 62 | + public function algoByKey(JWK $jwk): SignatureAlgorithm |
|
| 63 | + { |
|
| 64 | + $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
| 65 | + $cls = self::_algoClassByName($alg); |
|
| 66 | + return $cls::fromJWK($jwk, $this->_header); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Get signature algorithm using a matching key from given JWK set. |
|
| 71 | - * |
|
| 72 | - * @param JWKSet $set |
|
| 73 | - * @throws \UnexpectedValueException If a key cannot be found |
|
| 74 | - * @return SignatureAlgorithm |
|
| 75 | - */ |
|
| 76 | - public function algoByKeys(JWKSet $set): SignatureAlgorithm |
|
| 77 | - { |
|
| 78 | - if (!$this->_header->hasKeyID()) { |
|
| 79 | - throw new \UnexpectedValueException("No key ID paremeter."); |
|
| 80 | - } |
|
| 81 | - $id = $this->_header->keyID()->value(); |
|
| 82 | - if (!$set->hasKeyID($id)) { |
|
| 83 | - throw new \UnexpectedValueException("No key for ID '$id'."); |
|
| 84 | - } |
|
| 85 | - return $this->algoByKey($set->keyByID($id)); |
|
| 86 | - } |
|
| 69 | + /** |
|
| 70 | + * Get signature algorithm using a matching key from given JWK set. |
|
| 71 | + * |
|
| 72 | + * @param JWKSet $set |
|
| 73 | + * @throws \UnexpectedValueException If a key cannot be found |
|
| 74 | + * @return SignatureAlgorithm |
|
| 75 | + */ |
|
| 76 | + public function algoByKeys(JWKSet $set): SignatureAlgorithm |
|
| 77 | + { |
|
| 78 | + if (!$this->_header->hasKeyID()) { |
|
| 79 | + throw new \UnexpectedValueException("No key ID paremeter."); |
|
| 80 | + } |
|
| 81 | + $id = $this->_header->keyID()->value(); |
|
| 82 | + if (!$set->hasKeyID($id)) { |
|
| 83 | + throw new \UnexpectedValueException("No key for ID '$id'."); |
|
| 84 | + } |
|
| 85 | + return $this->algoByKey($set->keyByID($id)); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get the algorithm implementation class name by an algorithm name. |
|
| 90 | - * |
|
| 91 | - * @param string $alg Algorithm name |
|
| 92 | - * @throws \UnexpectedValueException |
|
| 93 | - * @return string Class name |
|
| 94 | - */ |
|
| 95 | - private static function _algoClassByName(string $alg): string |
|
| 96 | - { |
|
| 97 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 98 | - throw new \UnexpectedValueException( |
|
| 99 | - "Algorithm '$alg' not supported."); |
|
| 100 | - } |
|
| 101 | - return self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 102 | - } |
|
| 88 | + /** |
|
| 89 | + * Get the algorithm implementation class name by an algorithm name. |
|
| 90 | + * |
|
| 91 | + * @param string $alg Algorithm name |
|
| 92 | + * @throws \UnexpectedValueException |
|
| 93 | + * @return string Class name |
|
| 94 | + */ |
|
| 95 | + private static function _algoClassByName(string $alg): string |
|
| 96 | + { |
|
| 97 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 98 | + throw new \UnexpectedValueException( |
|
| 99 | + "Algorithm '$alg' not supported."); |
|
| 100 | + } |
|
| 101 | + return self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 102 | + } |
|
| 103 | 103 | } |
@@ -13,21 +13,21 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RS512Algorithm extends RSASSAPKCS1Algorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - protected function _mdMethod(): string |
|
| 21 | - { |
|
| 22 | - return "sha512WithRSAEncryption"; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + protected function _mdMethod(): string |
|
| 21 | + { |
|
| 22 | + return "sha512WithRSAEncryption"; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function algorithmParamValue(): string |
|
| 30 | - { |
|
| 31 | - return JWA::ALGO_RS512; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function algorithmParamValue(): string |
|
| 30 | + { |
|
| 31 | + return JWA::ALGO_RS512; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -13,21 +13,21 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RS384Algorithm extends RSASSAPKCS1Algorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - protected function _mdMethod(): string |
|
| 21 | - { |
|
| 22 | - return "sha384WithRSAEncryption"; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + protected function _mdMethod(): string |
|
| 21 | + { |
|
| 22 | + return "sha384WithRSAEncryption"; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function algorithmParamValue(): string |
|
| 30 | - { |
|
| 31 | - return JWA::ALGO_RS384; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function algorithmParamValue(): string |
|
| 30 | + { |
|
| 31 | + return JWA::ALGO_RS384; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -18,84 +18,84 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class RSASSAPKCS1Algorithm extends OpenSSLSignatureAlgorithm |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Mapping from algorithm name to class name. |
|
| 23 | - * |
|
| 24 | - * @internal |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - const MAP_ALGO_TO_CLASS = array( |
|
| 29 | - /* @formatter:off */ |
|
| 30 | - JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 31 | - JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 32 | - JWA::ALGO_RS512 => RS512Algorithm::class |
|
| 33 | - /* @formatter:on */ |
|
| 34 | - ); |
|
| 21 | + /** |
|
| 22 | + * Mapping from algorithm name to class name. |
|
| 23 | + * |
|
| 24 | + * @internal |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + const MAP_ALGO_TO_CLASS = array( |
|
| 29 | + /* @formatter:off */ |
|
| 30 | + JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 31 | + JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 32 | + JWA::ALGO_RS512 => RS512Algorithm::class |
|
| 33 | + /* @formatter:on */ |
|
| 34 | + ); |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Constructor. |
|
| 38 | - * |
|
| 39 | - * @param RSAPublicKeyJWK $pub_key |
|
| 40 | - * @param RSAPrivateKeyJWK $priv_key |
|
| 41 | - */ |
|
| 42 | - protected function __construct(RSAPublicKeyJWK $pub_key, |
|
| 43 | - RSAPrivateKeyJWK $priv_key = null) |
|
| 44 | - { |
|
| 45 | - $this->_publicKey = $pub_key; |
|
| 46 | - $this->_privateKey = $priv_key; |
|
| 47 | - } |
|
| 36 | + /** |
|
| 37 | + * Constructor. |
|
| 38 | + * |
|
| 39 | + * @param RSAPublicKeyJWK $pub_key |
|
| 40 | + * @param RSAPrivateKeyJWK $priv_key |
|
| 41 | + */ |
|
| 42 | + protected function __construct(RSAPublicKeyJWK $pub_key, |
|
| 43 | + RSAPrivateKeyJWK $priv_key = null) |
|
| 44 | + { |
|
| 45 | + $this->_publicKey = $pub_key; |
|
| 46 | + $this->_privateKey = $priv_key; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Initialize from a public key. |
|
| 51 | - * |
|
| 52 | - * @param RSAPublicKeyJWK $jwk |
|
| 53 | - * @return self |
|
| 54 | - */ |
|
| 55 | - public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
| 56 | - { |
|
| 57 | - return new static($jwk); |
|
| 58 | - } |
|
| 49 | + /** |
|
| 50 | + * Initialize from a public key. |
|
| 51 | + * |
|
| 52 | + * @param RSAPublicKeyJWK $jwk |
|
| 53 | + * @return self |
|
| 54 | + */ |
|
| 55 | + public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
| 56 | + { |
|
| 57 | + return new static($jwk); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Initialize from a private key. |
|
| 62 | - * |
|
| 63 | - * @param RSAPrivateKeyJWK $jwk |
|
| 64 | - * @return self |
|
| 65 | - */ |
|
| 66 | - public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
| 67 | - { |
|
| 68 | - return new static($jwk->publicKey(), $jwk); |
|
| 69 | - } |
|
| 60 | + /** |
|
| 61 | + * Initialize from a private key. |
|
| 62 | + * |
|
| 63 | + * @param RSAPrivateKeyJWK $jwk |
|
| 64 | + * @return self |
|
| 65 | + */ |
|
| 66 | + public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
| 67 | + { |
|
| 68 | + return new static($jwk->publicKey(), $jwk); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * |
|
| 73 | - * @param JWK $jwk |
|
| 74 | - * @param Header $header |
|
| 75 | - * @throws \UnexpectedValueException |
|
| 76 | - * @return RSASSAPKCS1Algorithm |
|
| 77 | - */ |
|
| 78 | - public static function fromJWK(JWK $jwk, Header $header): self |
|
| 79 | - { |
|
| 80 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 81 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 82 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
| 83 | - } |
|
| 84 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 85 | - if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 86 | - return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
| 87 | - } |
|
| 88 | - return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
| 89 | - } |
|
| 71 | + /** |
|
| 72 | + * |
|
| 73 | + * @param JWK $jwk |
|
| 74 | + * @param Header $header |
|
| 75 | + * @throws \UnexpectedValueException |
|
| 76 | + * @return RSASSAPKCS1Algorithm |
|
| 77 | + */ |
|
| 78 | + public static function fromJWK(JWK $jwk, Header $header): self |
|
| 79 | + { |
|
| 80 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 81 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 82 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
| 83 | + } |
|
| 84 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 85 | + if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 86 | + return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
| 87 | + } |
|
| 88 | + return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * |
|
| 93 | - * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 94 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 95 | - */ |
|
| 96 | - public function headerParameters(): array |
|
| 97 | - { |
|
| 98 | - return array_merge(parent::headerParameters(), |
|
| 99 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 100 | - } |
|
| 91 | + /** |
|
| 92 | + * |
|
| 93 | + * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 94 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 95 | + */ |
|
| 96 | + public function headerParameters(): array |
|
| 97 | + { |
|
| 98 | + return array_merge(parent::headerParameters(), |
|
| 99 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -14,30 +14,30 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ES512Algorithm extends ECDSAAlgorithm |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * |
|
| 19 | - * {@inheritdoc} |
|
| 20 | - */ |
|
| 21 | - protected function _curveName(): string |
|
| 22 | - { |
|
| 23 | - return CurveParameter::CURVE_P521; |
|
| 24 | - } |
|
| 17 | + /** |
|
| 18 | + * |
|
| 19 | + * {@inheritdoc} |
|
| 20 | + */ |
|
| 21 | + protected function _curveName(): string |
|
| 22 | + { |
|
| 23 | + return CurveParameter::CURVE_P521; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _mdMethod(): string |
|
| 31 | - { |
|
| 32 | - return "sha512"; |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _mdMethod(): string |
|
| 31 | + { |
|
| 32 | + return "sha512"; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * |
|
| 37 | - * {@inheritdoc} |
|
| 38 | - */ |
|
| 39 | - public function algorithmParamValue(): string |
|
| 40 | - { |
|
| 41 | - return JWA::ALGO_ES512; |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + */ |
|
| 39 | + public function algorithmParamValue(): string |
|
| 40 | + { |
|
| 41 | + return JWA::ALGO_ES512; |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -21,278 +21,278 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class JWS |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Protected header. |
|
| 26 | - * |
|
| 27 | - * @var Header $_protectedHeader |
|
| 28 | - */ |
|
| 29 | - protected $_protectedHeader; |
|
| 24 | + /** |
|
| 25 | + * Protected header. |
|
| 26 | + * |
|
| 27 | + * @var Header $_protectedHeader |
|
| 28 | + */ |
|
| 29 | + protected $_protectedHeader; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Payload. |
|
| 33 | - * |
|
| 34 | - * @var string $_payload |
|
| 35 | - */ |
|
| 36 | - protected $_payload; |
|
| 31 | + /** |
|
| 32 | + * Payload. |
|
| 33 | + * |
|
| 34 | + * @var string $_payload |
|
| 35 | + */ |
|
| 36 | + protected $_payload; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Input value for the signature computation. |
|
| 40 | - * |
|
| 41 | - * @var string $_signatureInput |
|
| 42 | - */ |
|
| 43 | - protected $_signatureInput; |
|
| 38 | + /** |
|
| 39 | + * Input value for the signature computation. |
|
| 40 | + * |
|
| 41 | + * @var string $_signatureInput |
|
| 42 | + */ |
|
| 43 | + protected $_signatureInput; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Signature. |
|
| 47 | - * |
|
| 48 | - * @var string $_signature |
|
| 49 | - */ |
|
| 50 | - protected $_signature; |
|
| 45 | + /** |
|
| 46 | + * Signature. |
|
| 47 | + * |
|
| 48 | + * @var string $_signature |
|
| 49 | + */ |
|
| 50 | + protected $_signature; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Constructor. |
|
| 54 | - * |
|
| 55 | - * @param Header $protected_header JWS Protected Header |
|
| 56 | - * @param string $payload JWS Payload |
|
| 57 | - * @param string $signature_input Input value for the signature computation |
|
| 58 | - * @param string $signature JWS Signature |
|
| 59 | - */ |
|
| 60 | - protected function __construct(Header $protected_header, string $payload, |
|
| 61 | - string $signature_input, string $signature) |
|
| 62 | - { |
|
| 63 | - $this->_protectedHeader = $protected_header; |
|
| 64 | - $this->_payload = $payload; |
|
| 65 | - $this->_signatureInput = $signature_input; |
|
| 66 | - $this->_signature = $signature; |
|
| 67 | - } |
|
| 52 | + /** |
|
| 53 | + * Constructor. |
|
| 54 | + * |
|
| 55 | + * @param Header $protected_header JWS Protected Header |
|
| 56 | + * @param string $payload JWS Payload |
|
| 57 | + * @param string $signature_input Input value for the signature computation |
|
| 58 | + * @param string $signature JWS Signature |
|
| 59 | + */ |
|
| 60 | + protected function __construct(Header $protected_header, string $payload, |
|
| 61 | + string $signature_input, string $signature) |
|
| 62 | + { |
|
| 63 | + $this->_protectedHeader = $protected_header; |
|
| 64 | + $this->_payload = $payload; |
|
| 65 | + $this->_signatureInput = $signature_input; |
|
| 66 | + $this->_signature = $signature; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Initialize from a compact serialization. |
|
| 71 | - * |
|
| 72 | - * @param string $data |
|
| 73 | - * @return self |
|
| 74 | - */ |
|
| 75 | - public static function fromCompact(string $data): self |
|
| 76 | - { |
|
| 77 | - return self::fromParts(explode(".", $data)); |
|
| 78 | - } |
|
| 69 | + /** |
|
| 70 | + * Initialize from a compact serialization. |
|
| 71 | + * |
|
| 72 | + * @param string $data |
|
| 73 | + * @return self |
|
| 74 | + */ |
|
| 75 | + public static function fromCompact(string $data): self |
|
| 76 | + { |
|
| 77 | + return self::fromParts(explode(".", $data)); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Initialize from the parts of a compact serialization. |
|
| 82 | - * |
|
| 83 | - * @param array $parts |
|
| 84 | - * @throws \UnexpectedValueException |
|
| 85 | - * @return self |
|
| 86 | - */ |
|
| 87 | - public static function fromParts(array $parts): self |
|
| 88 | - { |
|
| 89 | - if (count($parts) != 3) { |
|
| 90 | - throw new \UnexpectedValueException( |
|
| 91 | - "Invalid JWS compact serialization."); |
|
| 92 | - } |
|
| 93 | - $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
| 94 | - $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
| 95 | - $payload = $b64 ? Base64::urlDecode($parts[1]) : $parts[1]; |
|
| 96 | - $signature_input = $parts[0] . "." . $parts[1]; |
|
| 97 | - $signature = Base64::urlDecode($parts[2]); |
|
| 98 | - return new self($header, $payload, $signature_input, $signature); |
|
| 99 | - } |
|
| 80 | + /** |
|
| 81 | + * Initialize from the parts of a compact serialization. |
|
| 82 | + * |
|
| 83 | + * @param array $parts |
|
| 84 | + * @throws \UnexpectedValueException |
|
| 85 | + * @return self |
|
| 86 | + */ |
|
| 87 | + public static function fromParts(array $parts): self |
|
| 88 | + { |
|
| 89 | + if (count($parts) != 3) { |
|
| 90 | + throw new \UnexpectedValueException( |
|
| 91 | + "Invalid JWS compact serialization."); |
|
| 92 | + } |
|
| 93 | + $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
| 94 | + $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
| 95 | + $payload = $b64 ? Base64::urlDecode($parts[1]) : $parts[1]; |
|
| 96 | + $signature_input = $parts[0] . "." . $parts[1]; |
|
| 97 | + $signature = Base64::urlDecode($parts[2]); |
|
| 98 | + return new self($header, $payload, $signature_input, $signature); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Initialize by signing the payload with given algorithm. |
|
| 103 | - * |
|
| 104 | - * @param string $payload JWS Payload |
|
| 105 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
| 106 | - * @param Header|null $header Desired header. Algorithm specific |
|
| 107 | - * parameters are added automatically. |
|
| 108 | - * @throws \RuntimeException If signature computation fails |
|
| 109 | - * @return self |
|
| 110 | - */ |
|
| 111 | - public static function sign(string $payload, SignatureAlgorithm $algo, |
|
| 112 | - Header $header = null): self |
|
| 113 | - { |
|
| 114 | - if (!isset($header)) { |
|
| 115 | - $header = new Header(); |
|
| 116 | - } |
|
| 117 | - $header = $header->withParameters(...$algo->headerParameters()); |
|
| 118 | - // ensure that if b64 parameter is used, it's marked critical |
|
| 119 | - if ($header->hasB64Payload()) { |
|
| 120 | - if (!$header->hasCritical()) { |
|
| 121 | - $crit = new CriticalParameter(JWTParameter::P_B64); |
|
| 122 | - } else { |
|
| 123 | - $crit = $header->critical()->withParamName(JWTParameter::P_B64); |
|
| 124 | - } |
|
| 125 | - $header = $header->withParameters($crit); |
|
| 126 | - } |
|
| 127 | - $signature_input = self::_generateSignatureInput($payload, $header); |
|
| 128 | - $signature = $algo->computeSignature($signature_input); |
|
| 129 | - return new self($header, $payload, $signature_input, $signature); |
|
| 130 | - } |
|
| 101 | + /** |
|
| 102 | + * Initialize by signing the payload with given algorithm. |
|
| 103 | + * |
|
| 104 | + * @param string $payload JWS Payload |
|
| 105 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
| 106 | + * @param Header|null $header Desired header. Algorithm specific |
|
| 107 | + * parameters are added automatically. |
|
| 108 | + * @throws \RuntimeException If signature computation fails |
|
| 109 | + * @return self |
|
| 110 | + */ |
|
| 111 | + public static function sign(string $payload, SignatureAlgorithm $algo, |
|
| 112 | + Header $header = null): self |
|
| 113 | + { |
|
| 114 | + if (!isset($header)) { |
|
| 115 | + $header = new Header(); |
|
| 116 | + } |
|
| 117 | + $header = $header->withParameters(...$algo->headerParameters()); |
|
| 118 | + // ensure that if b64 parameter is used, it's marked critical |
|
| 119 | + if ($header->hasB64Payload()) { |
|
| 120 | + if (!$header->hasCritical()) { |
|
| 121 | + $crit = new CriticalParameter(JWTParameter::P_B64); |
|
| 122 | + } else { |
|
| 123 | + $crit = $header->critical()->withParamName(JWTParameter::P_B64); |
|
| 124 | + } |
|
| 125 | + $header = $header->withParameters($crit); |
|
| 126 | + } |
|
| 127 | + $signature_input = self::_generateSignatureInput($payload, $header); |
|
| 128 | + $signature = $algo->computeSignature($signature_input); |
|
| 129 | + return new self($header, $payload, $signature_input, $signature); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Get JOSE header. |
|
| 134 | - * |
|
| 135 | - * @return JOSE |
|
| 136 | - */ |
|
| 137 | - public function header(): JOSE |
|
| 138 | - { |
|
| 139 | - return new JOSE($this->_protectedHeader); |
|
| 140 | - } |
|
| 132 | + /** |
|
| 133 | + * Get JOSE header. |
|
| 134 | + * |
|
| 135 | + * @return JOSE |
|
| 136 | + */ |
|
| 137 | + public function header(): JOSE |
|
| 138 | + { |
|
| 139 | + return new JOSE($this->_protectedHeader); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Get the signature algorithm name. |
|
| 144 | - * |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - public function algorithmName(): string |
|
| 148 | - { |
|
| 149 | - return $this->header() |
|
| 150 | - ->algorithm() |
|
| 151 | - ->value(); |
|
| 152 | - } |
|
| 142 | + /** |
|
| 143 | + * Get the signature algorithm name. |
|
| 144 | + * |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + public function algorithmName(): string |
|
| 148 | + { |
|
| 149 | + return $this->header() |
|
| 150 | + ->algorithm() |
|
| 151 | + ->value(); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * Check whether JWS is unsecured, that is, contains no signature. |
|
| 156 | - * |
|
| 157 | - * @return bool |
|
| 158 | - */ |
|
| 159 | - public function isUnsecured(): bool |
|
| 160 | - { |
|
| 161 | - return $this->algorithmName() == JWA::ALGO_NONE; |
|
| 162 | - } |
|
| 154 | + /** |
|
| 155 | + * Check whether JWS is unsecured, that is, contains no signature. |
|
| 156 | + * |
|
| 157 | + * @return bool |
|
| 158 | + */ |
|
| 159 | + public function isUnsecured(): bool |
|
| 160 | + { |
|
| 161 | + return $this->algorithmName() == JWA::ALGO_NONE; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Get the payload. |
|
| 166 | - * |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public function payload(): string |
|
| 170 | - { |
|
| 171 | - return $this->_payload; |
|
| 172 | - } |
|
| 164 | + /** |
|
| 165 | + * Get the payload. |
|
| 166 | + * |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public function payload(): string |
|
| 170 | + { |
|
| 171 | + return $this->_payload; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * Get the signature. |
|
| 176 | - * |
|
| 177 | - * @return string |
|
| 178 | - */ |
|
| 179 | - public function signature(): string |
|
| 180 | - { |
|
| 181 | - return $this->_signature; |
|
| 182 | - } |
|
| 174 | + /** |
|
| 175 | + * Get the signature. |
|
| 176 | + * |
|
| 177 | + * @return string |
|
| 178 | + */ |
|
| 179 | + public function signature(): string |
|
| 180 | + { |
|
| 181 | + return $this->_signature; |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Get the payload encoded for serialization. |
|
| 186 | - * |
|
| 187 | - * @return string |
|
| 188 | - */ |
|
| 189 | - protected function _encodedPayload(): string |
|
| 190 | - { |
|
| 191 | - $b64 = true; |
|
| 192 | - if ($this->_protectedHeader->hasB64Payload()) { |
|
| 193 | - $b64 = $this->_protectedHeader->B64Payload()->value(); |
|
| 194 | - } |
|
| 195 | - return $b64 ? Base64::urlEncode($this->_payload) : $this->_payload; |
|
| 196 | - } |
|
| 184 | + /** |
|
| 185 | + * Get the payload encoded for serialization. |
|
| 186 | + * |
|
| 187 | + * @return string |
|
| 188 | + */ |
|
| 189 | + protected function _encodedPayload(): string |
|
| 190 | + { |
|
| 191 | + $b64 = true; |
|
| 192 | + if ($this->_protectedHeader->hasB64Payload()) { |
|
| 193 | + $b64 = $this->_protectedHeader->B64Payload()->value(); |
|
| 194 | + } |
|
| 195 | + return $b64 ? Base64::urlEncode($this->_payload) : $this->_payload; |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * Validate the signature using explicit algorithm. |
|
| 200 | - * |
|
| 201 | - * @param SignatureAlgorithm $algo |
|
| 202 | - * @throws \UnexpectedValueException If using different signature algorithm |
|
| 203 | - * then specified by the header |
|
| 204 | - * @throws \RuntimeException If signature computation fails |
|
| 205 | - * @return bool True if signature is valid |
|
| 206 | - */ |
|
| 207 | - public function validate(SignatureAlgorithm $algo): bool |
|
| 208 | - { |
|
| 209 | - if ($algo->algorithmParamValue() != $this->algorithmName()) { |
|
| 210 | - throw new \UnexpectedValueException("Invalid signature algorithm."); |
|
| 211 | - } |
|
| 212 | - return $algo->validateSignature($this->_signatureInput, |
|
| 213 | - $this->_signature); |
|
| 214 | - } |
|
| 198 | + /** |
|
| 199 | + * Validate the signature using explicit algorithm. |
|
| 200 | + * |
|
| 201 | + * @param SignatureAlgorithm $algo |
|
| 202 | + * @throws \UnexpectedValueException If using different signature algorithm |
|
| 203 | + * then specified by the header |
|
| 204 | + * @throws \RuntimeException If signature computation fails |
|
| 205 | + * @return bool True if signature is valid |
|
| 206 | + */ |
|
| 207 | + public function validate(SignatureAlgorithm $algo): bool |
|
| 208 | + { |
|
| 209 | + if ($algo->algorithmParamValue() != $this->algorithmName()) { |
|
| 210 | + throw new \UnexpectedValueException("Invalid signature algorithm."); |
|
| 211 | + } |
|
| 212 | + return $algo->validateSignature($this->_signatureInput, |
|
| 213 | + $this->_signature); |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - /** |
|
| 217 | - * Validate the signature using the given JWK. |
|
| 218 | - * |
|
| 219 | - * Signature algorithm is determined from the header. |
|
| 220 | - * |
|
| 221 | - * @param JWK $jwk JSON Web Key |
|
| 222 | - * @throws \RuntimeException If algorithm initialization fails |
|
| 223 | - * @return bool True if signature is valid |
|
| 224 | - */ |
|
| 225 | - public function validateWithJWK(JWK $jwk): bool |
|
| 226 | - { |
|
| 227 | - $algo = SignatureAlgorithm::fromJWK($jwk, $this->header()); |
|
| 228 | - return $this->validate($algo); |
|
| 229 | - } |
|
| 216 | + /** |
|
| 217 | + * Validate the signature using the given JWK. |
|
| 218 | + * |
|
| 219 | + * Signature algorithm is determined from the header. |
|
| 220 | + * |
|
| 221 | + * @param JWK $jwk JSON Web Key |
|
| 222 | + * @throws \RuntimeException If algorithm initialization fails |
|
| 223 | + * @return bool True if signature is valid |
|
| 224 | + */ |
|
| 225 | + public function validateWithJWK(JWK $jwk): bool |
|
| 226 | + { |
|
| 227 | + $algo = SignatureAlgorithm::fromJWK($jwk, $this->header()); |
|
| 228 | + return $this->validate($algo); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Validate the signature using a key from the given JWK set. |
|
| 233 | - * |
|
| 234 | - * Correct key shall be sought by the key ID indicated by the header. |
|
| 235 | - * |
|
| 236 | - * @param JWKSet $set Set of JSON Web Keys |
|
| 237 | - * @throws \RuntimeException If algorithm initialization fails |
|
| 238 | - * @return bool True if signature is valid |
|
| 239 | - */ |
|
| 240 | - public function validateWithJWKSet(JWKSet $set): bool |
|
| 241 | - { |
|
| 242 | - if (!count($set)) { |
|
| 243 | - throw new \RuntimeException("No keys."); |
|
| 244 | - } |
|
| 245 | - $factory = new SignatureAlgorithmFactory($this->header()); |
|
| 246 | - $algo = $factory->algoByKeys($set); |
|
| 247 | - return $this->validate($algo); |
|
| 248 | - } |
|
| 231 | + /** |
|
| 232 | + * Validate the signature using a key from the given JWK set. |
|
| 233 | + * |
|
| 234 | + * Correct key shall be sought by the key ID indicated by the header. |
|
| 235 | + * |
|
| 236 | + * @param JWKSet $set Set of JSON Web Keys |
|
| 237 | + * @throws \RuntimeException If algorithm initialization fails |
|
| 238 | + * @return bool True if signature is valid |
|
| 239 | + */ |
|
| 240 | + public function validateWithJWKSet(JWKSet $set): bool |
|
| 241 | + { |
|
| 242 | + if (!count($set)) { |
|
| 243 | + throw new \RuntimeException("No keys."); |
|
| 244 | + } |
|
| 245 | + $factory = new SignatureAlgorithmFactory($this->header()); |
|
| 246 | + $algo = $factory->algoByKeys($set); |
|
| 247 | + return $this->validate($algo); |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Convert to compact serialization. |
|
| 252 | - * |
|
| 253 | - * @return string |
|
| 254 | - */ |
|
| 255 | - public function toCompact(): string |
|
| 256 | - { |
|
| 257 | - return Base64::urlEncode($this->_protectedHeader->toJSON()) . "." . |
|
| 258 | - $this->_encodedPayload() . "." . |
|
| 259 | - Base64::urlEncode($this->_signature); |
|
| 260 | - } |
|
| 250 | + /** |
|
| 251 | + * Convert to compact serialization. |
|
| 252 | + * |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 255 | + public function toCompact(): string |
|
| 256 | + { |
|
| 257 | + return Base64::urlEncode($this->_protectedHeader->toJSON()) . "." . |
|
| 258 | + $this->_encodedPayload() . "." . |
|
| 259 | + Base64::urlEncode($this->_signature); |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - /** |
|
| 263 | - * Convert to compact serialization with payload detached. |
|
| 264 | - * |
|
| 265 | - * @return string |
|
| 266 | - */ |
|
| 267 | - public function toCompactDetached(): string |
|
| 268 | - { |
|
| 269 | - return Base64::urlEncode($this->_protectedHeader->toJSON()) . ".." . |
|
| 270 | - Base64::urlEncode($this->_signature); |
|
| 271 | - } |
|
| 262 | + /** |
|
| 263 | + * Convert to compact serialization with payload detached. |
|
| 264 | + * |
|
| 265 | + * @return string |
|
| 266 | + */ |
|
| 267 | + public function toCompactDetached(): string |
|
| 268 | + { |
|
| 269 | + return Base64::urlEncode($this->_protectedHeader->toJSON()) . ".." . |
|
| 270 | + Base64::urlEncode($this->_signature); |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - /** |
|
| 274 | - * Generate input for the signature computation. |
|
| 275 | - * |
|
| 276 | - * @param string $payload Payload |
|
| 277 | - * @param Header $header Protected header |
|
| 278 | - * @return string |
|
| 279 | - */ |
|
| 280 | - protected static function _generateSignatureInput(string $payload, |
|
| 281 | - Header $header): string |
|
| 282 | - { |
|
| 283 | - $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
| 284 | - $data = Base64::urlEncode($header->toJSON()) . "."; |
|
| 285 | - $data .= $b64 ? Base64::urlEncode($payload) : $payload; |
|
| 286 | - return $data; |
|
| 287 | - } |
|
| 273 | + /** |
|
| 274 | + * Generate input for the signature computation. |
|
| 275 | + * |
|
| 276 | + * @param string $payload Payload |
|
| 277 | + * @param Header $header Protected header |
|
| 278 | + * @return string |
|
| 279 | + */ |
|
| 280 | + protected static function _generateSignatureInput(string $payload, |
|
| 281 | + Header $header): string |
|
| 282 | + { |
|
| 283 | + $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
| 284 | + $data = Base64::urlEncode($header->toJSON()) . "."; |
|
| 285 | + $data .= $b64 ? Base64::urlEncode($payload) : $payload; |
|
| 286 | + return $data; |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * Convert JWS to string. |
|
| 291 | - * |
|
| 292 | - * @return string |
|
| 293 | - */ |
|
| 294 | - public function __toString() |
|
| 295 | - { |
|
| 296 | - return $this->toCompact(); |
|
| 297 | - } |
|
| 289 | + /** |
|
| 290 | + * Convert JWS to string. |
|
| 291 | + * |
|
| 292 | + * @return string |
|
| 293 | + */ |
|
| 294 | + public function __toString() |
|
| 295 | + { |
|
| 296 | + return $this->toCompact(); |
|
| 297 | + } |
|
| 298 | 298 | } |
@@ -15,73 +15,73 @@ |
||
| 15 | 15 | * Base class for algorithms usable for signing and validating JWS's. |
| 16 | 16 | */ |
| 17 | 17 | abstract class SignatureAlgorithm implements |
| 18 | - AlgorithmParameterValue, |
|
| 19 | - HeaderParameters |
|
| 18 | + AlgorithmParameterValue, |
|
| 19 | + HeaderParameters |
|
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * ID of the key used by the algorithm. |
|
| 23 | - * |
|
| 24 | - * If set, KeyID parameter shall be automatically inserted into JWS's |
|
| 25 | - * header. |
|
| 26 | - * |
|
| 27 | - * @var string|null $_keyID |
|
| 28 | - */ |
|
| 29 | - protected $_keyID; |
|
| 21 | + /** |
|
| 22 | + * ID of the key used by the algorithm. |
|
| 23 | + * |
|
| 24 | + * If set, KeyID parameter shall be automatically inserted into JWS's |
|
| 25 | + * header. |
|
| 26 | + * |
|
| 27 | + * @var string|null $_keyID |
|
| 28 | + */ |
|
| 29 | + protected $_keyID; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Compute signature. |
|
| 33 | - * |
|
| 34 | - * @param string $data Data for which the signature is computed |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - abstract public function computeSignature(string $data): string; |
|
| 31 | + /** |
|
| 32 | + * Compute signature. |
|
| 33 | + * |
|
| 34 | + * @param string $data Data for which the signature is computed |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + abstract public function computeSignature(string $data): string; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Validate signature. |
|
| 41 | - * |
|
| 42 | - * @param string $data Data to validate |
|
| 43 | - * @param string $signature Signature to compare |
|
| 44 | - * @return bool |
|
| 45 | - */ |
|
| 46 | - abstract public function validateSignature(string $data, string $signature): bool; |
|
| 39 | + /** |
|
| 40 | + * Validate signature. |
|
| 41 | + * |
|
| 42 | + * @param string $data Data to validate |
|
| 43 | + * @param string $signature Signature to compare |
|
| 44 | + * @return bool |
|
| 45 | + */ |
|
| 46 | + abstract public function validateSignature(string $data, string $signature): bool; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Initialize signature algorithm from a JWK and a header. |
|
| 50 | - * |
|
| 51 | - * @param JWK $jwk JSON Web Key |
|
| 52 | - * @param Header $header Header |
|
| 53 | - * @return SignatureAlgorithm |
|
| 54 | - */ |
|
| 55 | - public static function fromJWK(JWK $jwk, Header $header) |
|
| 56 | - { |
|
| 57 | - $factory = new SignatureAlgorithmFactory($header); |
|
| 58 | - return $factory->algoByKey($jwk); |
|
| 59 | - } |
|
| 48 | + /** |
|
| 49 | + * Initialize signature algorithm from a JWK and a header. |
|
| 50 | + * |
|
| 51 | + * @param JWK $jwk JSON Web Key |
|
| 52 | + * @param Header $header Header |
|
| 53 | + * @return SignatureAlgorithm |
|
| 54 | + */ |
|
| 55 | + public static function fromJWK(JWK $jwk, Header $header) |
|
| 56 | + { |
|
| 57 | + $factory = new SignatureAlgorithmFactory($header); |
|
| 58 | + return $factory->algoByKey($jwk); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Get self with key ID. |
|
| 63 | - * |
|
| 64 | - * @param string|null $id Key ID or null to remove |
|
| 65 | - * @return self |
|
| 66 | - */ |
|
| 67 | - public function withKeyID($id): self |
|
| 68 | - { |
|
| 69 | - $obj = clone $this; |
|
| 70 | - $obj->_keyID = $id; |
|
| 71 | - return $obj; |
|
| 72 | - } |
|
| 61 | + /** |
|
| 62 | + * Get self with key ID. |
|
| 63 | + * |
|
| 64 | + * @param string|null $id Key ID or null to remove |
|
| 65 | + * @return self |
|
| 66 | + */ |
|
| 67 | + public function withKeyID($id): self |
|
| 68 | + { |
|
| 69 | + $obj = clone $this; |
|
| 70 | + $obj->_keyID = $id; |
|
| 71 | + return $obj; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * |
|
| 76 | - * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
|
| 77 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 78 | - */ |
|
| 79 | - public function headerParameters(): array |
|
| 80 | - { |
|
| 81 | - $params = array(); |
|
| 82 | - if (isset($this->_keyID)) { |
|
| 83 | - $params[] = new KeyIDParameter($this->_keyID); |
|
| 84 | - } |
|
| 85 | - return $params; |
|
| 86 | - } |
|
| 74 | + /** |
|
| 75 | + * |
|
| 76 | + * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
|
| 77 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 78 | + */ |
|
| 79 | + public function headerParameters(): array |
|
| 80 | + { |
|
| 81 | + $params = array(); |
|
| 82 | + if (isset($this->_keyID)) { |
|
| 83 | + $params[] = new KeyIDParameter($this->_keyID); |
|
| 84 | + } |
|
| 85 | + return $params; |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -17,219 +17,219 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | abstract class JWA |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * HMAC using SHA-256. |
|
| 22 | - */ |
|
| 23 | - const ALGO_HS256 = "HS256"; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * HMAC using SHA-384. |
|
| 27 | - */ |
|
| 28 | - const ALGO_HS384 = "HS384"; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * HMAC using SHA-512. |
|
| 32 | - */ |
|
| 33 | - const ALGO_HS512 = "HS512"; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * RSASSA-PKCS1-v1_5 using SHA-256. |
|
| 37 | - */ |
|
| 38 | - const ALGO_RS256 = "RS256"; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * RSASSA-PKCS1-v1_5 using SHA-384. |
|
| 42 | - */ |
|
| 43 | - const ALGO_RS384 = "RS384"; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * RSASSA-PKCS1-v1_5 using SHA-512. |
|
| 47 | - */ |
|
| 48 | - const ALGO_RS512 = "RS512"; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * ECDSA using P-256 and SHA-256. |
|
| 52 | - */ |
|
| 53 | - const ALGO_ES256 = "ES256"; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * ECDSA using P-384 and SHA-384. |
|
| 57 | - */ |
|
| 58 | - const ALGO_ES384 = "ES384"; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * ECDSA using P-521 and SHA-512. |
|
| 62 | - */ |
|
| 63 | - const ALGO_ES512 = "ES512"; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * RSASSA-PSS using SHA-256 and MGF1 with SHA-256. |
|
| 67 | - */ |
|
| 68 | - const ALGO_PS256 = "PS256"; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * RSASSA-PSS using SHA-384 and MGF1 with SHA-384. |
|
| 72 | - */ |
|
| 73 | - const ALGO_PS384 = "PS384"; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * RSASSA-PSS using SHA-512 and MGF1 with SHA-512. |
|
| 77 | - */ |
|
| 78 | - const ALGO_PS512 = "PS512"; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * No digital signature or MAC performed. |
|
| 82 | - */ |
|
| 83 | - const ALGO_NONE = "none"; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * RSAES-PKCS1-v1_5. |
|
| 87 | - */ |
|
| 88 | - const ALGO_RSA1_5 = "RSA1_5"; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * RSAES OAEP using default parameters. |
|
| 92 | - */ |
|
| 93 | - const ALGO_RSA_OAEP = "RSA-OAEP"; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * RSAES OAEP using SHA-256 and MGF1 with SHA-256. |
|
| 97 | - */ |
|
| 98 | - const ALGO_RSA_OAEP256 = "RSA-OAEP-256"; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * AES Key Wrap using 128-bit key. |
|
| 102 | - */ |
|
| 103 | - const ALGO_A128KW = "A128KW"; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * AES Key Wrap using 192-bit key. |
|
| 107 | - */ |
|
| 108 | - const ALGO_A192KW = "A192KW"; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * AES Key Wrap using 256-bit key. |
|
| 112 | - */ |
|
| 113 | - const ALGO_A256KW = "A256KW"; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Direct use of a shared symmetric key. |
|
| 117 | - */ |
|
| 118 | - const ALGO_DIR = "dir"; |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * ECDH-ES using Concat KDF. |
|
| 122 | - */ |
|
| 123 | - const ALGO_ECDH_ES = "ECDH-ES"; |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * ECDH-ES using Concat KDF and "A128KW" wrapping. |
|
| 127 | - */ |
|
| 128 | - const ALGO_ECDH_ES_A128KW = "ECDH-ES+A128KW"; |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * ECDH-ES using Concat KDF and "A192KW" wrapping. |
|
| 132 | - */ |
|
| 133 | - const ALGO_ECDH_ES_A192KW = "ECDH-ES+A192KW"; |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * ECDH-ES using Concat KDF and "A256KW" wrapping. |
|
| 137 | - */ |
|
| 138 | - const ALGO_ECDH_ES_A256KW = "ECDH-ES+A256KW"; |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Key wrapping with AES GCM using 128-bit key. |
|
| 142 | - */ |
|
| 143 | - const ALGO_A128GCMKW = "A128GCMKW"; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Key wrapping with AES GCM using 192-bit key. |
|
| 147 | - */ |
|
| 148 | - const ALGO_A192GCMKW = "A192GCMKW"; |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Key wrapping with AES GCM using 256-bit key. |
|
| 152 | - */ |
|
| 153 | - const ALGO_A256GCMKW = "A256GCMKW"; |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * PBES2 with HMAC SHA-256 and "A128KW" wrapping. |
|
| 157 | - */ |
|
| 158 | - const ALGO_PBES2_HS256_A128KW = "PBES2-HS256+A128KW"; |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * PBES2 with HMAC SHA-384 and "A192KW" wrapping. |
|
| 162 | - */ |
|
| 163 | - const ALGO_PBES2_HS384_A192KW = "PBES2-HS384+A192KW"; |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * PBES2 with HMAC SHA-512 and "A256KW" wrapping. |
|
| 167 | - */ |
|
| 168 | - const ALGO_PBES2_HS512_A256KW = "PBES2-HS512+A256KW"; |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm. |
|
| 172 | - */ |
|
| 173 | - const ALGO_A128CBC_HS256 = "A128CBC-HS256"; |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm. |
|
| 177 | - */ |
|
| 178 | - const ALGO_A192CBC_HS384 = "A192CBC-HS384"; |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm. |
|
| 182 | - */ |
|
| 183 | - const ALGO_A256CBC_HS512 = "A256CBC-HS512"; |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * AES GCM using 128-bit key. |
|
| 187 | - */ |
|
| 188 | - const ALGO_A128GCM = "A128GCM"; |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * AES GCM using 192-bit key. |
|
| 192 | - */ |
|
| 193 | - const ALGO_A192GCM = "A192GCM"; |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * AES GCM using 256-bit key. |
|
| 197 | - */ |
|
| 198 | - const ALGO_A256GCM = "A256GCM"; |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * DEFLATE compression. |
|
| 202 | - */ |
|
| 203 | - const ALGO_DEFLATE = "DEF"; |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Derive algorithm name from the header and optionally from the given JWK. |
|
| 207 | - * |
|
| 208 | - * @param Header $header Header |
|
| 209 | - * @param JWK $jwk Optional JWK |
|
| 210 | - * @throws \UnexpectedValueException If algorithm parameter is not present |
|
| 211 | - * or header and JWK algorithms differ. |
|
| 212 | - * @return string Algorithm name |
|
| 213 | - */ |
|
| 214 | - public static function deriveAlgorithmName(Header $header, JWK $jwk = null): string |
|
| 215 | - { |
|
| 216 | - if ($header->hasAlgorithm()) { |
|
| 217 | - $alg = $header->algorithm()->value(); |
|
| 218 | - } |
|
| 219 | - // if JWK is set, and has an algorithm parameter |
|
| 220 | - if (isset($jwk) && $jwk->hasAlgorithmParameter()) { |
|
| 221 | - $jwk_alg = $jwk->algorithmParameter()->value(); |
|
| 222 | - // check that algorithms match |
|
| 223 | - if (isset($alg) && $alg != $jwk_alg) { |
|
| 224 | - throw new \UnexpectedValueException( |
|
| 225 | - "JWK algorithm '$jwk_alg' doesn't match" . |
|
| 226 | - " the header's algorithm '$alg'."); |
|
| 227 | - } |
|
| 228 | - $alg = $jwk_alg; |
|
| 229 | - } |
|
| 230 | - if (!isset($alg)) { |
|
| 231 | - throw new \UnexpectedValueException("No algorithm parameter."); |
|
| 232 | - } |
|
| 233 | - return $alg; |
|
| 234 | - } |
|
| 20 | + /** |
|
| 21 | + * HMAC using SHA-256. |
|
| 22 | + */ |
|
| 23 | + const ALGO_HS256 = "HS256"; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * HMAC using SHA-384. |
|
| 27 | + */ |
|
| 28 | + const ALGO_HS384 = "HS384"; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * HMAC using SHA-512. |
|
| 32 | + */ |
|
| 33 | + const ALGO_HS512 = "HS512"; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * RSASSA-PKCS1-v1_5 using SHA-256. |
|
| 37 | + */ |
|
| 38 | + const ALGO_RS256 = "RS256"; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * RSASSA-PKCS1-v1_5 using SHA-384. |
|
| 42 | + */ |
|
| 43 | + const ALGO_RS384 = "RS384"; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * RSASSA-PKCS1-v1_5 using SHA-512. |
|
| 47 | + */ |
|
| 48 | + const ALGO_RS512 = "RS512"; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * ECDSA using P-256 and SHA-256. |
|
| 52 | + */ |
|
| 53 | + const ALGO_ES256 = "ES256"; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * ECDSA using P-384 and SHA-384. |
|
| 57 | + */ |
|
| 58 | + const ALGO_ES384 = "ES384"; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * ECDSA using P-521 and SHA-512. |
|
| 62 | + */ |
|
| 63 | + const ALGO_ES512 = "ES512"; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * RSASSA-PSS using SHA-256 and MGF1 with SHA-256. |
|
| 67 | + */ |
|
| 68 | + const ALGO_PS256 = "PS256"; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * RSASSA-PSS using SHA-384 and MGF1 with SHA-384. |
|
| 72 | + */ |
|
| 73 | + const ALGO_PS384 = "PS384"; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * RSASSA-PSS using SHA-512 and MGF1 with SHA-512. |
|
| 77 | + */ |
|
| 78 | + const ALGO_PS512 = "PS512"; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * No digital signature or MAC performed. |
|
| 82 | + */ |
|
| 83 | + const ALGO_NONE = "none"; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * RSAES-PKCS1-v1_5. |
|
| 87 | + */ |
|
| 88 | + const ALGO_RSA1_5 = "RSA1_5"; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * RSAES OAEP using default parameters. |
|
| 92 | + */ |
|
| 93 | + const ALGO_RSA_OAEP = "RSA-OAEP"; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * RSAES OAEP using SHA-256 and MGF1 with SHA-256. |
|
| 97 | + */ |
|
| 98 | + const ALGO_RSA_OAEP256 = "RSA-OAEP-256"; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * AES Key Wrap using 128-bit key. |
|
| 102 | + */ |
|
| 103 | + const ALGO_A128KW = "A128KW"; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * AES Key Wrap using 192-bit key. |
|
| 107 | + */ |
|
| 108 | + const ALGO_A192KW = "A192KW"; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * AES Key Wrap using 256-bit key. |
|
| 112 | + */ |
|
| 113 | + const ALGO_A256KW = "A256KW"; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Direct use of a shared symmetric key. |
|
| 117 | + */ |
|
| 118 | + const ALGO_DIR = "dir"; |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * ECDH-ES using Concat KDF. |
|
| 122 | + */ |
|
| 123 | + const ALGO_ECDH_ES = "ECDH-ES"; |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * ECDH-ES using Concat KDF and "A128KW" wrapping. |
|
| 127 | + */ |
|
| 128 | + const ALGO_ECDH_ES_A128KW = "ECDH-ES+A128KW"; |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * ECDH-ES using Concat KDF and "A192KW" wrapping. |
|
| 132 | + */ |
|
| 133 | + const ALGO_ECDH_ES_A192KW = "ECDH-ES+A192KW"; |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * ECDH-ES using Concat KDF and "A256KW" wrapping. |
|
| 137 | + */ |
|
| 138 | + const ALGO_ECDH_ES_A256KW = "ECDH-ES+A256KW"; |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Key wrapping with AES GCM using 128-bit key. |
|
| 142 | + */ |
|
| 143 | + const ALGO_A128GCMKW = "A128GCMKW"; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Key wrapping with AES GCM using 192-bit key. |
|
| 147 | + */ |
|
| 148 | + const ALGO_A192GCMKW = "A192GCMKW"; |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Key wrapping with AES GCM using 256-bit key. |
|
| 152 | + */ |
|
| 153 | + const ALGO_A256GCMKW = "A256GCMKW"; |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * PBES2 with HMAC SHA-256 and "A128KW" wrapping. |
|
| 157 | + */ |
|
| 158 | + const ALGO_PBES2_HS256_A128KW = "PBES2-HS256+A128KW"; |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * PBES2 with HMAC SHA-384 and "A192KW" wrapping. |
|
| 162 | + */ |
|
| 163 | + const ALGO_PBES2_HS384_A192KW = "PBES2-HS384+A192KW"; |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * PBES2 with HMAC SHA-512 and "A256KW" wrapping. |
|
| 167 | + */ |
|
| 168 | + const ALGO_PBES2_HS512_A256KW = "PBES2-HS512+A256KW"; |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm. |
|
| 172 | + */ |
|
| 173 | + const ALGO_A128CBC_HS256 = "A128CBC-HS256"; |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm. |
|
| 177 | + */ |
|
| 178 | + const ALGO_A192CBC_HS384 = "A192CBC-HS384"; |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm. |
|
| 182 | + */ |
|
| 183 | + const ALGO_A256CBC_HS512 = "A256CBC-HS512"; |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * AES GCM using 128-bit key. |
|
| 187 | + */ |
|
| 188 | + const ALGO_A128GCM = "A128GCM"; |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * AES GCM using 192-bit key. |
|
| 192 | + */ |
|
| 193 | + const ALGO_A192GCM = "A192GCM"; |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * AES GCM using 256-bit key. |
|
| 197 | + */ |
|
| 198 | + const ALGO_A256GCM = "A256GCM"; |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * DEFLATE compression. |
|
| 202 | + */ |
|
| 203 | + const ALGO_DEFLATE = "DEF"; |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Derive algorithm name from the header and optionally from the given JWK. |
|
| 207 | + * |
|
| 208 | + * @param Header $header Header |
|
| 209 | + * @param JWK $jwk Optional JWK |
|
| 210 | + * @throws \UnexpectedValueException If algorithm parameter is not present |
|
| 211 | + * or header and JWK algorithms differ. |
|
| 212 | + * @return string Algorithm name |
|
| 213 | + */ |
|
| 214 | + public static function deriveAlgorithmName(Header $header, JWK $jwk = null): string |
|
| 215 | + { |
|
| 216 | + if ($header->hasAlgorithm()) { |
|
| 217 | + $alg = $header->algorithm()->value(); |
|
| 218 | + } |
|
| 219 | + // if JWK is set, and has an algorithm parameter |
|
| 220 | + if (isset($jwk) && $jwk->hasAlgorithmParameter()) { |
|
| 221 | + $jwk_alg = $jwk->algorithmParameter()->value(); |
|
| 222 | + // check that algorithms match |
|
| 223 | + if (isset($alg) && $alg != $jwk_alg) { |
|
| 224 | + throw new \UnexpectedValueException( |
|
| 225 | + "JWK algorithm '$jwk_alg' doesn't match" . |
|
| 226 | + " the header's algorithm '$alg'."); |
|
| 227 | + } |
|
| 228 | + $alg = $jwk_alg; |
|
| 229 | + } |
|
| 230 | + if (!isset($alg)) { |
|
| 231 | + throw new \UnexpectedValueException("No algorithm parameter."); |
|
| 232 | + } |
|
| 233 | + return $alg; |
|
| 234 | + } |
|
| 235 | 235 | } |
@@ -13,50 +13,50 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class CompressionFactory |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Mapping from algorithm name to class name. |
|
| 18 | - * |
|
| 19 | - * @internal |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - const MAP_ALGO_TO_CLASS = array( |
|
| 24 | - /* @formatter:off */ |
|
| 25 | - JWA::ALGO_DEFLATE => DeflateAlgorithm::class |
|
| 26 | - /* @formatter:on */ |
|
| 27 | - ); |
|
| 16 | + /** |
|
| 17 | + * Mapping from algorithm name to class name. |
|
| 18 | + * |
|
| 19 | + * @internal |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + const MAP_ALGO_TO_CLASS = array( |
|
| 24 | + /* @formatter:off */ |
|
| 25 | + JWA::ALGO_DEFLATE => DeflateAlgorithm::class |
|
| 26 | + /* @formatter:on */ |
|
| 27 | + ); |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Get the compression algorithm by name. |
|
| 31 | - * |
|
| 32 | - * @param string $name |
|
| 33 | - * @throws \UnexpectedValueException If algorithm is not supported |
|
| 34 | - * @return CompressionAlgorithm |
|
| 35 | - */ |
|
| 36 | - public static function algoByName(string $name): CompressionAlgorithm |
|
| 37 | - { |
|
| 38 | - if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
| 39 | - throw new \UnexpectedValueException( |
|
| 40 | - "No compression algorithm '$name'."); |
|
| 41 | - } |
|
| 42 | - $cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
| 43 | - return new $cls(); |
|
| 44 | - } |
|
| 29 | + /** |
|
| 30 | + * Get the compression algorithm by name. |
|
| 31 | + * |
|
| 32 | + * @param string $name |
|
| 33 | + * @throws \UnexpectedValueException If algorithm is not supported |
|
| 34 | + * @return CompressionAlgorithm |
|
| 35 | + */ |
|
| 36 | + public static function algoByName(string $name): CompressionAlgorithm |
|
| 37 | + { |
|
| 38 | + if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
| 39 | + throw new \UnexpectedValueException( |
|
| 40 | + "No compression algorithm '$name'."); |
|
| 41 | + } |
|
| 42 | + $cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
| 43 | + return new $cls(); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Get the compression algorithm as specified in the given header. |
|
| 48 | - * |
|
| 49 | - * @param Header $header Header |
|
| 50 | - * @throws \UnexpectedValueException If compression algorithm parameter is |
|
| 51 | - * not present or algorithm is not supported |
|
| 52 | - * @return CompressionAlgorithm |
|
| 53 | - */ |
|
| 54 | - public static function algoByHeader(Header $header): CompressionAlgorithm |
|
| 55 | - { |
|
| 56 | - if (!$header->hasCompressionAlgorithm()) { |
|
| 57 | - throw new \UnexpectedValueException( |
|
| 58 | - "No compression algorithm parameter."); |
|
| 59 | - } |
|
| 60 | - return self::algoByName($header->compressionAlgorithm()->value()); |
|
| 61 | - } |
|
| 46 | + /** |
|
| 47 | + * Get the compression algorithm as specified in the given header. |
|
| 48 | + * |
|
| 49 | + * @param Header $header Header |
|
| 50 | + * @throws \UnexpectedValueException If compression algorithm parameter is |
|
| 51 | + * not present or algorithm is not supported |
|
| 52 | + * @return CompressionAlgorithm |
|
| 53 | + */ |
|
| 54 | + public static function algoByHeader(Header $header): CompressionAlgorithm |
|
| 55 | + { |
|
| 56 | + if (!$header->hasCompressionAlgorithm()) { |
|
| 57 | + throw new \UnexpectedValueException( |
|
| 58 | + "No compression algorithm parameter."); |
|
| 59 | + } |
|
| 60 | + return self::algoByName($header->compressionAlgorithm()->value()); |
|
| 61 | + } |
|
| 62 | 62 | } |