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