1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace MiladRahimi\Jwt\Cryptography\Keys; |
4
|
|
|
|
5
|
|
|
use MiladRahimi\Jwt\Exceptions\InvalidKeyException; |
6
|
|
|
use Throwable; |
7
|
|
|
|
8
|
|
|
class RsaPublicKey |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var mixed Key file resource handler |
12
|
|
|
*/ |
13
|
|
|
private $resource; |
14
|
|
|
|
15
|
|
|
protected ?string $id; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @throws InvalidKeyException |
19
|
|
|
*/ |
20
|
|
|
public function __construct(string $filePath, ?string $id = null) |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
|
|
$this->resource = openssl_pkey_get_public(file_get_contents(realpath($filePath))); |
24
|
|
|
} catch (Throwable $e) { |
25
|
|
|
throw new InvalidKeyException('Failed to read the key.', 0, $e); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($this->resource === false) { |
29
|
|
|
throw new InvalidKeyException(openssl_error_string()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->id = $id; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function getResource() |
39
|
|
|
{ |
40
|
|
|
return $this->resource; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getId(): ?string |
44
|
|
|
{ |
45
|
|
|
return $this->id; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|