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( |
31
|
6 |
|
'ma27_api_key_authentication.property.apiKeyLength', |
32
|
6 |
|
(int) floor($config['user']['api_key_length'] / 2) // TODO to be moved to the `KeyFactory` |
33
|
6 |
|
); |
34
|
|
|
|
35
|
6 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
36
|
6 |
|
$this->loadPassword($container, $config['user']['password'], $loader); |
37
|
6 |
|
$this->loadServices($loader); |
38
|
6 |
|
$this->loadApiKeyPurger($container, $loader, $config['api_key_purge']); |
39
|
6 |
|
$this->overrideServices($container, $config['services']); |
40
|
|
|
|
41
|
6 |
|
$container->setParameter('ma27_api_key_authentication.response_values', $config['response']); |
42
|
6 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Loads the password strategy. |
46
|
|
|
* |
47
|
|
|
* @param ContainerBuilder $container |
48
|
|
|
* @param string $passwordConfig |
49
|
|
|
* @param Loader\YamlFileLoader $loader |
50
|
|
|
*/ |
51
|
6 |
|
private function loadPassword(ContainerBuilder $container, $passwordConfig, Loader\YamlFileLoader $loader) |
52
|
|
|
{ |
53
|
|
|
// TODO refactor this, the `phpass_iteration_length` is specific for a single strategy and therefore it shouldn't be present in the semantic configuration. |
54
|
6 |
|
$container->setParameter( |
55
|
6 |
|
'ma27_api_key_authentication.password_hasher.phpass.iteration_length', |
56
|
6 |
|
isset($passwordConfig['phpass_iteration_length']) ? $passwordConfig['phpass_iteration_length'] : 8 |
57
|
6 |
|
); |
58
|
6 |
|
$loader->load('hashers.yml'); |
59
|
|
|
|
60
|
6 |
|
$container->setParameter( |
61
|
6 |
|
'ma27_api_key_authentication.password_hashing_service', |
62
|
6 |
|
$passwordConfig['strategy'] |
63
|
6 |
|
); |
64
|
6 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Loads all internal services. |
68
|
|
|
* |
69
|
|
|
* @param Loader\YamlFileLoader $loader |
70
|
|
|
*/ |
71
|
6 |
|
private function loadServices(Loader\YamlFileLoader $loader) |
72
|
|
|
{ |
73
|
6 |
|
foreach (array('security_key', 'authentication', 'security', 'annotation') as $file) { |
74
|
6 |
|
$loader->load(sprintf('%s.yml', $file)); |
75
|
6 |
|
} |
76
|
6 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Loads the purger job command into the container. |
80
|
|
|
* |
81
|
|
|
* @param ContainerBuilder $container |
82
|
|
|
* @param Loader\YamlFileLoader $loader |
83
|
|
|
* @param string[] $purgerConfig |
84
|
|
|
*/ |
85
|
6 |
|
private function loadApiKeyPurger(ContainerBuilder $container, Loader\YamlFileLoader $loader, array $purgerConfig) |
86
|
|
|
{ |
87
|
6 |
|
if ($this->isConfigEnabled($container, $purgerConfig)) { |
88
|
2 |
|
$loader->load('session_cleanup.yml'); |
89
|
|
|
|
90
|
2 |
|
if ($purgerConfig['log_state']) { |
91
|
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); |
|
|
|
|
92
|
1 |
|
$container->setParameter( |
93
|
1 |
|
'ma27_api_key_authentication.logger', |
94
|
1 |
|
$purgerConfig['logger_service'] |
95
|
1 |
|
); |
96
|
|
|
|
97
|
1 |
|
$definition = $container->getDefinition('ma27_api_key_authentication.cleanup_command'); |
98
|
1 |
|
$definition->addArgument(new Reference($container->getParameter('ma27_api_key_authentication.logger'))); |
|
|
|
|
99
|
1 |
|
} |
100
|
|
|
|
101
|
2 |
|
if ($this->isConfigEnabled($container, $purgerConfig['last_action_listener'])) { |
|
|
|
|
102
|
2 |
|
$loader->load('last_action_listener.yml'); |
103
|
2 |
|
} |
104
|
2 |
|
} |
105
|
6 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Processes the service override configuration into the container. |
109
|
|
|
* |
110
|
|
|
* @param ContainerBuilder $container |
111
|
|
|
* @param array $services |
112
|
|
|
*/ |
113
|
6 |
|
private function overrideServices(ContainerBuilder $container, array $services) |
114
|
|
|
{ |
115
|
6 |
|
$semanticServiceReplacements = array_filter($services); |
116
|
6 |
|
if (!empty($semanticServiceReplacements)) { |
117
|
|
|
$serviceConfig = array( |
118
|
1 |
|
'auth_handler' => 'ma27_api_key_authentication.auth_handler', |
119
|
1 |
|
'key_factory' => 'ma27_api_key_authentication.key_factory', |
120
|
1 |
|
'password_hasher' => 'ma27_api_key_authentication.password.strategy', |
121
|
1 |
|
); |
122
|
|
|
|
123
|
1 |
|
if (isset($services['password_hasher'])) { |
124
|
|
|
@trigger_error('The option `services.password_hasher` is deprecated, to be removed in 2.0, create a custom hasher instead!', E_USER_DEPRECATED); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
foreach ($serviceConfig as $configIndex => $replaceableServiceId) { |
128
|
1 |
|
if (!isset($semanticServiceReplacements[$configIndex]) |
129
|
1 |
|
|| null === $serviceId = $semanticServiceReplacements[$configIndex] |
130
|
1 |
|
) { |
131
|
1 |
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
$container->removeDefinition($replaceableServiceId); |
135
|
1 |
|
$container->setAlias($replaceableServiceId, new Alias($serviceId)); |
136
|
1 |
|
} |
137
|
1 |
|
} |
138
|
6 |
|
} |
139
|
|
|
} |
140
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: