Skip to content

Commit fa97a19

Browse files
committed
Use custom validation for authentication plugin
1 parent f5815b6 commit fa97a19

File tree

3 files changed

+87
-53
lines changed

3 files changed

+87
-53
lines changed

DependencyInjection/Configuration.php

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,7 @@ protected function configurePlugins(ArrayNodeDefinition $root)
120120
->arrayNode('plugins')
121121
->addDefaultsIfNotSet()
122122
->children()
123-
->arrayNode('authentication')
124-
->useAttributeAsKey('name')
125-
->prototype('array')
126-
->children()
127-
->enumNode('type')
128-
->values(['basic', 'bearer', 'wsse'])
129-
->isRequired()
130-
->cannotBeEmpty()
131-
->end()
132-
->scalarNode('username')->end()
133-
->scalarNode('password')->end()
134-
->scalarNode('token')->end()
135-
->end()
136-
->end()
137-
->end() // End authentication plugin
123+
->append($this->addAuthenticationPluiginNode())
138124

139125
->arrayNode('cache')
140126
->canBeEnabled()
@@ -239,4 +225,62 @@ protected function configurePlugins(ArrayNodeDefinition $root)
239225
->end()
240226
->end();
241227
}
228+
229+
/**
230+
* Add configuration for authentication plugin.
231+
*
232+
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
233+
*/
234+
private function addAuthenticationPluiginNode()
235+
{
236+
$builder = new TreeBuilder();
237+
$node = $builder->root('authentication');
238+
$node
239+
->useAttributeAsKey('name')
240+
->prototype('array')
241+
->validate()
242+
->always()
243+
->then(function ($config) {
244+
switch ($config['type']) {
245+
case 'basic':
246+
if (empty($config['username']) || empty($config['password'])) {
247+
throw new InvalidConfigurationException('Authentication "basic" requires both "username" and "password".');
248+
}
249+
break;
250+
case 'bearer':
251+
if (empty($config['bearer'])) {
252+
throw new InvalidConfigurationException('Authentication "bearer" requires a "token".');
253+
}
254+
break;
255+
case 'service':
256+
if (empty($config['service'])) {
257+
throw new InvalidConfigurationException('Authentication "service" requires a "service".');
258+
}
259+
break;
260+
case 'wsse':
261+
if (empty($config['username']) || empty($config['password'])) {
262+
throw new InvalidConfigurationException('Authentication "wsse" requires both "username" and "password".');
263+
}
264+
break;
265+
}
266+
267+
return $config;
268+
})
269+
->end()
270+
->children()
271+
->enumNode('type')
272+
->values(['basic', 'bearer', 'wsse', 'service'])
273+
->isRequired()
274+
->cannotBeEmpty()
275+
->end()
276+
->scalarNode('username')->end()
277+
->scalarNode('password')->end()
278+
->scalarNode('token')->end()
279+
->scalarNode('service')->end()
280+
->end()
281+
->end()
282+
->end(); // End authentication plugin
283+
284+
return $node;
285+
}
242286
}

DependencyInjection/HttplugExtension.php

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -178,43 +178,30 @@ private function configureAuthentication(ContainerBuilder $container, array $con
178178
{
179179
foreach ($config as $name => $values) {
180180
$authServiceKey = sprintf('httplug.plugin.authentication.%s.auth', $name);
181-
if ($values['type'] === 'bearer') {
182-
$this->validateAuthenticationConfiguration($values, 'bearer', $name, ['token']);
183-
$container->register($authServiceKey, Bearer::class)
184-
->addArgument($values['token']);
185-
} elseif ($values['type'] === 'basic') {
186-
$this->validateAuthenticationConfiguration($values, 'bearer', $name, ['username', 'password']);
187-
$container->register($authServiceKey, BasicAuth::class)
188-
->addArgument($values['username'])
189-
->addArgument($values['password']);
190-
} elseif ($values['type'] === 'wsse') {
191-
$this->validateAuthenticationConfiguration($values, 'bearer', $name, ['username', 'password']);
192-
$container->register($authServiceKey, Wsse::class)
193-
->addArgument($values['username'])
194-
->addArgument($values['password']);
195-
} else {
196-
throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type']));
181+
switch ($values['type']) {
182+
case 'bearer':
183+
$container->register($authServiceKey, Bearer::class)
184+
->addArgument($values['token']);
185+
break;
186+
case 'basic':
187+
$container->register($authServiceKey, BasicAuth::class)
188+
->addArgument($values['username'])
189+
->addArgument($values['password']);
190+
break;
191+
case 'wsse':
192+
$container->register($authServiceKey, Wsse::class)
193+
->addArgument($values['username'])
194+
->addArgument($values['password']);
195+
break;
196+
case 'service':
197+
$authServiceKey = $values['service'];
198+
break;
199+
default:
200+
throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type']));
197201
}
198202

199203
$container->register('httplug.plugin.authentication.'.$name, AuthenticationPlugin::class)
200204
->addArgument(new Reference($authServiceKey));
201205
}
202206
}
203-
204-
/**
205-
* Throw a Logic error with a descriptive error if some configuration is missing.
206-
*
207-
* @param array $config
208-
* @param string $authType
209-
* @param string $type
210-
* @param array $names
211-
*/
212-
private function validateAuthenticationConfiguration(array $config, $type, $authType, array $names)
213-
{
214-
foreach ($names as $name) {
215-
if (empty($config[$name])) {
216-
throw new \LogicException(sprintf('When using %s ahtnetication you must configure a value for "httplug.plugins.authentication.%s.%s".', $type, $authType, $name));
217-
}
218-
}
219-
}
220207
}

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,18 @@ httpug:
143143
authentication:
144144
my_basic:
145145
type: 'basic'
146-
username: 'foo'
147-
password: 'bar'
146+
username: 'my_username'
147+
password: 'p4ssw0rd'
148148
my_wsse:
149149
type: 'wsse'
150-
username: 'foo'
151-
password: 'bar'
150+
username: 'my_username'
151+
password: 'p4ssw0rd'
152152
my_brearer:
153153
type: 'bearer'
154-
token: 'foo'
154+
token: 'authentication_token_hash'
155+
my_service:
156+
type: 'service'
157+
service: 'my_authentication_service'
155158

156159
clients:
157160
acme:

0 commit comments

Comments
 (0)