Skip to content

Commit

Permalink
Support other operating systems
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi authored Jul 10, 2021
1 parent 4d29ca9 commit 8462a57
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/Cryptography/Keys/RsaPrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MiladRahimi\Jwt\Cryptography\Keys;

use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
use Throwable;

/**
* Class RsaPrivateKey
Expand All @@ -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);
}

}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Cryptography/Keys/RsaPublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MiladRahimi\Jwt\Cryptography\Keys;

use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
use Throwable;

/**
* Class RsaPublicKey
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 8462a57

Please sign in to comment.