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.

RSAPublicKeyJWK::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK\RSA;
6
7
use Sop\CryptoEncoding\PEM;
8
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo;
9
use Sop\CryptoTypes\Asymmetric\RSA\RSAPublicKey;
10
use Sop\JWX\JWK\Asymmetric\PublicKeyJWK;
11
use Sop\JWX\JWK\Parameter\ExponentParameter;
12
use Sop\JWX\JWK\Parameter\JWKParameter;
13
use Sop\JWX\JWK\Parameter\KeyTypeParameter;
14
use Sop\JWX\JWK\Parameter\ModulusParameter;
15
16
/**
17
 * Class representing RSA public key as a JWK.
18
 *
19
 * @see https://tools.ietf.org/html/rfc7517#section-4
20
 * @see https://tools.ietf.org/html/rfc7518#section-6.3
21
 * @see https://tools.ietf.org/html/rfc7518#section-6.3.1
22
 */
23
class RSAPublicKeyJWK extends PublicKeyJWK
24
{
25
    /**
26
     * Parameter names managed by this class.
27
     *
28
     * @internal
29
     *
30
     * @var string[]
31
     */
32
    public const MANAGED_PARAMS = [
33
        JWKParameter::PARAM_KEY_TYPE,
34
        JWKParameter::PARAM_MODULUS,
35
        JWKParameter::PARAM_EXPONENT,
36
    ];
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param JWKParameter ...$params
42
     *
43
     * @throws \UnexpectedValueException If missing required parameter
44
     */
45 36
    public function __construct(JWKParameter ...$params)
46
    {
47 36
        parent::__construct(...$params);
48 36
        foreach (self::MANAGED_PARAMS as $name) {
49 36
            if (!$this->has($name)) {
50 2
                throw new \UnexpectedValueException(
51 36
                    "Missing '{$name}' parameter.");
52
            }
53
        }
54 34
        if (KeyTypeParameter::TYPE_RSA !== $this->keyTypeParameter()->value()) {
55 1
            throw new \UnexpectedValueException('Invalid key type.');
56
        }
57 33
    }
58
59
    /**
60
     * Initialize from RSAPublicKey.
61
     */
62 4
    public static function fromRSAPublicKey(RSAPublicKey $pk): self
63
    {
64 4
        $n = ModulusParameter::fromNumber($pk->modulus());
65 4
        $e = ExponentParameter::fromNumber($pk->publicExponent());
66 4
        $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA);
67 4
        return new self($key_type, $n, $e);
68
    }
69
70
    /**
71
     * Initialize from PEM.
72
     */
73 3
    public static function fromPEM(PEM $pem): self
74
    {
75 3
        return self::fromRSAPublicKey(RSAPublicKey::fromPEM($pem));
76
    }
77
78
    /**
79
     * Convert JWK to PEM.
80
     */
81 16
    public function toPEM(): PEM
82
    {
83 16
        $n = $this->modulusParameter()->number()->base10();
84 16
        $e = $this->exponentParameter()->number()->base10();
85 16
        $pk = new RSAPublicKey($n, $e);
86 16
        return PublicKeyInfo::fromPublicKey($pk)->toPEM();
87
    }
88
}
89