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.
Passed
Branch php72 (880eb0)
by Joni
05:58
created

ECPublicKeyJWK::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 5
nop 1
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK\EC;
6
7
use Sop\CryptoEncoding\PEM;
8
use Sop\CryptoTypes\Asymmetric\EC\ECConversion;
9
use Sop\CryptoTypes\Asymmetric\EC\ECPublicKey;
10
use Sop\JWX\JWK\Asymmetric\PublicKeyJWK;
11
use Sop\JWX\JWK\Parameter\CurveParameter;
12
use Sop\JWX\JWK\Parameter\JWKParameter;
13
use Sop\JWX\JWK\Parameter\KeyTypeParameter;
14
use Sop\JWX\JWK\Parameter\XCoordinateParameter;
15
use Sop\JWX\JWK\Parameter\YCoordinateParameter;
16
17
/**
18
 * Class representing elliptic curve public key as a JWK.
19
 *
20
 * @see https://tools.ietf.org/html/rfc7517#section-4
21
 * @see https://tools.ietf.org/html/rfc7518#section-6.2
22
 * @see https://tools.ietf.org/html/rfc7518#section-6.2.1
23
 */
24
class ECPublicKeyJWK extends PublicKeyJWK
25
{
26
    /**
27
     * Parameter names managed by this class.
28
     *
29
     * @var string[]
30
     */
31
    const MANAGED_PARAMS = [
32
        JWKParameter::PARAM_KEY_TYPE,
33
        JWKParameter::PARAM_CURVE,
34
        JWKParameter::PARAM_X_COORDINATE,
35
    ];
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param JWKParameter ...$params
41
     *
42
     * @throws \UnexpectedValueException If missing required parameter
43
     */
44 15
    public function __construct(JWKParameter ...$params)
45
    {
46 15
        parent::__construct(...$params);
47 15
        foreach (self::MANAGED_PARAMS as $name) {
48 15
            if (!$this->has($name)) {
49 2
                throw new \UnexpectedValueException(
50 15
                    "Missing '{$name}' parameter.");
51
            }
52
        }
53 13
        if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) {
54 1
            throw new \UnexpectedValueException('Invalid key type.');
55
        }
56 12
    }
57
58
    /**
59
     * Initialize from ECPublicKey.
60
     *
61
     * @param ECPublicKey $pk
62
     *
63
     * @throws \UnexpectedValueException
64
     *
65
     * @return self
66
     */
67 3
    public static function fromECPublicKey(ECPublicKey $pk): self
68
    {
69 3
        if (!$pk->hasNamedCurve()) {
70 1
            throw new \UnexpectedValueException('No curve name.');
71
        }
72 2
        $curve = CurveParameter::fromOID($pk->namedCurve());
73 2
        [$x, $y] = $pk->curvePointOctets();
74 2
        $xcoord = XCoordinateParameter::fromString($x);
75 2
        $ycoord = YCoordinateParameter::fromString($y);
76 2
        $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC);
77 2
        return new self($key_type, $curve, $xcoord, $ycoord);
78
    }
79
80
    /**
81
     * Initialize from PEM.
82
     *
83
     * @param PEM $pem
84
     *
85
     * @return self
86
     */
87 1
    public static function fromPEM(PEM $pem): self
88
    {
89 1
        return self::fromECPublicKey(ECPublicKey::fromPEM($pem));
90
    }
91
92
    /**
93
     * Convert EC public key to PEM.
94
     *
95
     * @return PEM
96
     */
97 5
    public function toPEM(): PEM
98
    {
99 5
        $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value());
100 5
        $x = ECConversion::octetsToNumber(
101 5
            $this->XCoordinateParameter()->coordinateOctets());
102 5
        $y = ECConversion::octetsToNumber(
103 5
            $this->YCoordinateParameter()->coordinateOctets());
104 5
        $ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid);
105 5
        return $ec->publicKeyInfo()->toPEM();
106
    }
107
}
108