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.

ECPublicKeyJWK::__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\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
    public 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
     * @throws \UnexpectedValueException
62
     */
63 3
    public static function fromECPublicKey(ECPublicKey $pk): self
64
    {
65 3
        if (!$pk->hasNamedCurve()) {
66 1
            throw new \UnexpectedValueException('No curve name.');
67
        }
68 2
        $curve = CurveParameter::fromOID($pk->namedCurve());
69 2
        [$x, $y] = $pk->curvePointOctets();
70 2
        $xcoord = XCoordinateParameter::fromString($x);
71 2
        $ycoord = YCoordinateParameter::fromString($y);
72 2
        $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC);
73 2
        return new self($key_type, $curve, $xcoord, $ycoord);
74
    }
75
76
    /**
77
     * Initialize from PEM.
78
     */
79 1
    public static function fromPEM(PEM $pem): self
80
    {
81 1
        return self::fromECPublicKey(ECPublicKey::fromPEM($pem));
82
    }
83
84
    /**
85
     * Convert EC public key to PEM.
86
     */
87 5
    public function toPEM(): PEM
88
    {
89 5
        $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value());
90 5
        $x = ECConversion::octetsToNumber(
91 5
            $this->XCoordinateParameter()->coordinateOctets());
92 5
        $y = ECConversion::octetsToNumber(
93 5
            $this->YCoordinateParameter()->coordinateOctets());
94 5
        $ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid);
95 5
        return $ec->publicKeyInfo()->toPEM();
96
    }
97
}
98