Skip to content

Commit 8462a57

Browse files
authored
Support other operating systems
1 parent 4d29ca9 commit 8462a57

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/Cryptography/Keys/RsaPrivateKey.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MiladRahimi\Jwt\Cryptography\Keys;
44

55
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
6+
use Throwable;
67

78
/**
89
* Class RsaPrivateKey
@@ -25,11 +26,18 @@ class RsaPrivateKey
2526
*/
2627
public function __construct(string $filePath, $passphrase = '')
2728
{
28-
$this->resource = openssl_pkey_get_private('file:///' . $filePath, $passphrase);
29+
try {
30+
$this->resource = openssl_pkey_get_private(file_get_contents(realpath($filePath)), $passphrase);
2931

30-
if (empty($this->resource)) {
31-
throw new InvalidKeyException();
32+
if (empty($this->resource)) {
33+
throw new InvalidKeyException();
34+
}
35+
} catch (InvalidKeyException $e) {
36+
throw $e;
37+
} catch (Throwable $e) {
38+
throw new InvalidKeyException('Failed to read the key.', 0, $e);
3239
}
40+
3341
}
3442

3543
/**

src/Cryptography/Keys/RsaPublicKey.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MiladRahimi\Jwt\Cryptography\Keys;
44

55
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
6+
use Throwable;
67

78
/**
89
* Class RsaPublicKey
@@ -24,10 +25,16 @@ class RsaPublicKey
2425
*/
2526
public function __construct(string $filePath)
2627
{
27-
$this->resource = openssl_pkey_get_public('file:///' . $filePath);
28+
try {
29+
$this->resource = openssl_pkey_get_public(file_get_contents(realpath($filePath)));
2830

29-
if (empty($this->resource)) {
30-
throw new InvalidKeyException();
31+
if (empty($this->resource)) {
32+
throw new InvalidKeyException();
33+
}
34+
} catch (InvalidKeyException $e) {
35+
throw $e;
36+
} catch (Throwable $e) {
37+
throw new InvalidKeyException('Failed to read the key.', 0, $e);
3138
}
3239
}
3340

0 commit comments

Comments
 (0)