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