AbstractRsaVerifier::verify()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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