@@ -21,134 +21,134 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class ECDSAAlgorithm extends OpenSSLSignatureAlgorithm |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Mapping from algorithm name to class name. |
|
| 26 | - * |
|
| 27 | - * @internal |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - const MAP_ALGO_TO_CLASS = [ |
|
| 32 | - JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 33 | - JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 34 | - JWA::ALGO_ES512 => ES512Algorithm::class, |
|
| 35 | - ]; |
|
| 24 | + /** |
|
| 25 | + * Mapping from algorithm name to class name. |
|
| 26 | + * |
|
| 27 | + * @internal |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + const MAP_ALGO_TO_CLASS = [ |
|
| 32 | + JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 33 | + JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 34 | + JWA::ALGO_ES512 => ES512Algorithm::class, |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Signature size in bytes. |
|
| 39 | - * |
|
| 40 | - * @var int |
|
| 41 | - */ |
|
| 42 | - private $_signatureSize; |
|
| 37 | + /** |
|
| 38 | + * Signature size in bytes. |
|
| 39 | + * |
|
| 40 | + * @var int |
|
| 41 | + */ |
|
| 42 | + private $_signatureSize; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Constructor. |
|
| 46 | - * |
|
| 47 | - * @param ECPublicKeyJWK $pub_key |
|
| 48 | - * @param ECPrivateKeyJWK $priv_key |
|
| 49 | - */ |
|
| 50 | - protected function __construct(ECPublicKeyJWK $pub_key, |
|
| 51 | - ?ECPrivateKeyJWK $priv_key = null) |
|
| 52 | - { |
|
| 53 | - $curve = $pub_key->curveParameter()->value(); |
|
| 54 | - if ($this->_curveName() !== $curve) { |
|
| 55 | - throw new \InvalidArgumentException( |
|
| 56 | - 'Key with ' . $this->_curveName() . |
|
| 57 | - " curve expected, got {$curve}."); |
|
| 58 | - } |
|
| 59 | - $this->_publicKey = $pub_key; |
|
| 60 | - $this->_privateKey = $priv_key; |
|
| 61 | - $key_size = $pub_key->curveParameter()->keySizeBits(); |
|
| 62 | - $this->_signatureSize = intval(ceil($key_size / 8) * 2); |
|
| 63 | - } |
|
| 44 | + /** |
|
| 45 | + * Constructor. |
|
| 46 | + * |
|
| 47 | + * @param ECPublicKeyJWK $pub_key |
|
| 48 | + * @param ECPrivateKeyJWK $priv_key |
|
| 49 | + */ |
|
| 50 | + protected function __construct(ECPublicKeyJWK $pub_key, |
|
| 51 | + ?ECPrivateKeyJWK $priv_key = null) |
|
| 52 | + { |
|
| 53 | + $curve = $pub_key->curveParameter()->value(); |
|
| 54 | + if ($this->_curveName() !== $curve) { |
|
| 55 | + throw new \InvalidArgumentException( |
|
| 56 | + 'Key with ' . $this->_curveName() . |
|
| 57 | + " curve expected, got {$curve}."); |
|
| 58 | + } |
|
| 59 | + $this->_publicKey = $pub_key; |
|
| 60 | + $this->_privateKey = $priv_key; |
|
| 61 | + $key_size = $pub_key->curveParameter()->keySizeBits(); |
|
| 62 | + $this->_signatureSize = intval(ceil($key_size / 8) * 2); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Initialize from a public key. |
|
| 67 | - * |
|
| 68 | - * @param ECPublicKeyJWK $jwk |
|
| 69 | - * |
|
| 70 | - * @return self |
|
| 71 | - */ |
|
| 72 | - public static function fromPublicKey(ECPublicKeyJWK $jwk): self |
|
| 73 | - { |
|
| 74 | - return new static($jwk); |
|
| 75 | - } |
|
| 65 | + /** |
|
| 66 | + * Initialize from a public key. |
|
| 67 | + * |
|
| 68 | + * @param ECPublicKeyJWK $jwk |
|
| 69 | + * |
|
| 70 | + * @return self |
|
| 71 | + */ |
|
| 72 | + public static function fromPublicKey(ECPublicKeyJWK $jwk): self |
|
| 73 | + { |
|
| 74 | + return new static($jwk); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Initialize from a private key. |
|
| 79 | - * |
|
| 80 | - * @param ECPrivateKeyJWK $jwk |
|
| 81 | - * |
|
| 82 | - * @return self |
|
| 83 | - */ |
|
| 84 | - public static function fromPrivateKey(ECPrivateKeyJWK $jwk): self |
|
| 85 | - { |
|
| 86 | - return new static($jwk->publicKey(), $jwk); |
|
| 87 | - } |
|
| 77 | + /** |
|
| 78 | + * Initialize from a private key. |
|
| 79 | + * |
|
| 80 | + * @param ECPrivateKeyJWK $jwk |
|
| 81 | + * |
|
| 82 | + * @return self |
|
| 83 | + */ |
|
| 84 | + public static function fromPrivateKey(ECPrivateKeyJWK $jwk): self |
|
| 85 | + { |
|
| 86 | + return new static($jwk->publicKey(), $jwk); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * {@inheritdoc} |
|
| 91 | - * |
|
| 92 | - * @return self |
|
| 93 | - */ |
|
| 94 | - public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 95 | - { |
|
| 96 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 97 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 98 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 99 | - } |
|
| 100 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 101 | - if ($jwk->has(...ECPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 102 | - return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
| 103 | - } |
|
| 104 | - return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
| 105 | - } |
|
| 89 | + /** |
|
| 90 | + * {@inheritdoc} |
|
| 91 | + * |
|
| 92 | + * @return self |
|
| 93 | + */ |
|
| 94 | + public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 95 | + { |
|
| 96 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 97 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 98 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 99 | + } |
|
| 100 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 101 | + if ($jwk->has(...ECPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 102 | + return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
| 103 | + } |
|
| 104 | + return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * {@inheritdoc} |
|
| 109 | - */ |
|
| 110 | - public function computeSignature(string $data): string |
|
| 111 | - { |
|
| 112 | - // OpenSSL returns ECDSA signature as a DER encoded ECDSA-Sig-Value |
|
| 113 | - $der = parent::computeSignature($data); |
|
| 114 | - $sig = ECSignature::fromDER($der); |
|
| 115 | - $mlen = intval(floor($this->_signatureSize / 2)); |
|
| 116 | - return ECConversion::numberToOctets($sig->r(), $mlen) . |
|
| 117 | - ECConversion::numberToOctets($sig->s(), $mlen); |
|
| 118 | - } |
|
| 107 | + /** |
|
| 108 | + * {@inheritdoc} |
|
| 109 | + */ |
|
| 110 | + public function computeSignature(string $data): string |
|
| 111 | + { |
|
| 112 | + // OpenSSL returns ECDSA signature as a DER encoded ECDSA-Sig-Value |
|
| 113 | + $der = parent::computeSignature($data); |
|
| 114 | + $sig = ECSignature::fromDER($der); |
|
| 115 | + $mlen = intval(floor($this->_signatureSize / 2)); |
|
| 116 | + return ECConversion::numberToOctets($sig->r(), $mlen) . |
|
| 117 | + ECConversion::numberToOctets($sig->s(), $mlen); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * {@inheritdoc} |
|
| 122 | - * |
|
| 123 | - * @throws \UnexpectedValueException If signature length is invalid |
|
| 124 | - */ |
|
| 125 | - public function validateSignature(string $data, string $signature): bool |
|
| 126 | - { |
|
| 127 | - if (strlen($signature) !== $this->_signatureSize) { |
|
| 128 | - throw new \UnexpectedValueException('Invalid signature length.'); |
|
| 129 | - } |
|
| 130 | - [$r_octets, $s_octets] = str_split($signature, |
|
| 131 | - intval(floor($this->_signatureSize / 2))); |
|
| 132 | - // convert signature to DER sequence for OpenSSL |
|
| 133 | - $r = ECConversion::octetsToNumber($r_octets); |
|
| 134 | - $s = ECConversion::octetsToNumber($s_octets); |
|
| 135 | - $sig = new ECSignature($r, $s); |
|
| 136 | - return parent::validateSignature($data, $sig->toDER()); |
|
| 137 | - } |
|
| 120 | + /** |
|
| 121 | + * {@inheritdoc} |
|
| 122 | + * |
|
| 123 | + * @throws \UnexpectedValueException If signature length is invalid |
|
| 124 | + */ |
|
| 125 | + public function validateSignature(string $data, string $signature): bool |
|
| 126 | + { |
|
| 127 | + if (strlen($signature) !== $this->_signatureSize) { |
|
| 128 | + throw new \UnexpectedValueException('Invalid signature length.'); |
|
| 129 | + } |
|
| 130 | + [$r_octets, $s_octets] = str_split($signature, |
|
| 131 | + intval(floor($this->_signatureSize / 2))); |
|
| 132 | + // convert signature to DER sequence for OpenSSL |
|
| 133 | + $r = ECConversion::octetsToNumber($r_octets); |
|
| 134 | + $s = ECConversion::octetsToNumber($s_octets); |
|
| 135 | + $sig = new ECSignature($r, $s); |
|
| 136 | + return parent::validateSignature($data, $sig->toDER()); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * {@inheritdoc} |
|
| 141 | - */ |
|
| 142 | - public function headerParameters(): array |
|
| 143 | - { |
|
| 144 | - return array_merge(parent::headerParameters(), |
|
| 145 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 146 | - } |
|
| 139 | + /** |
|
| 140 | + * {@inheritdoc} |
|
| 141 | + */ |
|
| 142 | + public function headerParameters(): array |
|
| 143 | + { |
|
| 144 | + return array_merge(parent::headerParameters(), |
|
| 145 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Get the name of the curve used by this algorithm. |
|
| 150 | - * |
|
| 151 | - * @return string |
|
| 152 | - */ |
|
| 153 | - abstract protected function _curveName(): string; |
|
| 148 | + /** |
|
| 149 | + * Get the name of the curve used by this algorithm. |
|
| 150 | + * |
|
| 151 | + * @return string |
|
| 152 | + */ |
|
| 153 | + abstract protected function _curveName(): string; |
|
| 154 | 154 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWS\Algorithm; |
| 6 | 6 | |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RS384Algorithm extends RSASSAPKCS1Algorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_RS384; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_RS384; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _mdMethod(): int |
|
| 28 | - { |
|
| 29 | - return OPENSSL_ALGO_SHA384; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _mdMethod(): int |
|
| 28 | + { |
|
| 29 | + return OPENSSL_ALGO_SHA384; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWS\Algorithm; |
| 6 | 6 | |
@@ -18,89 +18,89 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class HMACAlgorithm extends SignatureAlgorithm |
| 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 = [ |
|
| 29 | - JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 30 | - JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 31 | - JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 32 | - ]; |
|
| 21 | + /** |
|
| 22 | + * Mapping from algorithm name to class name. |
|
| 23 | + * |
|
| 24 | + * @internal |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + const MAP_ALGO_TO_CLASS = [ |
|
| 29 | + JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 30 | + JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 31 | + JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 32 | + ]; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Shared secret key. |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - protected $_key; |
|
| 34 | + /** |
|
| 35 | + * Shared secret key. |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + protected $_key; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Constructor. |
|
| 43 | - * |
|
| 44 | - * @param string $key Shared secret key |
|
| 45 | - */ |
|
| 46 | - public function __construct(string $key) |
|
| 47 | - { |
|
| 48 | - $this->_key = $key; |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Constructor. |
|
| 43 | + * |
|
| 44 | + * @param string $key Shared secret key |
|
| 45 | + */ |
|
| 46 | + public function __construct(string $key) |
|
| 47 | + { |
|
| 48 | + $this->_key = $key; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * {@inheritdoc} |
|
| 53 | - * |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 57 | - { |
|
| 58 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 59 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 60 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 61 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 62 | - } |
|
| 63 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 64 | - return new $cls($jwk->key()); |
|
| 65 | - } |
|
| 51 | + /** |
|
| 52 | + * {@inheritdoc} |
|
| 53 | + * |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 57 | + { |
|
| 58 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 59 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 60 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 61 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 62 | + } |
|
| 63 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 64 | + return new $cls($jwk->key()); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - * |
|
| 70 | - * @throws \RuntimeException For generic errors |
|
| 71 | - */ |
|
| 72 | - public function computeSignature(string $data): string |
|
| 73 | - { |
|
| 74 | - $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
| 75 | - if (false === $result) { |
|
| 76 | - $err = error_get_last(); |
|
| 77 | - $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 78 | - throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
| 79 | - } |
|
| 80 | - return $result; |
|
| 81 | - } |
|
| 67 | + /** |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + * |
|
| 70 | + * @throws \RuntimeException For generic errors |
|
| 71 | + */ |
|
| 72 | + public function computeSignature(string $data): string |
|
| 73 | + { |
|
| 74 | + $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
| 75 | + if (false === $result) { |
|
| 76 | + $err = error_get_last(); |
|
| 77 | + $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 78 | + throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
| 79 | + } |
|
| 80 | + return $result; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - public function validateSignature(string $data, string $signature): bool |
|
| 87 | - { |
|
| 88 | - return $this->computeSignature($data) === $signature; |
|
| 89 | - } |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + public function validateSignature(string $data, string $signature): bool |
|
| 87 | + { |
|
| 88 | + return $this->computeSignature($data) === $signature; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * {@inheritdoc} |
|
| 93 | - */ |
|
| 94 | - public function headerParameters(): array |
|
| 95 | - { |
|
| 96 | - return array_merge(parent::headerParameters(), |
|
| 97 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 98 | - } |
|
| 91 | + /** |
|
| 92 | + * {@inheritdoc} |
|
| 93 | + */ |
|
| 94 | + public function headerParameters(): array |
|
| 95 | + { |
|
| 96 | + return array_merge(parent::headerParameters(), |
|
| 97 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Get algorithm name recognized by the Hash extension. |
|
| 102 | - * |
|
| 103 | - * @return string |
|
| 104 | - */ |
|
| 105 | - abstract protected function _hashAlgo(): string; |
|
| 100 | + /** |
|
| 101 | + * Get algorithm name recognized by the Hash extension. |
|
| 102 | + * |
|
| 103 | + * @return string |
|
| 104 | + */ |
|
| 105 | + abstract protected function _hashAlgo(): string; |
|
| 106 | 106 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWS\Algorithm; |
| 6 | 6 | |
@@ -14,27 +14,27 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ES512Algorithm extends ECDSAAlgorithm |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function algorithmParamValue(): string |
|
| 21 | - { |
|
| 22 | - return JWA::ALGO_ES512; |
|
| 23 | - } |
|
| 17 | + /** |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function algorithmParamValue(): string |
|
| 21 | + { |
|
| 22 | + return JWA::ALGO_ES512; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - protected function _curveName(): string |
|
| 29 | - { |
|
| 30 | - return CurveParameter::CURVE_P521; |
|
| 31 | - } |
|
| 25 | + /** |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + protected function _curveName(): string |
|
| 29 | + { |
|
| 30 | + return CurveParameter::CURVE_P521; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * {@inheritdoc} |
|
| 35 | - */ |
|
| 36 | - protected function _mdMethod(): int |
|
| 37 | - { |
|
| 38 | - return OPENSSL_ALGO_SHA512; |
|
| 39 | - } |
|
| 33 | + /** |
|
| 34 | + * {@inheritdoc} |
|
| 35 | + */ |
|
| 36 | + protected function _mdMethod(): int |
|
| 37 | + { |
|
| 38 | + return OPENSSL_ALGO_SHA512; |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWS\Algorithm; |
| 6 | 6 | |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class HS256Algorithm extends HMACAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_HS256; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_HS256; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _hashAlgo(): string |
|
| 28 | - { |
|
| 29 | - return 'sha256'; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _hashAlgo(): string |
|
| 28 | + { |
|
| 29 | + return 'sha256'; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWS\Algorithm; |
| 6 | 6 | |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RS256Algorithm extends RSASSAPKCS1Algorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_RS256; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_RS256; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _mdMethod(): int |
|
| 28 | - { |
|
| 29 | - return OPENSSL_ALGO_SHA256; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _mdMethod(): int |
|
| 28 | + { |
|
| 29 | + return OPENSSL_ALGO_SHA256; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWS\Algorithm; |
| 6 | 6 | |
@@ -28,40 +28,40 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | class UUIDv4 |
| 30 | 30 | { |
| 31 | - /** |
|
| 32 | - * UUID. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - protected $_uuid; |
|
| 31 | + /** |
|
| 32 | + * UUID. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + protected $_uuid; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Constructor. |
|
| 40 | - * |
|
| 41 | - * @param string $uuid UUIDv4 in canonical hexadecimal format |
|
| 42 | - */ |
|
| 43 | - public function __construct(string $uuid) |
|
| 44 | - { |
|
| 45 | - // @todo Check that UUID is version 4 |
|
| 46 | - $this->_uuid = $uuid; |
|
| 47 | - } |
|
| 38 | + /** |
|
| 39 | + * Constructor. |
|
| 40 | + * |
|
| 41 | + * @param string $uuid UUIDv4 in canonical hexadecimal format |
|
| 42 | + */ |
|
| 43 | + public function __construct(string $uuid) |
|
| 44 | + { |
|
| 45 | + // @todo Check that UUID is version 4 |
|
| 46 | + $this->_uuid = $uuid; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function __toString(): string |
|
| 53 | - { |
|
| 54 | - return $this->canonical(); |
|
| 55 | - } |
|
| 49 | + /** |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function __toString(): string |
|
| 53 | + { |
|
| 54 | + return $this->canonical(); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Create new random UUIDv4. |
|
| 59 | - * |
|
| 60 | - * @return self |
|
| 61 | - */ |
|
| 62 | - public static function createRandom(): self |
|
| 63 | - { |
|
| 64 | - /* |
|
| 57 | + /** |
|
| 58 | + * Create new random UUIDv4. |
|
| 59 | + * |
|
| 60 | + * @return self |
|
| 61 | + */ |
|
| 62 | + public static function createRandom(): self |
|
| 63 | + { |
|
| 64 | + /* |
|
| 65 | 65 | 1. Set the two most significant bits (bits 6 and 7) of |
| 66 | 66 | the clock_seq_hi_and_reserved to zero and one, respectively. |
| 67 | 67 | |
@@ -72,29 +72,29 @@ discard block |
||
| 72 | 72 | 3. Set all the other bits to randomly (or pseudo-randomly) |
| 73 | 73 | chosen values. |
| 74 | 74 | */ |
| 75 | - $uuid = sprintf('%04x%04x-%04x-%04x-%02x%02x-%04x%04x%04x', |
|
| 76 | - // time_low |
|
| 77 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
| 78 | - // time_mid |
|
| 79 | - mt_rand(0, 0xffff), |
|
| 80 | - // time_hi_and_version |
|
| 81 | - mt_rand(0, 0x0fff) | 0x4000, |
|
| 82 | - // clk_seq_hi_res |
|
| 83 | - mt_rand(0, 0x3f) | 0x80, |
|
| 84 | - // clk_seq_low |
|
| 85 | - mt_rand(0, 0xff), |
|
| 86 | - // node |
|
| 87 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
| 88 | - return new self($uuid); |
|
| 89 | - } |
|
| 75 | + $uuid = sprintf('%04x%04x-%04x-%04x-%02x%02x-%04x%04x%04x', |
|
| 76 | + // time_low |
|
| 77 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
| 78 | + // time_mid |
|
| 79 | + mt_rand(0, 0xffff), |
|
| 80 | + // time_hi_and_version |
|
| 81 | + mt_rand(0, 0x0fff) | 0x4000, |
|
| 82 | + // clk_seq_hi_res |
|
| 83 | + mt_rand(0, 0x3f) | 0x80, |
|
| 84 | + // clk_seq_low |
|
| 85 | + mt_rand(0, 0xff), |
|
| 86 | + // node |
|
| 87 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
| 88 | + return new self($uuid); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Get UUIDv4 in canonical form. |
|
| 93 | - * |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public function canonical(): string |
|
| 97 | - { |
|
| 98 | - return $this->_uuid; |
|
| 99 | - } |
|
| 91 | + /** |
|
| 92 | + * Get UUIDv4 in canonical form. |
|
| 93 | + * |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public function canonical(): string |
|
| 97 | + { |
|
| 98 | + return $this->_uuid; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\Util; |
| 6 | 6 | |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | // time_mid |
| 79 | 79 | mt_rand(0, 0xffff), |
| 80 | 80 | // time_hi_and_version |
| 81 | - mt_rand(0, 0x0fff) | 0x4000, |
|
| 81 | + mt_rand(0, 0x0fff)|0x4000, |
|
| 82 | 82 | // clk_seq_hi_res |
| 83 | - mt_rand(0, 0x3f) | 0x80, |
|
| 83 | + mt_rand(0, 0x3f)|0x80, |
|
| 84 | 84 | // clk_seq_low |
| 85 | 85 | mt_rand(0, 0xff), |
| 86 | 86 | // node |
@@ -9,87 +9,87 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class BigInt |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Number. |
|
| 14 | - * |
|
| 15 | - * @var \GMP |
|
| 16 | - */ |
|
| 17 | - protected $_num; |
|
| 12 | + /** |
|
| 13 | + * Number. |
|
| 14 | + * |
|
| 15 | + * @var \GMP |
|
| 16 | + */ |
|
| 17 | + protected $_num; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param \GMP $num GMP number |
|
| 23 | - */ |
|
| 24 | - protected function __construct(\GMP $num) |
|
| 25 | - { |
|
| 26 | - $this->_num = $num; |
|
| 27 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param \GMP $num GMP number |
|
| 23 | + */ |
|
| 24 | + protected function __construct(\GMP $num) |
|
| 25 | + { |
|
| 26 | + $this->_num = $num; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @return string |
|
| 31 | - */ |
|
| 32 | - public function __toString(): string |
|
| 33 | - { |
|
| 34 | - return $this->base10(); |
|
| 35 | - } |
|
| 29 | + /** |
|
| 30 | + * @return string |
|
| 31 | + */ |
|
| 32 | + public function __toString(): string |
|
| 33 | + { |
|
| 34 | + return $this->base10(); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Initialize from a base10 number. |
|
| 39 | - * |
|
| 40 | - * @param int|string $number |
|
| 41 | - * |
|
| 42 | - * @return self |
|
| 43 | - */ |
|
| 44 | - public static function fromBase10($number): self |
|
| 45 | - { |
|
| 46 | - $num = gmp_init($number, 10); |
|
| 47 | - return new self($num); |
|
| 48 | - } |
|
| 37 | + /** |
|
| 38 | + * Initialize from a base10 number. |
|
| 39 | + * |
|
| 40 | + * @param int|string $number |
|
| 41 | + * |
|
| 42 | + * @return self |
|
| 43 | + */ |
|
| 44 | + public static function fromBase10($number): self |
|
| 45 | + { |
|
| 46 | + $num = gmp_init($number, 10); |
|
| 47 | + return new self($num); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Initialize from a base256 number. |
|
| 52 | - * |
|
| 53 | - * Base64 number is an octet string of big endian, most significant word |
|
| 54 | - * first integer. |
|
| 55 | - * |
|
| 56 | - * @param string $octets |
|
| 57 | - * |
|
| 58 | - * @return self |
|
| 59 | - */ |
|
| 60 | - public static function fromBase256(string $octets): self |
|
| 61 | - { |
|
| 62 | - $num = gmp_import($octets, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 63 | - return new self($num); |
|
| 64 | - } |
|
| 50 | + /** |
|
| 51 | + * Initialize from a base256 number. |
|
| 52 | + * |
|
| 53 | + * Base64 number is an octet string of big endian, most significant word |
|
| 54 | + * first integer. |
|
| 55 | + * |
|
| 56 | + * @param string $octets |
|
| 57 | + * |
|
| 58 | + * @return self |
|
| 59 | + */ |
|
| 60 | + public static function fromBase256(string $octets): self |
|
| 61 | + { |
|
| 62 | + $num = gmp_import($octets, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 63 | + return new self($num); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Convert to base10 string. |
|
| 68 | - * |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - public function base10(): string |
|
| 72 | - { |
|
| 73 | - return gmp_strval($this->_num, 10); |
|
| 74 | - } |
|
| 66 | + /** |
|
| 67 | + * Convert to base10 string. |
|
| 68 | + * |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + public function base10(): string |
|
| 72 | + { |
|
| 73 | + return gmp_strval($this->_num, 10); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Convert to base16 string. |
|
| 78 | - * |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function base16(): string |
|
| 82 | - { |
|
| 83 | - return gmp_strval($this->_num, 16); |
|
| 84 | - } |
|
| 76 | + /** |
|
| 77 | + * Convert to base16 string. |
|
| 78 | + * |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function base16(): string |
|
| 82 | + { |
|
| 83 | + return gmp_strval($this->_num, 16); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Convert to base256 string. |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function base256(): string |
|
| 92 | - { |
|
| 93 | - return gmp_export($this->_num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 94 | - } |
|
| 86 | + /** |
|
| 87 | + * Convert to base256 string. |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function base256(): string |
|
| 92 | + { |
|
| 93 | + return gmp_export($this->_num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\Util; |
| 6 | 6 | |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public static function fromBase256(string $octets): self |
| 61 | 61 | { |
| 62 | - $num = gmp_import($octets, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 62 | + $num = gmp_import($octets, 1, GMP_MSW_FIRST|GMP_BIG_ENDIAN); |
|
| 63 | 63 | return new self($num); |
| 64 | 64 | } |
| 65 | 65 | |
@@ -90,6 +90,6 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | public function base256(): string |
| 92 | 92 | { |
| 93 | - return gmp_export($this->_num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 93 | + return gmp_export($this->_num, 1, GMP_MSW_FIRST|GMP_BIG_ENDIAN); |
|
| 94 | 94 | } |
| 95 | 95 | } |
@@ -9,111 +9,111 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Base64 |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Encode a string using base64url variant. |
|
| 14 | - * |
|
| 15 | - * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 16 | - * |
|
| 17 | - * @param string $data |
|
| 18 | - * |
|
| 19 | - * @return string |
|
| 20 | - */ |
|
| 21 | - public static function urlEncode(string $data): string |
|
| 22 | - { |
|
| 23 | - return strtr(rtrim(self::encode($data), '='), '+/', '-_'); |
|
| 24 | - } |
|
| 12 | + /** |
|
| 13 | + * Encode a string using base64url variant. |
|
| 14 | + * |
|
| 15 | + * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 16 | + * |
|
| 17 | + * @param string $data |
|
| 18 | + * |
|
| 19 | + * @return string |
|
| 20 | + */ |
|
| 21 | + public static function urlEncode(string $data): string |
|
| 22 | + { |
|
| 23 | + return strtr(rtrim(self::encode($data), '='), '+/', '-_'); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Decode a string using base64url variant. |
|
| 28 | - * |
|
| 29 | - * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 30 | - * |
|
| 31 | - * @param string $data |
|
| 32 | - * |
|
| 33 | - * @throws \UnexpectedValueException |
|
| 34 | - * |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public static function urlDecode(string $data): string |
|
| 38 | - { |
|
| 39 | - $data = strtr($data, '-_', '+/'); |
|
| 40 | - switch (strlen($data) % 4) { |
|
| 41 | - case 0: |
|
| 42 | - break; |
|
| 43 | - case 2: |
|
| 44 | - $data .= '=='; |
|
| 45 | - break; |
|
| 46 | - case 3: |
|
| 47 | - $data .= '='; |
|
| 48 | - break; |
|
| 49 | - default: |
|
| 50 | - throw new \UnexpectedValueException( |
|
| 51 | - 'Malformed base64url encoding.'); |
|
| 52 | - } |
|
| 53 | - return self::decode($data); |
|
| 54 | - } |
|
| 26 | + /** |
|
| 27 | + * Decode a string using base64url variant. |
|
| 28 | + * |
|
| 29 | + * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 30 | + * |
|
| 31 | + * @param string $data |
|
| 32 | + * |
|
| 33 | + * @throws \UnexpectedValueException |
|
| 34 | + * |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public static function urlDecode(string $data): string |
|
| 38 | + { |
|
| 39 | + $data = strtr($data, '-_', '+/'); |
|
| 40 | + switch (strlen($data) % 4) { |
|
| 41 | + case 0: |
|
| 42 | + break; |
|
| 43 | + case 2: |
|
| 44 | + $data .= '=='; |
|
| 45 | + break; |
|
| 46 | + case 3: |
|
| 47 | + $data .= '='; |
|
| 48 | + break; |
|
| 49 | + default: |
|
| 50 | + throw new \UnexpectedValueException( |
|
| 51 | + 'Malformed base64url encoding.'); |
|
| 52 | + } |
|
| 53 | + return self::decode($data); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Check whether string is validly base64url encoded. |
|
| 58 | - * |
|
| 59 | - * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 60 | - * |
|
| 61 | - * @param string $data |
|
| 62 | - * |
|
| 63 | - * @return bool |
|
| 64 | - */ |
|
| 65 | - public static function isValidURLEncoding(string $data): bool |
|
| 66 | - { |
|
| 67 | - return 1 === preg_match('#^[A-Za-z0-9\-_]*$#', $data); |
|
| 68 | - } |
|
| 56 | + /** |
|
| 57 | + * Check whether string is validly base64url encoded. |
|
| 58 | + * |
|
| 59 | + * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 60 | + * |
|
| 61 | + * @param string $data |
|
| 62 | + * |
|
| 63 | + * @return bool |
|
| 64 | + */ |
|
| 65 | + public static function isValidURLEncoding(string $data): bool |
|
| 66 | + { |
|
| 67 | + return 1 === preg_match('#^[A-Za-z0-9\-_]*$#', $data); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Encode a string in base64. |
|
| 72 | - * |
|
| 73 | - * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 74 | - * |
|
| 75 | - * @param string $data |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public static function encode(string $data): string |
|
| 80 | - { |
|
| 81 | - return base64_encode($data); |
|
| 82 | - } |
|
| 70 | + /** |
|
| 71 | + * Encode a string in base64. |
|
| 72 | + * |
|
| 73 | + * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 74 | + * |
|
| 75 | + * @param string $data |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public static function encode(string $data): string |
|
| 80 | + { |
|
| 81 | + return base64_encode($data); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Decode a string from base64 encoding. |
|
| 86 | - * |
|
| 87 | - * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 88 | - * |
|
| 89 | - * @param string $data |
|
| 90 | - * |
|
| 91 | - * @throws \RuntimeException If decoding fails |
|
| 92 | - * |
|
| 93 | - * @return string |
|
| 94 | - */ |
|
| 95 | - public static function decode(string $data): string |
|
| 96 | - { |
|
| 97 | - $ret = base64_decode($data, true); |
|
| 98 | - if (!is_string($ret)) { |
|
| 99 | - $err = error_get_last(); |
|
| 100 | - $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 101 | - throw new \RuntimeException($msg ?? 'base64_decode() failed.'); |
|
| 102 | - } |
|
| 103 | - return $ret; |
|
| 104 | - } |
|
| 84 | + /** |
|
| 85 | + * Decode a string from base64 encoding. |
|
| 86 | + * |
|
| 87 | + * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 88 | + * |
|
| 89 | + * @param string $data |
|
| 90 | + * |
|
| 91 | + * @throws \RuntimeException If decoding fails |
|
| 92 | + * |
|
| 93 | + * @return string |
|
| 94 | + */ |
|
| 95 | + public static function decode(string $data): string |
|
| 96 | + { |
|
| 97 | + $ret = base64_decode($data, true); |
|
| 98 | + if (!is_string($ret)) { |
|
| 99 | + $err = error_get_last(); |
|
| 100 | + $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 101 | + throw new \RuntimeException($msg ?? 'base64_decode() failed.'); |
|
| 102 | + } |
|
| 103 | + return $ret; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Check whether string is validly base64 encoded. |
|
| 108 | - * |
|
| 109 | - * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 110 | - * |
|
| 111 | - * @param string $data |
|
| 112 | - * |
|
| 113 | - * @return bool |
|
| 114 | - */ |
|
| 115 | - public static function isValid(string $data): bool |
|
| 116 | - { |
|
| 117 | - return 1 === preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data); |
|
| 118 | - } |
|
| 106 | + /** |
|
| 107 | + * Check whether string is validly base64 encoded. |
|
| 108 | + * |
|
| 109 | + * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 110 | + * |
|
| 111 | + * @param string $data |
|
| 112 | + * |
|
| 113 | + * @return bool |
|
| 114 | + */ |
|
| 115 | + public static function isValid(string $data): bool |
|
| 116 | + { |
|
| 117 | + return 1 === preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data); |
|
| 118 | + } |
|
| 119 | 119 | } |
@@ -38,17 +38,17 @@ |
||
| 38 | 38 | { |
| 39 | 39 | $data = strtr($data, '-_', '+/'); |
| 40 | 40 | switch (strlen($data) % 4) { |
| 41 | - case 0: |
|
| 42 | - break; |
|
| 43 | - case 2: |
|
| 44 | - $data .= '=='; |
|
| 45 | - break; |
|
| 46 | - case 3: |
|
| 47 | - $data .= '='; |
|
| 48 | - break; |
|
| 49 | - default: |
|
| 50 | - throw new \UnexpectedValueException( |
|
| 51 | - 'Malformed base64url encoding.'); |
|
| 41 | + case 0: |
|
| 42 | + break; |
|
| 43 | + case 2: |
|
| 44 | + $data .= '=='; |
|
| 45 | + break; |
|
| 46 | + case 3: |
|
| 47 | + $data .= '='; |
|
| 48 | + break; |
|
| 49 | + default: |
|
| 50 | + throw new \UnexpectedValueException( |
|
| 51 | + 'Malformed base64url encoding.'); |
|
| 52 | 52 | } |
| 53 | 53 | return self::decode($data); |
| 54 | 54 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\Util; |
| 6 | 6 | |