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

AbstractRsaVerifier   A

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 __construct() 0 3 1
A verify() 0 4 3
A kid() 0 3 1
A getPublicKey() 0 3 1
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