From 23d51fef4668e927f52f16c95c6a6bab98ca7c01 Mon Sep 17 00:00:00 2001 From: Milad Rahimi Date: Sat, 1 Jun 2024 12:00:03 +0200 Subject: [PATCH 1/6] bring the realpath back --- src/Cryptography/Keys/EcdsaPrivateKey.php | 5 ++--- src/Cryptography/Keys/EcdsaPublicKey.php | 5 ++--- src/Cryptography/Keys/RsaPrivateKey.php | 5 ++--- src/Cryptography/Keys/RsaPublicKey.php | 5 ++--- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Cryptography/Keys/EcdsaPrivateKey.php b/src/Cryptography/Keys/EcdsaPrivateKey.php index c482b2b..54fc119 100644 --- a/src/Cryptography/Keys/EcdsaPrivateKey.php +++ b/src/Cryptography/Keys/EcdsaPrivateKey.php @@ -3,12 +3,11 @@ namespace MiladRahimi\Jwt\Cryptography\Keys; use MiladRahimi\Jwt\Exceptions\InvalidKeyException; -use Throwable; class EcdsaPrivateKey { /** - * @var mixed Key file resource handler + * @var resource Key resource handler */ protected $resource; @@ -23,7 +22,7 @@ class EcdsaPrivateKey */ public function __construct(string $key, string $passphrase = '', ?string $id = null) { - $content = file_exists($key) ? file_get_contents($key) : $key; + $content = file_exists($key) ? file_get_contents(realpath($key)) : $key; $this->resource = openssl_pkey_get_private($content, $passphrase); if ($this->resource === false) { diff --git a/src/Cryptography/Keys/EcdsaPublicKey.php b/src/Cryptography/Keys/EcdsaPublicKey.php index 0e43fca..c4fab54 100644 --- a/src/Cryptography/Keys/EcdsaPublicKey.php +++ b/src/Cryptography/Keys/EcdsaPublicKey.php @@ -3,12 +3,11 @@ namespace MiladRahimi\Jwt\Cryptography\Keys; use MiladRahimi\Jwt\Exceptions\InvalidKeyException; -use Throwable; class EcdsaPublicKey { /** - * @var mixed Key file resource handler + * @var resource Key resource handler */ protected $resource; @@ -22,7 +21,7 @@ class EcdsaPublicKey */ public function __construct(string $key, ?string $id = null) { - $content = file_exists($key) ? file_get_contents($key) : $key; + $content = file_exists($key) ? file_get_contents(realpath($key)) : $key; $this->resource = openssl_pkey_get_public($content); if ($this->resource === false) { diff --git a/src/Cryptography/Keys/RsaPrivateKey.php b/src/Cryptography/Keys/RsaPrivateKey.php index 2c7b87b..960e507 100644 --- a/src/Cryptography/Keys/RsaPrivateKey.php +++ b/src/Cryptography/Keys/RsaPrivateKey.php @@ -3,12 +3,11 @@ namespace MiladRahimi\Jwt\Cryptography\Keys; use MiladRahimi\Jwt\Exceptions\InvalidKeyException; -use Throwable; class RsaPrivateKey { /** - * @var mixed Key file resource handler + * @var resource Key resource handler */ protected $resource; @@ -23,7 +22,7 @@ class RsaPrivateKey */ public function __construct(string $key, string $passphrase = '', ?string $id = null) { - $content = file_exists($key) ? file_get_contents($key) : $key; + $content = file_exists($key) ? file_get_contents(realpath($key)) : $key; $this->resource = openssl_pkey_get_private($content, $passphrase); if ($this->resource === false) { diff --git a/src/Cryptography/Keys/RsaPublicKey.php b/src/Cryptography/Keys/RsaPublicKey.php index 92b5e8a..0592ba2 100644 --- a/src/Cryptography/Keys/RsaPublicKey.php +++ b/src/Cryptography/Keys/RsaPublicKey.php @@ -3,12 +3,11 @@ namespace MiladRahimi\Jwt\Cryptography\Keys; use MiladRahimi\Jwt\Exceptions\InvalidKeyException; -use Throwable; class RsaPublicKey { /** - * @var mixed Key file resource handler + * @var resource Key resource handler */ protected $resource; @@ -22,7 +21,7 @@ class RsaPublicKey */ public function __construct(string $key, ?string $id = null) { - $content = file_exists($key) ? file_get_contents($key) : $key; + $content = file_exists($key) ? file_get_contents(realpath($key)) : $key; $this->resource = openssl_pkey_get_public($content); if ($this->resource === false) { From 2149fa225e339ccdf424a1e2f591e3b9b2c892f5 Mon Sep 17 00:00:00 2001 From: Milad Rahimi Date: Sat, 1 Jun 2024 12:01:26 +0200 Subject: [PATCH 2/6] fix return type --- src/Cryptography/Keys/EcdsaPrivateKey.php | 2 +- src/Cryptography/Keys/EcdsaPublicKey.php | 2 +- src/Cryptography/Keys/RsaPrivateKey.php | 2 +- src/Cryptography/Keys/RsaPublicKey.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Cryptography/Keys/EcdsaPrivateKey.php b/src/Cryptography/Keys/EcdsaPrivateKey.php index 54fc119..86411b4 100644 --- a/src/Cryptography/Keys/EcdsaPrivateKey.php +++ b/src/Cryptography/Keys/EcdsaPrivateKey.php @@ -33,7 +33,7 @@ public function __construct(string $key, string $passphrase = '', ?string $id = } /** - * @return mixed + * @return resource */ public function getResource() { diff --git a/src/Cryptography/Keys/EcdsaPublicKey.php b/src/Cryptography/Keys/EcdsaPublicKey.php index c4fab54..b0161af 100644 --- a/src/Cryptography/Keys/EcdsaPublicKey.php +++ b/src/Cryptography/Keys/EcdsaPublicKey.php @@ -32,7 +32,7 @@ public function __construct(string $key, ?string $id = null) } /** - * @return mixed + * @return resource */ public function getResource() { diff --git a/src/Cryptography/Keys/RsaPrivateKey.php b/src/Cryptography/Keys/RsaPrivateKey.php index 960e507..f558aa9 100644 --- a/src/Cryptography/Keys/RsaPrivateKey.php +++ b/src/Cryptography/Keys/RsaPrivateKey.php @@ -33,7 +33,7 @@ public function __construct(string $key, string $passphrase = '', ?string $id = } /** - * @return mixed + * @return resource */ public function getResource() { diff --git a/src/Cryptography/Keys/RsaPublicKey.php b/src/Cryptography/Keys/RsaPublicKey.php index 0592ba2..2bd4632 100644 --- a/src/Cryptography/Keys/RsaPublicKey.php +++ b/src/Cryptography/Keys/RsaPublicKey.php @@ -32,7 +32,7 @@ public function __construct(string $key, ?string $id = null) } /** - * @return mixed + * @return resource */ public function getResource() { From 60309e7e4ba3b3643cb9712a883f45984736dab5 Mon Sep 17 00:00:00 2001 From: Milad Rahimi Date: Sat, 1 Jun 2024 12:05:55 +0200 Subject: [PATCH 3/6] add cs fixer cache --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7af493e..c5bf2a4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ /build /vendor .DS_Store +.php-cs-fixer.cache .phpunit.result.cache composer.lock From 353477eb168fe596b7c48c2f6d6424eaaf57548b Mon Sep 17 00:00:00 2001 From: Milad Rahimi Date: Sat, 1 Jun 2024 12:06:17 +0200 Subject: [PATCH 4/6] run cs fixer --- src/Base64/Base64Parser.php | 4 +++- src/Base64/SafeBase64Parser.php | 4 +++- .../Algorithms/Ecdsa/AbstractEcdsaSigner.php | 5 ++++- .../Algorithms/Ecdsa/AbstractEcdsaVerifier.php | 5 ++++- .../Algorithms/Ecdsa/Algorithm.php | 4 +++- .../Algorithms/Ecdsa/ES256KVerifier.php | 4 +++- .../Algorithms/Ecdsa/ES256Signer.php | 4 +++- .../Algorithms/Ecdsa/ES256Verifier.php | 4 +++- .../Algorithms/Ecdsa/ES384Signer.php | 4 +++- .../Algorithms/Ecdsa/ES384Verifier.php | 4 +++- .../Algorithms/Eddsa/EdDsaSigner.php | 5 ++++- .../Algorithms/Eddsa/EdDsaVerifier.php | 4 +++- .../Algorithms/Hmac/AbstractHmac.php | 4 +++- src/Cryptography/Algorithms/Hmac/HS256.php | 4 +++- src/Cryptography/Algorithms/Hmac/HS384.php | 4 +++- src/Cryptography/Algorithms/Hmac/HS512.php | 4 +++- .../Algorithms/Rsa/AbstractRsaSigner.php | 4 +++- .../Algorithms/Rsa/AbstractRsaVerifier.php | 4 +++- src/Cryptography/Algorithms/Rsa/Algorithm.php | 4 +++- .../Algorithms/Rsa/RS256Signer.php | 4 +++- .../Algorithms/Rsa/RS256Verifier.php | 4 +++- .../Algorithms/Rsa/RS384Signer.php | 4 +++- .../Algorithms/Rsa/RS384Verifier.php | 4 +++- .../Algorithms/Rsa/RS512Signer.php | 4 +++- .../Algorithms/Rsa/RS512Verifier.php | 4 +++- src/Cryptography/Keys/EcdsaPrivateKey.php | 4 +++- src/Cryptography/Keys/EcdsaPublicKey.php | 4 +++- src/Cryptography/Keys/EdDsaPrivateKey.php | 4 +++- src/Cryptography/Keys/EdDsaPublicKey.php | 4 +++- src/Cryptography/Keys/HmacKey.php | 4 +++- src/Cryptography/Keys/RsaPrivateKey.php | 4 +++- src/Cryptography/Keys/RsaPublicKey.php | 4 +++- src/Cryptography/Signer.php | 4 +++- src/Cryptography/Verifier.php | 4 +++- src/Enums/PublicClaimNames.php | 18 ++++++++++-------- src/Exceptions/InvalidKeyException.php | 4 +++- src/Exceptions/InvalidSignatureException.php | 4 +++- src/Exceptions/InvalidTokenException.php | 4 +++- src/Exceptions/JsonDecodingException.php | 4 +++- src/Exceptions/JsonEncodingException.php | 4 +++- src/Exceptions/NoKidException.php | 4 +++- src/Exceptions/SigningException.php | 4 +++- src/Exceptions/ValidationException.php | 4 +++- src/Exceptions/VerifierNotFoundException.php | 4 +++- src/Generator.php | 4 +++- src/Json/JsonParser.php | 4 +++- src/Json/StrictJsonParser.php | 4 +++- src/Parser.php | 7 ++++--- src/Validator/BaseValidator.php | 4 +++- src/Validator/DefaultValidator.php | 4 +++- src/Validator/Rule.php | 4 +++- src/Validator/Rules/ConsistsOf.php | 4 +++- src/Validator/Rules/EqualsTo.php | 4 +++- src/Validator/Rules/GreaterThan.php | 4 +++- src/Validator/Rules/GreaterThanOrEqualTo.php | 4 +++- src/Validator/Rules/IdenticalTo.php | 4 +++- src/Validator/Rules/LessThan.php | 4 +++- src/Validator/Rules/LessThanOrEqualTo.php | 4 +++- src/Validator/Rules/NewerThan.php | 4 +++- src/Validator/Rules/NewerThanOrSame.php | 4 +++- src/Validator/Rules/NotEmpty.php | 4 +++- src/Validator/Rules/NotNull.php | 4 +++- src/Validator/Rules/OlderThan.php | 4 +++- src/Validator/Rules/OlderThanOrSame.php | 4 +++- src/Validator/Validator.php | 4 +++- src/VerifierFactory.php | 2 +- 66 files changed, 207 insertions(+), 75 deletions(-) diff --git a/src/Base64/Base64Parser.php b/src/Base64/Base64Parser.php index edd8496..e0c81df 100644 --- a/src/Base64/Base64Parser.php +++ b/src/Base64/Base64Parser.php @@ -1,4 +1,6 @@ -verifier = $verifier; $this->validator = $validator ?: new DefaultValidator(); $this->jsonParser = $jsonParser ?: new StrictJsonParser(); diff --git a/src/Validator/BaseValidator.php b/src/Validator/BaseValidator.php index a703d86..adb6be7 100644 --- a/src/Validator/BaseValidator.php +++ b/src/Validator/BaseValidator.php @@ -1,4 +1,6 @@ - Date: Sat, 1 Jun 2024 12:18:52 +0200 Subject: [PATCH 5/6] use realpath to check files --- src/Cryptography/Keys/EcdsaPrivateKey.php | 6 +++--- src/Cryptography/Keys/EcdsaPublicKey.php | 6 +++--- src/Cryptography/Keys/RsaPrivateKey.php | 6 +++--- src/Cryptography/Keys/RsaPublicKey.php | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Cryptography/Keys/EcdsaPrivateKey.php b/src/Cryptography/Keys/EcdsaPrivateKey.php index 0f81da7..146f4ff 100644 --- a/src/Cryptography/Keys/EcdsaPrivateKey.php +++ b/src/Cryptography/Keys/EcdsaPrivateKey.php @@ -9,7 +9,7 @@ class EcdsaPrivateKey { /** - * @var resource Key resource handler + * @var mixed Key resource handler */ protected $resource; @@ -24,7 +24,7 @@ class EcdsaPrivateKey */ public function __construct(string $key, string $passphrase = '', ?string $id = null) { - $content = file_exists($key) ? file_get_contents(realpath($key)) : $key; + $content = realpath($key) ? file_get_contents(realpath($key)) : $key; $this->resource = openssl_pkey_get_private($content, $passphrase); if ($this->resource === false) { @@ -35,7 +35,7 @@ public function __construct(string $key, string $passphrase = '', ?string $id = } /** - * @return resource + * @return mixed */ public function getResource() { diff --git a/src/Cryptography/Keys/EcdsaPublicKey.php b/src/Cryptography/Keys/EcdsaPublicKey.php index c2b21ff..381955a 100644 --- a/src/Cryptography/Keys/EcdsaPublicKey.php +++ b/src/Cryptography/Keys/EcdsaPublicKey.php @@ -9,7 +9,7 @@ class EcdsaPublicKey { /** - * @var resource Key resource handler + * @var mixed Key resource handler */ protected $resource; @@ -23,7 +23,7 @@ class EcdsaPublicKey */ public function __construct(string $key, ?string $id = null) { - $content = file_exists($key) ? file_get_contents(realpath($key)) : $key; + $content = realpath($key) ? file_get_contents(realpath($key)) : $key; $this->resource = openssl_pkey_get_public($content); if ($this->resource === false) { @@ -34,7 +34,7 @@ public function __construct(string $key, ?string $id = null) } /** - * @return resource + * @return mixed */ public function getResource() { diff --git a/src/Cryptography/Keys/RsaPrivateKey.php b/src/Cryptography/Keys/RsaPrivateKey.php index 48a2af9..2125ba8 100644 --- a/src/Cryptography/Keys/RsaPrivateKey.php +++ b/src/Cryptography/Keys/RsaPrivateKey.php @@ -9,7 +9,7 @@ class RsaPrivateKey { /** - * @var resource Key resource handler + * @var mixed Key resource handler */ protected $resource; @@ -24,7 +24,7 @@ class RsaPrivateKey */ public function __construct(string $key, string $passphrase = '', ?string $id = null) { - $content = file_exists($key) ? file_get_contents(realpath($key)) : $key; + $content = realpath($key) ? file_get_contents(realpath($key)) : $key; $this->resource = openssl_pkey_get_private($content, $passphrase); if ($this->resource === false) { @@ -35,7 +35,7 @@ public function __construct(string $key, string $passphrase = '', ?string $id = } /** - * @return resource + * @return mixed */ public function getResource() { diff --git a/src/Cryptography/Keys/RsaPublicKey.php b/src/Cryptography/Keys/RsaPublicKey.php index 0b9602a..51d34d9 100644 --- a/src/Cryptography/Keys/RsaPublicKey.php +++ b/src/Cryptography/Keys/RsaPublicKey.php @@ -9,7 +9,7 @@ class RsaPublicKey { /** - * @var resource Key resource handler + * @var mixed Key resource handler */ protected $resource; @@ -23,7 +23,7 @@ class RsaPublicKey */ public function __construct(string $key, ?string $id = null) { - $content = file_exists($key) ? file_get_contents(realpath($key)) : $key; + $content = realpath($key) ? file_get_contents(realpath($key)) : $key; $this->resource = openssl_pkey_get_public($content); if ($this->resource === false) { @@ -34,7 +34,7 @@ public function __construct(string $key, ?string $id = null) } /** - * @return resource + * @return mixed */ public function getResource() { From a41c622bf884b29b1b694cf92699dc6e9665731b Mon Sep 17 00:00:00 2001 From: Milad Rahimi Date: Sat, 1 Jun 2024 12:19:14 +0200 Subject: [PATCH 6/6] run cs fix --- tests/ExamplesTest.php | 8 ++++++-- tests/VerifierFactoryTest.php | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/ExamplesTest.php b/tests/ExamplesTest.php index 5accfe7..bb81137 100644 --- a/tests/ExamplesTest.php +++ b/tests/ExamplesTest.php @@ -117,12 +117,16 @@ public function test_eddsa_algorithms() public function test_multiple_keys() { $privateKey1 = new RsaPrivateKey( - __DIR__ . '/../assets/keys/rsa-private.pem', '', 'key-1' + __DIR__ . '/../assets/keys/rsa-private.pem', + '', + 'key-1' ); $publicKey1 = new RsaPublicKey(__DIR__ . '/../assets/keys/rsa-public.pem', 'key-1'); $privateKey2 = new EcdsaPrivateKey( - __DIR__ . '/../assets/keys/ecdsa384-private.pem', '', 'key-2' + __DIR__ . '/../assets/keys/ecdsa384-private.pem', + '', + 'key-2' ); $publicKey2 = new EcdsaPublicKey(__DIR__ . '/../assets/keys/ecdsa384-public.pem', 'key-2'); diff --git a/tests/VerifierFactoryTest.php b/tests/VerifierFactoryTest.php index af2afc5..016b7aa 100644 --- a/tests/VerifierFactoryTest.php +++ b/tests/VerifierFactoryTest.php @@ -25,7 +25,9 @@ class VerifierFactoryTest extends TestCase public function test_getVerifier_it_should_return_the_right_verifier() { $privateKey = new RsaPrivateKey( - __DIR__ . '/../assets/keys/rsa-private.pem', '', 'key-1' + __DIR__ . '/../assets/keys/rsa-private.pem', + '', + 'key-1' ); $publicKey = new RsaPublicKey(__DIR__ . '/../assets/keys/rsa-public.pem', 'key-1');