Completed
Pull Request — master (#20)
by Дмитрий
09:49 queued 07:44
created

JWK   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 101
ccs 0
cts 52
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 4
B getPublicKey() 0 35 1
A encodeLength() 0 9 2
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
7
namespace SocialConnect\OpenIDConnect;
8
9
use SocialConnect\OpenIDConnect\Exception\InvalidJWK;
10
11
class JWK
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $kty;
17
18
    /**
19
     * @var string
20
     */
21
    protected $n;
1 ignored issue
show
Comprehensibility introduced by
Avoid variables with short names like $n. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
22
23
    /**
24
     * @var string
25
     */
26
    protected $e;
1 ignored issue
show
Comprehensibility introduced by
Avoid variables with short names like $e. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
27
28
    /**
29
     * @param array $parameters
30
     * @throws InvalidJWK
31
     */
32
    public function __construct($parameters)
33
    {
34
        if (!isset($parameters['kty'])) {
35
            throw new InvalidJWK('Unknown kty');
36
        }
37
38
        $this->kty = $parameters['kty'];
39
40
        if (!isset($parameters['n'])) {
41
            throw new InvalidJWK('Unknown n');
42
        }
43
44
        $this->n = $parameters['n'];
45
46
        if (!isset($parameters['e'])) {
47
            throw new InvalidJWK('Unknown e');
48
        }
49
50
        $this->e = $parameters['e'];
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getPublicKey()
57
    {
58
        $modulus = JWT::urlsafeB64Decode($this->n);
59
        $publicExponent = JWT::urlsafeB64Decode($this->e);
60
61
        $components = array(
62
            'modulus' => pack('Ca*a*', 2, self::encodeLength(strlen($modulus)), $modulus),
63
            'publicExponent' => pack('Ca*a*', 2, self::encodeLength(strlen($publicExponent)), $publicExponent)
64
        );
65
66
        $publicKey = pack(
67
            'Ca*a*a*',
68
            48,
69
            self::encodeLength(strlen($components['modulus']) + strlen($components['publicExponent'])),
70
            $components['modulus'],
71
            $components['publicExponent']
72
        );
73
74
        // sequence(oid(1.2.840.113549.1.1.1), null)) = rsaEncryption.
75
        $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
76
        $publicKey = chr(0) . $publicKey;
77
        $publicKey = chr(3) . self::encodeLength(strlen($publicKey)) . $publicKey;
78
        $publicKey = pack(
79
            'Ca*a*',
80
            48,
81
            self::encodeLength(strlen($rsaOID . $RSAPublicKey)),
0 ignored issues
show
Bug introduced by
The variable $RSAPublicKey does not exist. Did you mean $publicKey?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
82
            $rsaOID . $publicKey
83
        );
84
85
        $publicKey = "-----BEGIN PUBLIC KEY-----\r\n" .
86
            chunk_split(base64_encode($publicKey), 64) .
87
            '-----END PUBLIC KEY-----';
88
89
        return $publicKey;
90
    }
91
92
    /**
93
     * DER-encode the length
94
     *
95
     * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4.  See
96
     * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information.
97
     *
98
     * @access private
99
     * @param int $length
100
     * @return string
101
     */
102
    private static function encodeLength($length)
103
    {
104
        if ($length <= 0x7F) {
105
            return chr($length);
106
        }
107
108
        $temp = ltrim(pack('N', $length), chr(0));
109
        return pack('Ca*', 0x80 | strlen($temp), $temp);
110
    }
111
}
112