MixedPublicAndPrivateKeys::analyze()   B
last analyzed

Complexity

Conditions 9
Paths 17

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.0555
c 0
b 0
f 0
cc 9
nc 17
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\KeyManagement\Analyzer;
15
16
use Jose\Component\Core\JWKSet;
17
18
final class MixedPublicAndPrivateKeys implements KeysetAnalyzer
19
{
20
    public function analyze(JWKSet $jwkset, MessageBag $bag): void
21
    {
22
        if (0 === $jwkset->count()) {
23
            return;
24
        }
25
26
        $hasPublicKeys = false;
27
        $hasPrivateKeys = false;
28
29
        foreach ($jwkset as $jwk) {
30
            switch ($jwk->get('kty')) {
31
                case 'OKP':
32
                case 'RSA':
33
                case 'EC':
34
                    if ($jwk->has('d')) {
35
                        $hasPrivateKeys = true;
36
                    } else {
37
                        $hasPublicKeys = true;
38
                    }
39
40
                    break;
41
            }
42
        }
43
44
        if ($hasPrivateKeys && $hasPublicKeys) {
45
            $bag->add(Message::high('This key set mixes public and private keys.'));
46
        }
47
    }
48
}
49