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

SignatureAlgorithmFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 26
dl 0
loc 88
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 _algoClassByName() 0 7 2
A algoByKey() 0 5 1
A algoByKeys() 0 10 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWS\Algorithm;
6
7
use Sop\JWX\JWA\JWA;
8
use Sop\JWX\JWK\JWK;
9
use Sop\JWX\JWK\JWKSet;
10
use Sop\JWX\JWS\SignatureAlgorithm;
11
use Sop\JWX\JWT\Header\Header;
12
13
/**
14
 * Factory class to construct signature algorithm instances.
15
 */
16
class SignatureAlgorithmFactory
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_HS256 => HS256Algorithm::class,
27
        JWA::ALGO_HS384 => HS384Algorithm::class,
28
        JWA::ALGO_HS512 => HS512Algorithm::class,
29
        JWA::ALGO_RS256 => RS256Algorithm::class,
30
        JWA::ALGO_RS384 => RS384Algorithm::class,
31
        JWA::ALGO_RS512 => RS512Algorithm::class,
32
        JWA::ALGO_ES256 => ES256Algorithm::class,
33
        JWA::ALGO_ES384 => ES384Algorithm::class,
34
        JWA::ALGO_ES512 => ES512Algorithm::class,
35
    ];
36
    /**
37
     * Header.
38
     *
39
     * @var Header
40
     */
41
    protected $_header;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param Header $header
47
     */
48 14
    public function __construct(Header $header)
49
    {
50 14
        $this->_header = $header;
51 14
    }
52
53
    /**
54
     * Get signature algorithm by given JWK.
55
     *
56
     * @param JWK $jwk
57
     *
58
     * @return SignatureAlgorithm
59
     */
60 11
    public function algoByKey(JWK $jwk): SignatureAlgorithm
61
    {
62 11
        $alg = JWA::deriveAlgorithmName($this->_header, $jwk);
63 11
        $cls = self::_algoClassByName($alg);
64 10
        return $cls::fromJWK($jwk, $this->_header);
65
    }
66
67
    /**
68
     * Get signature algorithm using a matching key from given JWK set.
69
     *
70
     * @param JWKSet $set
71
     *
72
     * @throws \UnexpectedValueException If a key cannot be found
73
     *
74
     * @return SignatureAlgorithm
75
     */
76 7
    public function algoByKeys(JWKSet $set): SignatureAlgorithm
77
    {
78 7
        if (!$this->_header->hasKeyID()) {
79 1
            throw new \UnexpectedValueException('No key ID paremeter.');
80
        }
81 6
        $id = $this->_header->keyID()->value();
82 6
        if (!$set->hasKeyID($id)) {
83 2
            throw new \UnexpectedValueException("No key for ID '{$id}'.");
84
        }
85 4
        return $this->algoByKey($set->keyByID($id));
86
    }
87
88
    /**
89
     * Get the algorithm implementation class name by an algorithm name.
90
     *
91
     * @param string $alg Algorithm name
92
     *
93
     * @throws \UnexpectedValueException
94
     *
95
     * @return string Class name
96
     */
97 11
    private static function _algoClassByName(string $alg): string
98
    {
99 11
        if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
100 1
            throw new \UnexpectedValueException(
101 1
                "Algorithm '{$alg}' not supported.");
102
        }
103 10
        return self::MAP_ALGO_TO_CLASS[$alg];
104
    }
105
}
106