Completed
Push — master ( b9975d...05326a )
by Florent
06:18 queued 51s
created

SpomkyLabsJoseBundleExtension::createJWKSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 13
nc 2
nop 3
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;
13
14
use Symfony\Component\Config\Definition\Processor;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
final class SpomkyLabsJoseBundleExtension extends Extension
23
{
24
    /**
25
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[]
26
     */
27
    private $jwk_sources;
28
29
    /**
30
     * @var string
31
     */
32
    private $alias;
33
34
    /**
35
     * @param string $alias
36
     */
37
    public function __construct($alias)
38
    {
39
        $this->alias = $alias;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function load(array $configs, ContainerBuilder $container)
46
    {
47
        $processor = new Processor();
48
49
        $config = $processor->processConfiguration(
50
            $this->getConfiguration($configs, $container),
51
            $configs
52
        );
53
54
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
55
        $services = $this->getXmlFileToLoad($config);
56
        foreach ($services as $basename) {
57
            $loader->load(sprintf('%s.xml', $basename));
58
        }
59
60
        $this->initConfiguration($container, $config);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getConfiguration(array $configs, ContainerBuilder $container)
67
    {
68
        $jwk_sources = $this->createJWKSources();
69
70
        return new Configuration($this->getAlias(), $jwk_sources);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getAlias()
77
    {
78
        return $this->alias;
79
    }
80
81
    /**
82
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
83
     * @param array                                                   $config
84
     */
85
    private function initConfiguration(ContainerBuilder $container, array $config)
86
    {
87
        if (true === $config['storage']['enabled']) {
88
            $container->setParameter($this->getAlias().'.jot.class', $config['storage']['class']);
89
            $container->setAlias($this->getAlias().'.jot.manager', $config['storage']['manager']);
90
        }
91
92
        $parameters = [
93
            'compression_methods',
94
        ];
95
96
        foreach ($parameters as $parameter) {
97
            $container->setParameter($this->getAlias().'.'.$parameter, $config[$parameter]);
98
        }
99
100
        foreach ($config['keys'] as $name => $key) {
101
            $this->createJWK($name, $key, $container, $this->jwk_sources);
102
        }
103
104
        foreach ($config['key_sets'] as $name => $key_set) {
105
            $this->createJWKSet($name, $key_set, $container);
106
        }
107
    }
108
109
    /**
110
     * @param string                                                  $name
111
     * @param array                                                   $config
112
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
113
     */
114
    private function createJWKSet($name, array $config, ContainerBuilder $container)
115
    {
116
        $keys = [];
117
        foreach ($config as $kid) {
118
            $id = sprintf('jose.key.%s', $kid);
119
120
            $keys[] = new Reference($id);
121
        }
122
123
        $service_id = sprintf('jose.key_set.%s', $name);
124
        $definition = new Definition('Jose\Object\JWKSet');
125
        $definition->setFactory([
126
            new Reference('jose.factory.jwk_set'),
127
            'createFromKey',
128
        ]);
129
        $definition->setArguments([
130
            $keys,
131
        ]);
132
133
        $container->setDefinition($service_id, $definition);
134
    }
135
136
    /**
137
     * @param string                                                                    $name
138
     * @param array                                                                     $config
139
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder                   $container
140
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] $jwk_sources
141
     */
142
    private function createJWK($name, array $config, ContainerBuilder $container, array $jwk_sources)
143
    {
144
        foreach ($config as $key => $adapter) {
145
            if (array_key_exists($key, $jwk_sources)) {
146
                $jwk_sources[$key]->create($container, $name, $adapter);
147
148
                return;
149
            }
150
        }
151
        throw new \LogicException(sprintf('The JWK definition "%s" is not configured.', $name));
152
    }
153
154
    /**
155
     * @param array $config
156
     *
157
     * @return string[]
158
     */
159
    private function getXmlFileToLoad(array $config)
160
    {
161
        $services = [
162
            'services',
163
            'compression_methods',
164
            'checkers',
165
        ];
166
167
        if (true === $config['storage']['enabled']) {
168
            $services[] = 'jot';
169
        }
170
171
        return $services;
172
    }
173
174
    private function createJWKSources()
175
    {
176
        if (null !== $this->jwk_sources) {
177
            return $this->jwk_sources;
178
        }
179
180
        // load bundled adapter factories
181
        $tempContainer = new ContainerBuilder();
182
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
183
        $loader->load('jwk_sources.xml');
184
185
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_source');
186
        $jwk_sources = [];
187
        foreach (array_keys($services) as $id) {
188
            $factory = $tempContainer->get($id);
189
            $jwk_sources[str_replace('-', '_', $factory->getKey())] = $factory;
190
        }
191
192
        return $this->jwk_sources = $jwk_sources;
193
    }
194
}
195