Passed
Push — master ( d759f2...bf67e8 )
by Milad
02:12
created

AbstractRsaSigner::kid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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