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.
Completed
Push — master ( 74ce6a...af33f1 )
by Joni
05:36
created

ECPublicKeyJWK::fromECPublicKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
3
namespace JWX\JWK\EC;
4
5
use CryptoUtil\ASN1\EC\ECPublicKey;
6
use CryptoUtil\Conversion\ECConversion;
7
use CryptoUtil\PEM\PEM;
8
use JWX\JWK\Feature\AsymmetricPublicKey;
9
use JWX\JWK\JWK;
10
use JWX\JWK\Parameter\CurveParameter;
11
use JWX\JWK\Parameter\JWKParameter;
12
use JWX\JWK\Parameter\KeyTypeParameter;
13
use JWX\JWK\Parameter\RegisteredJWKParameter;
14
use JWX\JWK\Parameter\XCoordinateParameter;
15
use JWX\JWK\Parameter\YCoordinateParameter;
16
17
18
/**
19
 * Class representing elliptic curve public key as a JWK.
20
 *
21
 * @link https://tools.ietf.org/html/rfc7517#section-4
22
 * @link https://tools.ietf.org/html/rfc7518#section-6.2
23
 * @link https://tools.ietf.org/html/rfc7518#section-6.2.1
24
 */
25
class ECPublicKeyJWK extends JWK implements AsymmetricPublicKey
26
{
27
	/**
28
	 * Parameter names managed by this class.
29
	 *
30
	 * @var string[]
31
	 */
32
	const MANAGED_PARAMS = array(
33
		/* @formatter:off */
34
		RegisteredJWKParameter::PARAM_KEY_TYPE, 
35
		RegisteredJWKParameter::PARAM_CURVE, 
36
		RegisteredJWKParameter::PARAM_X_COORDINATE
37
		/* @formatter:on */
38
	);
39
	
40
	/**
41
	 * Constructor
42
	 *
43
	 * @param JWKParameter ...$params
44
	 * @throws \UnexpectedValueException If missing required parameter
45
	 */
46 14 View Code Duplication
	public function __construct(JWKParameter ...$params) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47 14
		parent::__construct(...$params);
48 14
		foreach (self::MANAGED_PARAMS as $name) {
49 14
			if (!$this->has($name)) {
50 1
				throw new \UnexpectedValueException("Missing '$name' parameter.");
51
			}
52 13
		}
53 13
		if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_EC) {
54 1
			throw new \UnexpectedValueException("Invalid key type.");
55
		}
56 12
	}
57
	
58
	/**
59
	 * Initialize from ECPublicKey.
60
	 *
61
	 * @param ECPublicKey $pk
62
	 * @throws \UnexpectedValueException
63
	 * @return self
64
	 */
65 2
	public static function fromECPublicKey(ECPublicKey $pk) {
66 2
		if (!$pk->hasNamedCurve()) {
67 1
			throw new \UnexpectedValueException("No curve name.");
68
		}
69 1
		$curve = CurveParameter::fromOID($pk->namedCurve());
70 1
		list($x, $y) = $pk->curvePointOctets();
71 1
		$xcoord = XCoordinateParameter::fromString($x);
72 1
		$ycoord = YCoordinateParameter::fromString($y);
73 1
		$key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC);
74 1
		return new self($key_type, $curve, $xcoord, $ycoord);
75
	}
76
	
77
	/**
78
	 * Initialize from PEM.
79
	 *
80
	 * @param PEM $pem
81
	 * @return self
82
	 */
83 1
	public static function fromPEM(PEM $pem) {
84 1
		return self::fromECPublicKey(ECPublicKey::fromPEM($pem));
85
	}
86
	
87
	/**
88
	 * Convert EC public key to PEM.
89
	 *
90
	 * @return PEM
91
	 */
92 5
	public function toPEM() {
93 5
		$curve_oid = CurveParameter::nameToOID($this->curveParameter()->value());
94 5
		$x = ECConversion::octetsToNumber(
95 5
			$this->XCoordinateParameter()->coordinateOctets());
96 5
		$y = ECConversion::octetsToNumber(
97 5
			$this->YCoordinateParameter()->coordinateOctets());
98 5
		$ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid);
99 5
		return $ec->publicKeyInfo()->toPEM();
100
	}
101
}
102