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

AbstractRsaSigner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 39
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A kid() 0 3 1
A getPrivateKey() 0 3 1
A sign() 0 9 3
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