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 ( 59f4b4...e271c1 )
by Joni
03:51
created

DirectCEKAlgorithm   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 103
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A _decryptKey() 0 7 2
A fromJWK() 0 8 2
A _encryptKey() 0 6 2
A __construct() 0 3 1
A algorithmParamValue() 0 3 1
A cekForEncryption() 0 6 2
A cek() 0 3 1
A headerParameters() 0 4 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
     * @param JWK    $jwk
42
     * @param Header $header
43
     *
44
     * @throws \UnexpectedValueException
45
     *
46
     * @return self
47
     */
48 12
    public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm
49
    {
50 12
        $jwk = SymmetricKeyJWK::fromJWK($jwk);
51 12
        $alg = JWA::deriveAlgorithmName($header);
52 12
        if (JWA::ALGO_DIR !== $alg) {
53 1
            throw new \UnexpectedValueException("Invalid algorithm '{$alg}'.");
54
        }
55 11
        return new self($jwk->key());
56
    }
57
58
    /**
59
     * Get content encryption key.
60
     *
61
     * @return string
62
     */
63 2
    public function cek(): string
64
    {
65 2
        return $this->_cek;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 8
    public function cekForEncryption(int $length): string
72
    {
73 8
        if (strlen($this->_cek) !== $length) {
74 1
            throw new \UnexpectedValueException('Invalid key length.');
75
        }
76 7
        return $this->_cek;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 18
    public function algorithmParamValue(): string
83
    {
84 18
        return JWA::ALGO_DIR;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 8
    public function headerParameters(): array
91
    {
92 8
        return array_merge(parent::headerParameters(),
93 8
            [AlgorithmParameter::fromAlgorithm($this)]);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 8
    protected function _encryptKey(string $key, Header &$header): string
100
    {
101 8
        if ($key !== $this->_cek) {
102 1
            throw new \LogicException("Content encryption key doesn't match.");
103
        }
104 7
        return '';
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 10
    protected function _decryptKey(string $ciphertext, Header $header): string
111
    {
112 10
        if ('' !== $ciphertext) {
113 1
            throw new \UnexpectedValueException(
114 1
                'Encrypted key must be an empty octet sequence.');
115
        }
116 9
        return $this->_cek;
117
    }
118
}
119