From 8462a5763dca178b8d5b68807c3b4d169aae5e8c Mon Sep 17 00:00:00 2001 From: Milad Rahimi Date: Sat, 10 Jul 2021 17:15:31 +0430 Subject: [PATCH] Support other operating systems --- src/Cryptography/Keys/RsaPrivateKey.php | 14 +++++++++++--- src/Cryptography/Keys/RsaPublicKey.php | 13 ++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Cryptography/Keys/RsaPrivateKey.php b/src/Cryptography/Keys/RsaPrivateKey.php index 56bde82..901ebf5 100644 --- a/src/Cryptography/Keys/RsaPrivateKey.php +++ b/src/Cryptography/Keys/RsaPrivateKey.php @@ -3,6 +3,7 @@ namespace MiladRahimi\Jwt\Cryptography\Keys; use MiladRahimi\Jwt\Exceptions\InvalidKeyException; +use Throwable; /** * Class RsaPrivateKey @@ -25,11 +26,18 @@ class RsaPrivateKey */ public function __construct(string $filePath, $passphrase = '') { - $this->resource = openssl_pkey_get_private('file:///' . $filePath, $passphrase); + try { + $this->resource = openssl_pkey_get_private(file_get_contents(realpath($filePath)), $passphrase); - if (empty($this->resource)) { - throw new InvalidKeyException(); + if (empty($this->resource)) { + throw new InvalidKeyException(); + } + } catch (InvalidKeyException $e) { + throw $e; + } catch (Throwable $e) { + throw new InvalidKeyException('Failed to read the key.', 0, $e); } + } /** diff --git a/src/Cryptography/Keys/RsaPublicKey.php b/src/Cryptography/Keys/RsaPublicKey.php index bb2a09b..54020c8 100644 --- a/src/Cryptography/Keys/RsaPublicKey.php +++ b/src/Cryptography/Keys/RsaPublicKey.php @@ -3,6 +3,7 @@ namespace MiladRahimi\Jwt\Cryptography\Keys; use MiladRahimi\Jwt\Exceptions\InvalidKeyException; +use Throwable; /** * Class RsaPublicKey @@ -24,10 +25,16 @@ class RsaPublicKey */ public function __construct(string $filePath) { - $this->resource = openssl_pkey_get_public('file:///' . $filePath); + try { + $this->resource = openssl_pkey_get_public(file_get_contents(realpath($filePath))); - if (empty($this->resource)) { - throw new InvalidKeyException(); + if (empty($this->resource)) { + throw new InvalidKeyException(); + } + } catch (InvalidKeyException $e) { + throw $e; + } catch (Throwable $e) { + throw new InvalidKeyException('Failed to read the key.', 0, $e); } }