GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RSA::extractPublicKeyFromPrivateKeyFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Gandung\JWT\Algorithm;
4
5
use Gandung\JWT\SignerInterface;
6
use Gandung\JWT\Manager\KeyManagerInterface;
7
8
/**
9
 * @author Paulus Gandung Prakosa <[email protected]>
10
 */
11
abstract class RSA implements SignerInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function sign($payload, KeyManagerInterface $key)
17
    {
18
        $priv = \openssl_get_privatekey(
19
            $key->getContent(),
20
            $key->getPassphrase()
21
        );
22
23
        if (false === $priv) {
24
            throw new \RuntimeException(
25
                sprintf("Error: %s\n", \openssl_error_string())
26
            );
27
        }
28
29
        \openssl_sign($payload, $signature, $priv, $this->getAlgorithm());
30
31
        return $signature;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function verify($expected, $payload, KeyManagerInterface $key)
38
    {
39
        $pub = $this->extractPublicKeyFromPrivateKeyFile($key);
40
41
        return \openssl_verify($payload, $expected, $pub, $this->getAlgorithm()) === 1;
42
    }
43
44
    /**
45
     * Extract public key from private key file.
46
     *
47
     * @param string $key
48
     */
49
    private function extractPublicKeyFromPrivateKeyFile(KeyManagerInterface $key)
50
    {
51
        $priv = \openssl_get_privatekey(
52
            $key->getContent(),
53
            $key->getPassphrase()
54
        );
55
56
        if (false === $priv) {
57
            throw new \RuntimeException(
58
                sprintf("Failed to get private key object: %s\n", \openssl_error_string())
59
            );
60
        }
61
62
        $details = \openssl_pkey_get_details($priv);
63
64
        return $details['key'];
65
    }
66
}
67