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

KeyManagementAlgorithm   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 17
dl 0
loc 125
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A headerParameters() 0 7 2
A withKeyID() 0 5 1
A encrypt() 0 6 2
A decrypt() 0 6 2
A fromJWK() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWE;
6
7
use Sop\JWX\JWE\KeyAlgorithm\KeyAlgorithmFactory;
8
use Sop\JWX\JWK\JWK;
9
use Sop\JWX\JWT\Header\Header;
10
use Sop\JWX\JWT\Header\HeaderParameters;
11
use Sop\JWX\JWT\Parameter\AlgorithmParameterValue;
12
use Sop\JWX\JWT\Parameter\KeyIDParameter;
13
14
/**
15
 * Base class for algorithms used for CEK management for the content encryption
16
 * algorithms.
17
 */
18
abstract class KeyManagementAlgorithm implements AlgorithmParameterValue, HeaderParameters
19
{
20
    /**
21
     * ID of the key used by the algorithm.
22
     *
23
     * If set, KeyID parameter shall be automatically inserted into JWE's
24
     * header.
25
     *
26
     * @var null|string
27
     */
28
    protected $_keyID;
29
30
    /**
31
     * Encrypt a key to be inserted into JWE header.
32
     *
33
     * @param string      $cek    Content encryption key
34
     * @param null|Header $header Optional reference to the Header variable,
35
     *                            which may be updated to contain parameters
36
     *                            specific to this encrypt invocation.
37
     *                            If the variable is referenced, but is a null,
38
     *                            it shall be initialized to an empty Header.
39
     *
40
     * @throws \RuntimeException For generic errors
41
     *
42
     * @return string Encrypted key
43
     */
44 49
    final public function encrypt(string $cek, Header &$header = null): string
45
    {
46 49
        if (!isset($header)) {
47 34
            $header = new Header();
48
        }
49 49
        return $this->_encryptKey($cek, $header);
50
    }
51
52
    /**
53
     * Decrypt a CEK from the encrypted data.
54
     *
55
     * @param string      $data   Encrypted key
56
     * @param null|Header $header Optional header containing parameters
57
     *                            required to decrypt the key
58
     *
59
     * @throws \RuntimeException For generic errors
60
     *
61
     * @return string Content encryption key
62
     */
63 44
    final public function decrypt(string $data, ?Header $header = null): string
64
    {
65 44
        if (!isset($header)) {
66 27
            $header = new Header();
67
        }
68 44
        return $this->_decryptKey($data, $header);
69
    }
70
71
    /**
72
     * Get content encryption key for the encryption.
73
     *
74
     * Returned key may be random depending on the key management algorithm.
75
     *
76
     * @param int $length Required key size in bytes
77
     *
78
     * @return string
79
     */
80
    abstract public function cekForEncryption(int $length): string;
81
82
    /**
83
     * Initialize key management algorithm from a JWK and a header.
84
     *
85
     * @param JWK    $jwk
86
     * @param Header $header
87
     *
88
     * @return KeyManagementAlgorithm
89
     */
90 3
    public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm
91
    {
92 3
        $factory = new KeyAlgorithmFactory($header);
93 3
        return $factory->algoByKey($jwk);
94
    }
95
96
    /**
97
     * Get self with key ID.
98
     *
99
     * @param null|string $id Key ID or null to remove
100
     *
101
     * @return self
102
     */
103 3
    public function withKeyID(?string $id): self
104
    {
105 3
        $obj = clone $this;
106 3
        $obj->_keyID = $id;
107 3
        return $obj;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 19
    public function headerParameters(): array
114
    {
115 19
        $params = [];
116 19
        if (isset($this->_keyID)) {
117 3
            $params[] = new KeyIDParameter($this->_keyID);
1 ignored issue
show
Bug introduced by
It seems like $this->_keyID can also be of type null; however, parameter $id of Sop\JWX\JWT\Parameter\Ke...arameter::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
            $params[] = new KeyIDParameter(/** @scrutinizer ignore-type */ $this->_keyID);
Loading history...
118
        }
119 19
        return $params;
120
    }
121
122
    /**
123
     * Encrypt a key.
124
     *
125
     * @param string $key    Key to be encrypted
126
     * @param Header $header Reference to the Header variable, that shall
127
     *                       be updated to contain parameters specific to the encryption
128
     *
129
     * @return string Ciphertext
130
     */
131
    abstract protected function _encryptKey(string $key, Header &$header): string;
132
133
    /**
134
     * Decrypt a key.
135
     *
136
     * @param string $ciphertext Ciphertext of the encrypted key
137
     * @param Header $header     Header possibly containing encoding specific
138
     *                           parameters
139
     *
140
     * @return string Plaintext key
141
     */
142
    abstract protected function _decryptKey(string $ciphertext, Header $header): string;
143
}
144