Passed
Push — master ( bf67e8...626507 )
by Milad
02:27
created

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