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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This is the class that loads and manages your bundle configuration. |
13
|
|
|
* |
14
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
15
|
|
|
*/ |
16
|
|
|
class Ma27ApiKeyAuthenticationExtension extends Extension |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
22
|
|
|
{ |
23
|
6 |
|
$configuration = new Configuration(); |
24
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
25
|
|
|
|
26
|
6 |
|
$container->setParameter('ma27_api_key_authentication.key_header', $config['key_header']); |
27
|
6 |
|
$container->setParameter('ma27_api_key_authentication.model_name', $config['user']['model_name']); |
28
|
6 |
|
$container->setParameter('ma27_api_key_authentication.object_manager', $config['user']['object_manager']); |
29
|
6 |
|
$container->setParameter('ma27_api_key_authentication.property.apiKeyLength', $config['user']['api_key_length']); |
30
|
6 |
|
$container->setParameter('ma27_api_key_authentication.response_values', $config['response']); |
31
|
|
|
|
32
|
6 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
33
|
|
|
|
34
|
6 |
|
$this->loadPassword($container, $config['user']['password'], $loader); |
35
|
6 |
|
$this->loadServices($loader); |
36
|
|
|
|
37
|
6 |
|
if ($this->isConfigEnabled($container, $config['api_key_purge'])) { |
38
|
2 |
|
$this->loadApiKeyPurger($container, $loader, $config['api_key_purge']); |
39
|
2 |
|
} |
40
|
|
|
|
41
|
6 |
|
$services = array_filter($config['services']); |
42
|
6 |
|
if (!empty($services)) { |
43
|
1 |
|
$this->overrideServices($container, $services); |
44
|
1 |
|
} |
45
|
6 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Loads the password strategy. |
49
|
|
|
* |
50
|
|
|
* @param ContainerBuilder $container |
51
|
|
|
* @param string $passwordConfig |
52
|
|
|
* @param Loader\YamlFileLoader $loader |
53
|
|
|
*/ |
54
|
6 |
|
private function loadPassword(ContainerBuilder $container, $passwordConfig, Loader\YamlFileLoader $loader) |
55
|
|
|
{ |
56
|
6 |
|
$container->setParameter('ma27_api_key_authentication.password_hashing_service', $passwordConfig['strategy']); |
57
|
6 |
|
$container->setParameter('ma27_api_key_authentication.password_hasher.phpass.iteration_length', 8); |
58
|
6 |
|
$container->setParameter('ma27_api_key_authentication.password_hasher.php55.cost', 12); |
59
|
|
|
|
60
|
6 |
|
$loader->load('hashers.yml'); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Loads all internal services. |
65
|
|
|
* |
66
|
|
|
* @param Loader\YamlFileLoader $loader |
67
|
|
|
*/ |
68
|
6 |
|
private function loadServices(Loader\YamlFileLoader $loader) |
69
|
|
|
{ |
70
|
6 |
|
foreach (array('security_key', 'authentication', 'security', 'annotation') as $file) { |
71
|
6 |
|
$loader->load(sprintf('%s.yml', $file)); |
72
|
6 |
|
} |
73
|
6 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Loads the purger job command into the container. |
77
|
|
|
* |
78
|
|
|
* @param ContainerBuilder $container |
79
|
|
|
* @param Loader\YamlFileLoader $loader |
80
|
|
|
* @param string[] $purgerConfig |
81
|
|
|
*/ |
82
|
2 |
|
private function loadApiKeyPurger(ContainerBuilder $container, Loader\YamlFileLoader $loader, array $purgerConfig) |
83
|
|
|
{ |
84
|
2 |
|
$container->setParameter('ma27_api_key_authentication.cleanup_command.date_time_rule', $purgerConfig['outdated_rule']); |
85
|
2 |
|
$loader->load('session_cleanup.yml'); |
86
|
|
|
|
87
|
2 |
|
if ($this->isConfigEnabled($container, $purgerConfig['last_action_listener'])) { |
|
|
|
|
88
|
2 |
|
$loader->load('last_action_listener.yml'); |
89
|
2 |
|
} |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Processes the service override configuration into the container. |
94
|
|
|
* |
95
|
|
|
* @param ContainerBuilder $container |
96
|
|
|
* @param array $services |
97
|
|
|
*/ |
98
|
1 |
|
private function overrideServices(ContainerBuilder $container, array $services) |
99
|
|
|
{ |
100
|
|
|
$serviceConfig = array( |
101
|
1 |
|
'auth_handler' => 'ma27_api_key_authentication.auth_handler', |
102
|
1 |
|
'key_factory' => 'ma27_api_key_authentication.key_factory', |
103
|
1 |
|
); |
104
|
|
|
|
105
|
1 |
|
foreach ($serviceConfig as $configIndex => $replaceableServiceId) { |
106
|
1 |
|
if (!isset($services[$configIndex]) || null === $serviceId = $services[$configIndex]) { |
107
|
1 |
|
continue; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$container->removeDefinition($replaceableServiceId); |
111
|
1 |
|
$container->setAlias($replaceableServiceId, new Alias($serviceId)); |
112
|
1 |
|
} |
113
|
1 |
|
} |
114
|
|
|
} |
115
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: