Completed
Push — master ( a6e833...2ffd36 )
by Florent
02:08
created

EasyJWTLoaderSource   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 24
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 181
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A createService() 0 3 1
A getNodeDefinition() 0 20 1
A prepend() 0 12 3
A createServiceConfiguration() 0 9 1
A createVerifierServiceConfiguration() 0 12 3
A createDecrypterServiceConfiguration() 0 17 4
A createCheckerServiceConfiguration() 0 12 2
A createJWTLoaderServiceConfiguration() 0 16 3
B isEncryptionSupportEnabled() 0 12 5
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 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 SpomkyLabs\JoseBundle\DependencyInjection\Source;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
final class EasyJWTLoaderSource implements SourceInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function getName()
23
    {
24
        return 'easy_jwt_loader';
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function createService($name, array $config, ContainerBuilder $container)
31
    {
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getNodeDefinition(ArrayNodeDefinition $node)
38
    {
39
        $node
40
            ->children()
41
                ->arrayNode('easy_jwt_loader')
42
                    ->useAttributeAsKey('name')
43
                    ->prototype('array')
44
                        ->children()
45
                            ->arrayNode('signature_algorithms')->isRequired()->cannotBeEmpty()->prototype('scalar')->end()->end()
46
                            ->arrayNode('key_encryption_algorithms')->defaultValue([])->prototype('scalar')->end()->end()
47
                            ->arrayNode('content_encryption_algorithms')->defaultValue([])->prototype('scalar')->end()->end()
48
                            ->arrayNode('compression_methods')->defaultValue(['DEF'])->prototype('scalar')->end()->end()
49
                            ->arrayNode('claim_checkers')->defaultValue([])->prototype('scalar')->end()->end()
50
                            ->arrayNode('header_checkers')->defaultValue([])->prototype('scalar')->end()->end()
51
                            ->scalarNode('logger')->defaultNull()->end()
52
                        ->end()
53
                    ->end()
54
                ->end()
55
            ->end();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function prepend(ContainerBuilder $container, array $config)
62
    {
63
        if (false === array_key_exists($this->getName(), $config)) {
64
            return;
65
        }
66
67
        foreach ($config[$this->getName()] as $id=>$section) {
68
            $config = $this->createServiceConfiguration($config, $id, $section);
69
        }
70
71
        return $config;
72
    }
73
74
    /**
75
     * @param array  $config
76
     * @param string $id
77
     * @param array  $section
78
     *
79
     * @return array
80
     */
81
    private function createServiceConfiguration(array $config, $id, array $section)
82
    {
83
        $config = $this->createVerifierServiceConfiguration($config, $id, $section);
84
        $config = $this->createDecrypterServiceConfiguration($config, $id, $section);
85
        $config = $this->createJWTLoaderServiceConfiguration($config, $id, $section);
86
        $config = $this->createCheckerServiceConfiguration($config, $id, $section);
87
        
88
        return $config;
89
    }
90
91
    /**
92
     * @param array  $config
93
     * @param string $id
94
     * @param array  $section
95
     *
96
     * @return array
97
     */
98
    private function createVerifierServiceConfiguration(array $config, $id, array $section)
99
    {
100
        $config['verifiers'] = array_merge(
101
            array_key_exists('verifiers', $config) ? $config['verifiers'] : [],
102
            [$id => [
103
                'algorithms' => $section['signature_algorithms'],
104
                'logger' => array_key_exists('logger', $section) ? $section['logger'] : null,
105
            ]]
106
        );
107
108
        return $config;
109
    }
110
111
    /**
112
     * @param array  $config
113
     * @param string $id
114
     * @param array  $section
115
     *
116
     * @return array
117
     */
118
    private function createDecrypterServiceConfiguration(array $config, $id, array $section)
119
    {
120
        if (false === $this->isEncryptionSupportEnabled($section)) {
121
            return $config;
122
        }
123
        $config['decrypters'] = array_merge(
124
            array_key_exists('decrypters', $config) ? $config['decrypters'] : [],
125
            [$id => [
126
                'key_encryption_algorithms' => $section['key_encryption_algorithms'],
127
                'content_encryption_algorithms' => $section['content_encryption_algorithms'],
128
                'compression_methods' => $section['compression_methods'],
129
                'logger' => array_key_exists('logger', $section) ? $section['logger'] : null,
130
            ]]
131
        );
132
133
        return $config;
134
    }
135
136
    /**
137
     * @param array  $config
138
     * @param string $id
139
     * @param array  $section
140
     *
141
     * @return array
142
     */
143
    private function createCheckerServiceConfiguration(array $config, $id, array $section)
144
    {
145
        $config['checkers'] = array_merge(
146
            array_key_exists('checkers', $config) ? $config['checkers'] : [],
147
            [$id => [
148
                'claims' => $section['claim_checkers'],
149
                'headers' => $section['header_checkers'],
150
            ]]
151
        );
152
153
        return $config;
154
    }
155
156
    /**
157
     * @param array  $config
158
     * @param string $id
159
     * @param array  $section
160
     *
161
     * @return array
162
     */
163
    private function createJWTLoaderServiceConfiguration(array $config, $id, array $section)
164
    {
165
        $service = [
166
            'verifier' => sprintf('jose.verifier.%s', $id),
167
            'checker' => sprintf('jose.checker.%s', $id),
168
        ];
169
        if (true === $this->isEncryptionSupportEnabled($section)) {
170
            $service['decrypter'] = sprintf('jose.decrypter.%s', $id);
171
        }
172
        $config['jwt_loaders'] = array_merge(
173
            array_key_exists('jwt_loaders', $config) ? $config['jwt_loaders'] : [],
174
            [$id => $service]
175
        );
176
177
        return $config;
178
    }
179
180
    /**
181
     * @param array $section
182
     *
183
     * @return bool
184
     */
185
    private function isEncryptionSupportEnabled(array $section)
186
    {
187
        if (true === empty($section['key_encryption_algorithms']) && true === empty($section['content_encryption_algorithms'])) {
188
            return false;
189
        }
190
191
        if (true === empty($section['key_encryption_algorithms']) || true === empty($section['content_encryption_algorithms'])) {
192
            throw new \LogicException('Both key encryption algorithms and content encryption algorithms must be set to enable the encryption support.');
193
        }
194
195
        return true;
196
    }
197
}
198