Completed
Push — master ( 12250a...423c81 )
by Florent
04:39
created

JWKFinderManager::addKey()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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