|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
10
|
|
|
use Symfony\Component\Validator\Tests\Fixtures\Reference; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This is the class that loads and manages your bundle configuration. |
|
14
|
|
|
* |
|
15
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
|
16
|
|
|
*/ |
|
17
|
|
|
class Ma27ApiKeyAuthenticationExtension extends Extension |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
|
23
|
|
|
{ |
|
24
|
6 |
|
$configuration = new Configuration(); |
|
25
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
26
|
|
|
|
|
27
|
6 |
|
$container->setParameter('ma27_api_key_authentication.key_header', $config['key_header']); |
|
28
|
6 |
|
$container->setParameter('ma27_api_key_authentication.model_name', $config['user']['model_name']); |
|
29
|
6 |
|
$container->setParameter('ma27_api_key_authentication.object_manager', $config['user']['object_manager']); |
|
30
|
6 |
|
$container->setParameter('ma27_api_key_authentication.property.apiKeyLength', $config['user']['api_key_length']); |
|
31
|
|
|
|
|
32
|
6 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
33
|
6 |
|
$this->loadPassword($container, $config['user']['password'], $loader); |
|
34
|
6 |
|
$this->loadServices($loader); |
|
35
|
6 |
|
$this->loadApiKeyPurger($container, $loader, $config['api_key_purge']); |
|
36
|
6 |
|
$this->overrideServices($container, $config['services']); |
|
37
|
|
|
|
|
38
|
6 |
|
$container->setParameter('ma27_api_key_authentication.response_values', $config['response']); |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Loads the password strategy. |
|
43
|
|
|
* |
|
44
|
|
|
* @param ContainerBuilder $container |
|
45
|
|
|
* @param string $passwordConfig |
|
46
|
|
|
* @param Loader\YamlFileLoader $loader |
|
47
|
|
|
*/ |
|
48
|
6 |
|
private function loadPassword(ContainerBuilder $container, $passwordConfig, Loader\YamlFileLoader $loader) |
|
49
|
|
|
{ |
|
50
|
6 |
|
$container->setParameter('ma27_api_key_authentication.password_hashing_service', $passwordConfig['strategy']); |
|
51
|
6 |
|
$container->setParameter('ma27_api_key_authentication.password_hasher.phpass.iteration_length', 8); |
|
52
|
6 |
|
$loader->load('hashers.yml'); |
|
53
|
6 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Loads all internal services. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Loader\YamlFileLoader $loader |
|
59
|
|
|
*/ |
|
60
|
6 |
|
private function loadServices(Loader\YamlFileLoader $loader) |
|
61
|
|
|
{ |
|
62
|
6 |
|
foreach (array('security_key', 'authentication', 'security', 'annotation') as $file) { |
|
63
|
6 |
|
$loader->load(sprintf('%s.yml', $file)); |
|
64
|
6 |
|
} |
|
65
|
6 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Loads the purger job command into the container. |
|
69
|
|
|
* |
|
70
|
|
|
* @param ContainerBuilder $container |
|
71
|
|
|
* @param Loader\YamlFileLoader $loader |
|
72
|
|
|
* @param string[] $purgerConfig |
|
73
|
|
|
*/ |
|
74
|
6 |
|
private function loadApiKeyPurger(ContainerBuilder $container, Loader\YamlFileLoader $loader, array $purgerConfig) |
|
75
|
|
|
{ |
|
76
|
6 |
|
if ($this->isConfigEnabled($container, $purgerConfig)) { |
|
77
|
2 |
|
$loader->load('session_cleanup.yml'); |
|
78
|
|
|
|
|
79
|
2 |
|
if ($purgerConfig['log_state']) { |
|
80
|
1 |
|
@trigger_error('The options `api_key_purge.log_state` and the corresponding logger support are deprecated and will be dropped/removed in 2.0!', E_USER_DEPRECATED); |
|
|
|
|
|
|
81
|
1 |
|
$container->setParameter( |
|
82
|
1 |
|
'ma27_api_key_authentication.logger', |
|
83
|
1 |
|
$purgerConfig['logger_service'] |
|
84
|
1 |
|
); |
|
85
|
|
|
|
|
86
|
1 |
|
$definition = $container->getDefinition('ma27_api_key_authentication.cleanup_command'); |
|
87
|
1 |
|
$definition->addArgument(new Reference($container->getParameter('ma27_api_key_authentication.logger'))); |
|
|
|
|
|
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
if ($this->isConfigEnabled($container, $purgerConfig['last_action_listener'])) { |
|
|
|
|
|
|
91
|
2 |
|
$loader->load('last_action_listener.yml'); |
|
92
|
2 |
|
} |
|
93
|
2 |
|
} |
|
94
|
6 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Processes the service override configuration into the container. |
|
98
|
|
|
* |
|
99
|
|
|
* @param ContainerBuilder $container |
|
100
|
|
|
* @param array $services |
|
101
|
|
|
*/ |
|
102
|
6 |
|
private function overrideServices(ContainerBuilder $container, array $services) |
|
103
|
|
|
{ |
|
104
|
6 |
|
$semanticServiceReplacements = array_filter($services); |
|
105
|
6 |
|
if (!empty($semanticServiceReplacements)) { |
|
106
|
|
|
$serviceConfig = array( |
|
107
|
1 |
|
'auth_handler' => 'ma27_api_key_authentication.auth_handler', |
|
108
|
1 |
|
'key_factory' => 'ma27_api_key_authentication.key_factory', |
|
109
|
1 |
|
); |
|
110
|
|
|
|
|
111
|
1 |
|
foreach ($serviceConfig as $configIndex => $replaceableServiceId) { |
|
112
|
1 |
|
if (!isset($semanticServiceReplacements[$configIndex]) || null === $serviceId = $semanticServiceReplacements[$configIndex]) { |
|
113
|
1 |
|
continue; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
$container->removeDefinition($replaceableServiceId); |
|
117
|
1 |
|
$container->setAlias($replaceableServiceId, new Alias($serviceId)); |
|
118
|
1 |
|
} |
|
119
|
1 |
|
} |
|
120
|
6 |
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: