AbstractRsaSigner   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

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

4 Methods

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