|
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.model_name', $config['user']['model_name']); |
|
29
|
|
|
$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 |
|
intval(floor($config['user']['api_key_length'] / 2)) |
|
33
|
6 |
|
); |
|
34
|
6 |
|
|
|
35
|
6 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
36
|
|
|
$this->loadPassword($container, $config['user']['password']); |
|
37
|
6 |
|
$this->loadServices($loader); |
|
38
|
|
|
$this->loadApiKeyPurger($container, $loader, $config['api_key_purge']); |
|
39
|
6 |
|
$this->overrideServices($container, $config['services']); |
|
40
|
6 |
|
} |
|
41
|
6 |
|
|
|
42
|
6 |
|
/** |
|
43
|
|
|
* Loads the password strategy. |
|
44
|
6 |
|
* |
|
45
|
6 |
|
* @param ContainerBuilder $container |
|
46
|
6 |
|
* @param string $passwordConfig |
|
47
|
6 |
|
*/ |
|
48
|
6 |
|
private function loadPassword(ContainerBuilder $container, $passwordConfig) |
|
49
|
6 |
|
{ |
|
50
|
|
|
$strategyArguments = array(); |
|
51
|
|
|
switch ($passwordConfig['strategy']) { |
|
52
|
|
|
case 'php55': |
|
53
|
|
|
$className = 'Ma27\\ApiKeyAuthenticationBundle\\Model\\Password\\PhpPasswordHasher'; |
|
54
|
|
|
|
|
55
|
|
|
break; |
|
56
|
|
|
case 'crypt': |
|
57
|
6 |
|
$className = 'Ma27\\ApiKeyAuthenticationBundle\\Model\\Password\\CryptPasswordHasher'; |
|
58
|
|
|
|
|
59
|
6 |
|
break; |
|
60
|
|
|
case 'sha512': |
|
61
|
6 |
|
$className = 'Ma27\\ApiKeyAuthenticationBundle\\Model\\Password\\Sha512PasswordHasher'; |
|
62
|
6 |
|
|
|
63
|
6 |
|
break; |
|
64
|
1 |
|
case 'phpass': |
|
65
|
|
|
$className = 'Ma27\\ApiKeyAuthenticationBundle\\Model\\Password\\PHPassHasher'; |
|
66
|
1 |
|
$strategyArguments[] = $passwordConfig['phpass_iteration_length']; |
|
67
|
5 |
|
|
|
68
|
2 |
|
break; |
|
69
|
|
|
default: |
|
70
|
2 |
|
throw new InvalidConfigurationException('Cannot create password config!'); |
|
71
|
3 |
|
} |
|
72
|
2 |
|
|
|
73
|
|
|
$container->setDefinition( |
|
74
|
2 |
|
'ma27_api_key_authentication.password.strategy', |
|
75
|
1 |
|
new Definition($className, $strategyArguments) |
|
76
|
1 |
|
); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
/** |
|
80
|
|
|
* Loads all internal services. |
|
81
|
|
|
* |
|
82
|
6 |
|
* @param Loader\YamlFileLoader $loader |
|
83
|
|
|
*/ |
|
84
|
6 |
|
private function loadServices(Loader\YamlFileLoader $loader) |
|
85
|
6 |
|
{ |
|
86
|
6 |
|
foreach (array('security_key', 'authentication', 'security', 'annotation') as $file) { |
|
87
|
6 |
|
$loader->load(sprintf('%s.yml', $file)); |
|
88
|
6 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Loads the purger job command into the container. |
|
93
|
|
|
* |
|
94
|
|
|
* @param ContainerBuilder $container |
|
95
|
6 |
|
* @param Loader\YamlFileLoader $loader |
|
96
|
|
|
* @param string[] $purgerConfig |
|
97
|
6 |
|
*/ |
|
98
|
6 |
|
private function loadApiKeyPurger(ContainerBuilder $container, Loader\YamlFileLoader $loader, array $purgerConfig) |
|
99
|
6 |
|
{ |
|
100
|
6 |
|
if ($this->isConfigEnabled($container, $purgerConfig)) { |
|
101
|
|
|
$container->setParameter( |
|
102
|
|
|
'ma27_api_key_authentication.last_activation_parameter', |
|
103
|
|
|
$purgerConfig['last_active_property'] |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
$loader->load('session_cleanup.yml'); |
|
107
|
|
|
|
|
108
|
|
|
if ($purgerConfig['log_state']) { |
|
109
|
6 |
|
$container->setParameter( |
|
110
|
|
|
'ma27_api_key_authentication.logger', |
|
111
|
6 |
|
$purgerConfig['logger_service'] |
|
112
|
1 |
|
); |
|
113
|
1 |
|
} |
|
114
|
1 |
|
} |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
/** |
|
118
|
|
|
* Processes the service override configuration into the container. |
|
119
|
1 |
|
* |
|
120
|
1 |
|
* @param ContainerBuilder $container |
|
121
|
1 |
|
* @param array $services |
|
122
|
1 |
|
*/ |
|
123
|
1 |
|
private function overrideServices(ContainerBuilder $container, array $services) |
|
124
|
1 |
|
{ |
|
125
|
1 |
|
$semanticServiceReplacements = array_filter($services); |
|
126
|
6 |
|
if (!empty($semanticServiceReplacements)) { |
|
127
|
|
|
$serviceConfig = array( |
|
128
|
|
|
'auth_handler' => 'ma27_api_key_authentication.auth_handler', |
|
129
|
|
|
'key_factory' => 'ma27_api_key_authentication.key_factory', |
|
130
|
|
|
'password_hasher' => 'ma27_api_key_authentication.password.strategy', |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
foreach ($serviceConfig as $configIndex => $replaceableServiceId) { |
|
134
|
6 |
|
if (!isset($semanticServiceReplacements[$configIndex]) |
|
135
|
|
|
|| null === $serviceId = $semanticServiceReplacements[$configIndex] |
|
136
|
6 |
|
) { |
|
137
|
6 |
|
continue; |
|
138
|
|
|
} |
|
139
|
1 |
|
|
|
140
|
1 |
|
$container->removeDefinition($replaceableServiceId); |
|
141
|
1 |
|
$container->setAlias($replaceableServiceId, new Alias($serviceId)); |
|
142
|
1 |
|
} |
|
143
|
|
|
} |
|
144
|
1 |
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|