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.

DirectCEKAlgorithm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWE\KeyAlgorithm;
6
7
use Sop\JWX\JWA\JWA;
8
use Sop\JWX\JWE\KeyManagementAlgorithm;
9
use Sop\JWX\JWK\JWK;
10
use Sop\JWX\JWK\Symmetric\SymmetricKeyJWK;
11
use Sop\JWX\JWT\Header\Header;
12
use Sop\JWX\JWT\Parameter\AlgorithmParameter;
13
14
/**
15
 * Algorithm to carry CEK in plaintext.
16
 *
17
 * @see https://tools.ietf.org/html/rfc7518#section-4.5
18
 */
19
class DirectCEKAlgorithm extends KeyManagementAlgorithm
20
{
21
    /**
22
     * Content encryption key.
23
     *
24
     * @var string
25
     */
26
    protected $_cek;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param string $cek Content encryption key
32
     */
33 16
    public function __construct(string $cek)
34
    {
35 16
        $this->_cek = $cek;
36 16
    }
37
38
    /**
39
     * Initialize from JWK.
40
     *
41
     * @throws \UnexpectedValueException
42
     *
43
     * @return self
44
     */
45 12
    public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm
46
    {
47 12
        $jwk = SymmetricKeyJWK::fromJWK($jwk);
48 12
        $alg = JWA::deriveAlgorithmName($header);
49 12
        if (JWA::ALGO_DIR !== $alg) {
50 1
            throw new \UnexpectedValueException("Invalid algorithm '{$alg}'.");
51
        }
52 11
        return new self($jwk->key());
53
    }
54
55
    /**
56
     * Get content encryption key.
57
     */
58 2
    public function cek(): string
59
    {
60 2
        return $this->_cek;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 8
    public function cekForEncryption(int $length): string
67
    {
68 8
        if (strlen($this->_cek) !== $length) {
69 1
            throw new \UnexpectedValueException('Invalid key length.');
70
        }
71 7
        return $this->_cek;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 18
    public function algorithmParamValue(): string
78
    {
79 18
        return JWA::ALGO_DIR;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 8
    public function headerParameters(): array
86
    {
87 8
        return array_merge(parent::headerParameters(),
88 8
            [AlgorithmParameter::fromAlgorithm($this)]);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 8
    protected function _encryptKey(string $key, Header &$header): string
95
    {
96 8
        if ($key !== $this->_cek) {
97 1
            throw new \LogicException("Content encryption key doesn't match.");
98
        }
99 7
        return '';
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 10
    protected function _decryptKey(string $ciphertext, Header $header): string
106
    {
107 10
        if ('' !== $ciphertext) {
108 1
            throw new \UnexpectedValueException(
109 1
                'Encrypted key must be an empty octet sequence.');
110
        }
111 9
        return $this->_cek;
112
    }
113
}
114