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

RSAPrivateKeyJWK   B

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 12.3 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 16
dl 15
loc 122
ccs 59
cts 59
cp 1
rs 8.4614

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 4
A fromRSAPrivateKey() 0 12 1
A fromPEM() 0 3 1
A publicKey() 0 6 1
B toPEM() 0 30 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace JWX\JWK\RSA;
4
5
use CryptoUtil\ASN1\AlgorithmIdentifier\Crypto\RSAEncryptionAlgorithmIdentifier;
6
use CryptoUtil\ASN1\PrivateKeyInfo;
7
use CryptoUtil\ASN1\RSA\RSAPrivateKey;
8
use CryptoUtil\PEM\PEM;
9
use JWX\JWK\Feature\AsymmetricPrivateKey;
10
use JWX\JWK\JWK;
11
use JWX\JWK\Parameter\ExponentParameter;
12
use JWX\JWK\Parameter\FirstCRTCoefficientParameter;
13
use JWX\JWK\Parameter\FirstFactorCRTExponentParameter;
14
use JWX\JWK\Parameter\FirstPrimeFactorParameter;
15
use JWX\JWK\Parameter\JWKParameter;
16
use JWX\JWK\Parameter\KeyTypeParameter;
17
use JWX\JWK\Parameter\ModulusParameter;
18
use JWX\JWK\Parameter\PrivateExponentParameter;
19
use JWX\JWK\Parameter\RegisteredJWKParameter;
20
use JWX\JWK\Parameter\SecondFactorCRTExponentParameter;
21
use JWX\JWK\Parameter\SecondPrimeFactorParameter;
22
23
24
/**
25
 * Class representing RSA private key as a JWK.
26
 *
27
 * @link https://tools.ietf.org/html/rfc7517#section-4
28
 * @link https://tools.ietf.org/html/rfc7518#section-6.3
29
 * @link https://tools.ietf.org/html/rfc7518#section-6.3.2
30
 */
31
class RSAPrivateKeyJWK extends JWK implements AsymmetricPrivateKey
32
{
33
	/**
34
	 * Parameter names managed by this class.
35
	 *
36
	 * @internal
37
	 *
38
	 * @var string[]
39
	 */
40
	const MANAGED_PARAMS = array(
41
		/* @formatter:off */
42
		RegisteredJWKParameter::PARAM_KEY_TYPE,
43
		RegisteredJWKParameter::PARAM_MODULUS,
44
		RegisteredJWKParameter::PARAM_EXPONENT,
45
		RegisteredJWKParameter::PARAM_PRIVATE_EXPONENT,
46
		RegisteredJWKParameter::PARAM_FIRST_PRIME_FACTOR,
47
		RegisteredJWKParameter::PARAM_SECOND_PRIME_FACTOR,
48
		RegisteredJWKParameter::PARAM_FIRST_FACTOR_CRT_EXPONENT,
49
		RegisteredJWKParameter::PARAM_SECOND_FACTOR_CRT_EXPONENT,
50
		RegisteredJWKParameter::PARAM_FIRST_CRT_COEFFICIENT
51
		/* @formatter:on */
52
	);
53
	
54
	/**
55
	 * Constructor
56
	 *
57
	 * @param JWKParameter ...$params
58
	 * @throws \UnexpectedValueException If missing required parameter
59
	 */
60 11 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...
61 11
		parent::__construct(...$params);
62 11
		foreach (self::MANAGED_PARAMS as $name) {
63 11
			if (!$this->has($name)) {
64 1
				throw new \UnexpectedValueException("Missing '$name' parameter.");
65
			}
66 10
		}
67 10
		if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_RSA) {
68 1
			throw new \UnexpectedValueException("Invalid key type.");
69
		}
70
		// cast private exponent to correct class
71 9
		$key = RegisteredJWKParameter::PARAM_PRIVATE_EXPONENT;
72 9
		$this->_parameters[$key] = new PrivateExponentParameter(
73 9
			$this->_parameters[$key]->value());
74 9
	}
75
	
76
	/**
77
	 * Initialize from RSAPrivateKey.
78
	 *
79
	 * @param RSAPrivateKey $pk
80
	 * @return self
81
	 */
82 2
	public static function fromRSAPrivateKey(RSAPrivateKey $pk) {
83 2
		$n = ModulusParameter::fromNumber($pk->modulus());
84 2
		$e = ExponentParameter::fromNumber($pk->publicExponent());
85 2
		$d = PrivateExponentParameter::fromNumber($pk->privateExponent());
86 2
		$p = FirstPrimeFactorParameter::fromNumber($pk->prime1());
87 2
		$q = SecondPrimeFactorParameter::fromNumber($pk->prime2());
88 2
		$dp = FirstFactorCRTExponentParameter::fromNumber($pk->exponent1());
89 2
		$dq = SecondFactorCRTExponentParameter::fromNumber($pk->exponent2());
90 2
		$qi = FirstCRTCoefficientParameter::fromNumber($pk->coefficient());
91 2
		$key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA);
92 2
		return new self($key_type, $n, $e, $d, $p, $q, $dp, $dq, $qi);
93
	}
94
	
95
	/**
96
	 * Initialize from PEM.
97
	 *
98
	 * @param PEM $pem
99
	 * @return self
100
	 */
101 1
	public static function fromPEM(PEM $pem) {
102 1
		return self::fromRSAPrivateKey(RSAPrivateKey::fromPEM($pem));
103
	}
104
	
105
	/**
106
	 * Get public key component.
107
	 *
108
	 * @return RSAPublicKeyJWK
109
	 */
110 26
	public function publicKey() {
111 26
		$kty = $this->keyTypeParameter();
112 26
		$n = $this->modulusParameter();
113 26
		$e = $this->exponentParameter();
114 26
		return new RSAPublicKeyJWK($kty, $n, $e);
115
	}
116
	
117
	/**
118
	 * Convert JWK to PEM.
119
	 *
120
	 * @return PEM PRIVATE KEY
121
	 */
122 16
	public function toPEM() {
123 16
		$n = $this->modulusParameter()
124 16
			->number()
125 16
			->base10();
126 16
		$e = $this->exponentParameter()
127 16
			->number()
128 16
			->base10();
129 16
		$d = $this->privateExponentParameter()
130 16
			->number()
131 16
			->base10();
132 16
		$p = $this->firstPrimeFactorParameter()
133 16
			->number()
134 16
			->base10();
135 16
		$q = $this->secondPrimeFactorParameter()
136 16
			->number()
137 16
			->base10();
138 16
		$dp = $this->firstFactorCRTExponentParameter()
139 16
			->number()
140 16
			->base10();
141 16
		$dq = $this->secondFactorCRTExponentParameter()
142 16
			->number()
143 16
			->base10();
144 16
		$qi = $this->firstCRTCoefficientParameter()
145 16
			->number()
146 16
			->base10();
147 16
		$pk = new RSAPrivateKey($n, $e, $d, $p, $q, $dp, $dq, $qi);
148 16
		$pki = new PrivateKeyInfo(new RSAEncryptionAlgorithmIdentifier(), 
149 16
			$pk->toDER());
150 16
		return $pki->toPEM();
151
	}
152
}
153