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

KeyAlgorithmFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 91
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A algoByKeys() 0 10 3
A algoByKey() 0 5 1
A _algoClassByName() 0 7 2
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\JWKSet;
11
use Sop\JWX\JWT\Header\Header;
12
13
/**
14
 * Factory class to construct key management algorithm instances.
15
 */
16
class KeyAlgorithmFactory
17
{
18
    /**
19
     * Mapping from algorithm name to class name.
20
     *
21
     * @internal
22
     *
23
     * @var array
24
     */
25
    const MAP_ALGO_TO_CLASS = [
26
        JWA::ALGO_A128KW => A128KWAlgorithm::class,
27
        JWA::ALGO_A192KW => A192KWAlgorithm::class,
28
        JWA::ALGO_A128KW => A256KWAlgorithm::class,
29
        JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class,
30
        JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class,
31
        JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class,
32
        JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class,
33
        JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class,
34
        JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class,
35
        JWA::ALGO_DIR => DirectCEKAlgorithm::class,
36
        JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class,
37
        JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class,
38
    ];
39
    /**
40
     * Header.
41
     *
42
     * @var Header
43
     */
44
    protected $_header;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param Header $header
50
     */
51 12
    public function __construct(Header $header)
52
    {
53 12
        $this->_header = $header;
54 12
    }
55
56
    /**
57
     * Get key management algorithm by given JWK.
58
     *
59
     * @param JWK $jwk
60
     *
61
     * @return KeyManagementAlgorithm
62
     */
63 9
    public function algoByKey(JWK $jwk): KeyManagementAlgorithm
64
    {
65 9
        $alg = JWA::deriveAlgorithmName($this->_header, $jwk);
66 9
        $cls = self::_algoClassByName($alg);
67 8
        return $cls::fromJWK($jwk, $this->_header);
68
    }
69
70
    /**
71
     * Get key management algorithm using a matching key from given JWK set.
72
     *
73
     * @param JWKSet $set
74
     *
75
     * @throws \UnexpectedValueException If a key cannot be found
76
     *
77
     * @return KeyManagementAlgorithm
78
     */
79 7
    public function algoByKeys(JWKSet $set): KeyManagementAlgorithm
80
    {
81 7
        if (!$this->_header->hasKeyID()) {
82 1
            throw new \UnexpectedValueException('No key ID paremeter.');
83
        }
84 6
        $id = $this->_header->keyID()->value();
85 6
        if (!$set->hasKeyID($id)) {
86 2
            throw new \UnexpectedValueException("No key for ID '{$id}'.");
87
        }
88 4
        return $this->algoByKey($set->keyByID($id));
89
    }
90
91
    /**
92
     * Get the algorithm implementation class name by an algorithm name.
93
     *
94
     * @param string $alg Algorithm name
95
     *
96
     * @throws \UnexpectedValueException
97
     *
98
     * @return string Class name
99
     */
100 9
    private static function _algoClassByName(string $alg): string
101
    {
102 9
        if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
103 1
            throw new \UnexpectedValueException(
104 1
                "Algorithm '{$alg}' not supported.");
105
        }
106 8
        return self::MAP_ALGO_TO_CLASS[$alg];
107
    }
108
}
109