Completed
Push — master ( 668fd8...cdc0a6 )
by Florent
12:11
created

JWKFinderManager::isDirectKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Finder;
13
14
/**
15
 */
16
final class JWKFinderManager implements JWKFinderManagerInterface
17
{
18
    /**
19
     * @var \Jose\Finder\JWKFinderInterface[]
20
     */
21
    private $finders = [];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function addJWKFinder(JWKFinderInterface $finder)
27
    {
28
        $this->finders[] = $finder;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function findJWK(array $header, $key_type)
35
    {
36
        $keys = ['keys' => []];
37
        foreach ($this->finders as $finder) {
38
            $result = $finder->findJWK($header);
39
            if (is_array($result)) {
40
                $this->addKey($keys, $result, $key_type);
41
            }
42
        }
43
44
        return $keys;
45
    }
46
47
    /**
48
     * @param array $keys
49
     * @param array $result
50
     * @param int   $key_type
51
     */
52
    private function addKey(array &$keys, array $result, $key_type)
53
    {
54
        if (array_key_exists('keys', $result)) {
55
            foreach ($result['keys'] as $key) {
56
                $this->addKey($keys, $key, $key_type);
57
            }
58
        } else {
59
            if (true === $this->isKeySearched($result, $key_type)) {
60
                $keys['keys'][] = $result;
61
            }
62
        }
63
    }
64
65
    /**
66
     * @param array $key
67
     * @param int   $key_type
68
     *
69
     * @return bool
70
     */
71
    private function isKeySearched(array $key, $key_type)
72
    {
73
        if ($key_type & self::KEY_TYPE_DIRECT && true === $this->isDirectKey($key)) {
74
            return true;
75
        }
76
        if ($key_type & self::KEY_TYPE_NONE && true === $this->isNoneKey($key)) {
77
            return true;
78
        }
79
        if ($key_type & self::KEY_TYPE_SYMMETRIC && true === $this->isSymmetricKey($key)) {
80
            return true;
81
        }
82
        if ($key_type & self::KEY_TYPE_PUBLIC && true === $this->isPublicKey($key)) {
83
            return true;
84
        }
85
        if ($key_type & self::KEY_TYPE_PRIVATE && true === $this->isPrivateKey($key)) {
86
            return true;
87
        }
88
89
        return false;
90
    }
91
92
    /**
93
     * @param array $key
94
     *
95
     * @return bool
96
     */
97
    private function isDirectKey(array $key)
98
    {
99
        return array_key_exists('kty', $key) && 'dir' === $key['kty'];
100
    }
101
102
    /**
103
     * @param array $key
104
     *
105
     * @return bool
106
     */
107
    private function isNoneKey(array $key)
108
    {
109
        return array_key_exists('kty', $key) && 'none' === $key['kty'];
110
    }
111
112
    /**
113
     * @param array $key
114
     *
115
     * @return bool
116
     */
117
    private function isSymmetricKey(array $key)
118
    {
119
        return array_key_exists('kty', $key) && 'oct' === $key['kty'];
120
    }
121
122
    /**
123
     * @param array $key
124
     *
125
     * @return bool
126
     */
127
    private function isPrivateKey(array $key)
128
    {
129
        if (!array_key_exists('kty', $key) || !in_array($key['kty'], ['RSA', 'EC'])) {
130
            return false;
131
        }
132
133
        return array_key_exists('d', $key);
134
    }
135
136
    /**
137
     * @param array $key
138
     *
139
     * @return bool
140
     */
141
    private function isPublicKey(array $key)
142
    {
143
        if (!array_key_exists('kty', $key) || !in_array($key['kty'], ['RSA', 'EC'])) {
144
            return false;
145
        }
146
147
        return !array_key_exists('d', $key);
148
    }
149
}
150