1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace MiladRahimi\Jwt\Cryptography\Algorithms\Rsa; |
4
|
|
|
|
5
|
|
|
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey; |
6
|
|
|
use MiladRahimi\Jwt\Cryptography\Signer; |
7
|
|
|
use MiladRahimi\Jwt\Exceptions\SigningException; |
8
|
|
|
|
9
|
|
|
class AbstractRsaSigner implements Signer |
10
|
|
|
{ |
11
|
|
|
use Algorithm; |
12
|
|
|
|
13
|
|
|
protected RsaPrivateKey $privateKey; |
14
|
|
|
|
15
|
|
|
protected ?string $kid; |
16
|
|
|
|
17
|
|
|
public function __construct(RsaPrivateKey $publicKey, ?string $kid = null) |
18
|
|
|
{ |
19
|
|
|
$this->privateKey = $publicKey; |
20
|
|
|
$this->kid = $kid; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function sign(string $message): string |
27
|
|
|
{ |
28
|
|
|
$signature = ''; |
29
|
|
|
|
30
|
|
|
if (openssl_sign($message, $signature, $this->privateKey->getResource(), $this->algorithm()) === true) { |
31
|
|
|
return $signature; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
throw new SigningException(openssl_error_string() ?: "OpenSSL cannot sign the token."); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
|
|
public function kid(): ?string |
41
|
|
|
{ |
42
|
|
|
return $this->kid; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getPrivateKey(): RsaPrivateKey |
46
|
|
|
{ |
47
|
|
|
return $this->privateKey; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|