AbstractRsaVerifier   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A kid() 0 3 1
A verify() 0 4 3
A getPublicKey() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MiladRahimi\Jwt\Cryptography\Algorithms\Rsa;
6
7
use MiladRahimi\Jwt\Cryptography\Keys\RsaPublicKey;
8
use MiladRahimi\Jwt\Cryptography\Verifier;
9
use MiladRahimi\Jwt\Exceptions\InvalidSignatureException;
10
11
class AbstractRsaVerifier implements Verifier
12
{
13
    use Algorithm;
14
15
    protected RsaPublicKey $publicKey;
16
17
    public function __construct(RsaPublicKey $publicKey)
18
    {
19
        $this->publicKey = $publicKey;
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function verify(string $plain, string $signature): void
26
    {
27
        if (openssl_verify($plain, $signature, $this->publicKey->getResource(), $this->algorithm()) !== 1) {
28
            throw new InvalidSignatureException(openssl_error_string() ?: "The signature is invalid.");
29
        }
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function kid(): ?string
36
    {
37
        return $this->publicKey->getId();
38
    }
39
40
    public function getPublicKey(): RsaPublicKey
41
    {
42
        return $this->publicKey;
43
    }
44
}
45