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