From fb5394363ee68f62a8fb0f962c14f13590e2c7f7 Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Thu, 7 Mar 2024 21:38:24 +0000 Subject: [PATCH] chore: remove jwt incorrect key warning --- src/JWT.php | 3 +++ tests/JWTTest.php | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/JWT.php b/src/JWT.php index 26349206..93f6e47d 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -251,6 +251,9 @@ public static function sign( return \hash_hmac($algorithm, $msg, $key, true); case 'openssl': $signature = ''; + if (!openssl_pkey_get_private($key)) { + throw new DomainException('OpenSSL unable to validate key'); + } $success = \openssl_sign($msg, $signature, $key, $algorithm); // @phpstan-ignore-line if (!$success) { throw new DomainException('OpenSSL unable to sign data'); diff --git a/tests/JWTTest.php b/tests/JWTTest.php index b59c3c20..d09d43e3 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -26,6 +26,12 @@ public function testMalformedUtf8StringsFail() JWT::encode(['message' => pack('c', 128)], 'a', 'HS256'); } + public function testInvalidKeyOpensslSignFail() + { + $this->expectException(DomainException::class); + JWT::sign('message', 'invalid key', 'openssl'); + } + public function testMalformedJsonThrowsException() { $this->expectException(DomainException::class);