Skip to content

Commit 5e3a44c

Browse files
committed
Updated the authentication configuration to support credentials
1 parent b27c849 commit 5e3a44c

File tree

5 files changed

+135
-24
lines changed

5 files changed

+135
-24
lines changed

DependencyInjection/Configuration.php

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,7 @@ protected function configurePlugins(ArrayNodeDefinition $root)
120120
->arrayNode('plugins')
121121
->addDefaultsIfNotSet()
122122
->children()
123-
124-
->arrayNode('authentication')
125-
->canBeEnabled()
126-
->children()
127-
->scalarNode('authentication')
128-
->info('This must be a service id to a service implementing Http\Message\Authentication')
129-
->isRequired()
130-
->cannotBeEmpty()
131-
->end()
132-
->end()
133-
->end() // End authentication plugin
123+
->append($this->addAuthenticationPluiginNode())
134124

135125
->arrayNode('cache')
136126
->canBeEnabled()
@@ -235,4 +225,62 @@ protected function configurePlugins(ArrayNodeDefinition $root)
235225
->end()
236226
->end();
237227
}
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['token'])) {
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+
}
238286
}

DependencyInjection/HttplugExtension.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Http\HttplugBundle\DependencyInjection;
44

5+
use Http\Client\Plugin\AuthenticationPlugin;
56
use Http\Client\Plugin\PluginClient;
67
use Http\HttplugBundle\ClientFactory\DummyClient;
8+
use Http\Message\Authentication\BasicAuth;
9+
use Http\Message\Authentication\Bearer;
10+
use Http\Message\Authentication\Wsse;
711
use Symfony\Component\Config\FileLocator;
812
use Symfony\Component\DependencyInjection\ContainerBuilder;
913
use Symfony\Component\DependencyInjection\Definition;
@@ -106,6 +110,11 @@ private function configureClients(ContainerBuilder $container, array $config)
106110
*/
107111
private function configurePlugins(ContainerBuilder $container, array $config)
108112
{
113+
if (!empty($config['authentication'])) {
114+
$this->configureAuthentication($container, $config['authentication']);
115+
}
116+
unset($config['authentication']);
117+
109118
foreach ($config as $name => $pluginConfig) {
110119
$pluginId = 'httplug.plugin.'.$name;
111120
if ($pluginConfig['enabled']) {
@@ -125,9 +134,6 @@ private function configurePlugins(ContainerBuilder $container, array $config)
125134
private function configurePluginByName($name, Definition $definition, array $config)
126135
{
127136
switch ($name) {
128-
case 'authentication':
129-
$definition->replaceArgument(0, new Reference($config['authentication']));
130-
break;
131137
case 'cache':
132138
$definition
133139
->replaceArgument(0, new Reference($config['cache_pool']))
@@ -162,4 +168,40 @@ private function configurePluginByName($name, Definition $definition, array $con
162168
break;
163169
}
164170
}
171+
172+
/**
173+
* @param ContainerBuilder $container
174+
* @param Definition $parent
175+
* @param array $config
176+
*/
177+
private function configureAuthentication(ContainerBuilder $container, array $config)
178+
{
179+
foreach ($config as $name => $values) {
180+
$authServiceKey = sprintf('httplug.plugin.authentication.%s.auth', $name);
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']));
201+
}
202+
203+
$container->register('httplug.plugin.authentication.'.$name, AuthenticationPlugin::class)
204+
->addArgument(new Reference($authServiceKey));
205+
}
206+
}
165207
}

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ For information how to write applications with the services provided by this bun
5252
| httplug.client.[name] | This is your Httpclient that you have configured. With the configuration below the name would be `acme_client`.
5353
| httplug.client | This is the first client configured or a client named `default`.
5454
| httplug.plugin.content_length <br> httplug.plugin.decoder<br> httplug.plugin.error<br> httplug.plugin.logger<br> httplug.plugin.redirect<br> httplug.plugin.retry<br> httplug.plugin.stopwatch | These are plugins that are enabled by default. These services are not public and may only be used when configure HttpClients or other services.
55-
| httplug.plugin.authentication <br> httplug.plugin.cache<br> httplug.plugin.cookie<br> httplug.plugin.history | These are plugins that are disabled by default. They need to be configured before they can be used. These services are not public and may only be used when configure HttpClients or other services.
55+
| httplug.plugin.cache<br> httplug.plugin.cookie<br> httplug.plugin.history | These are plugins that are disabled by default. They need to be configured before they can be used. These services are not public and may only be used when configure HttpClients or other services.
5656

5757
\* *These services are always an alias to another service. You can specify your own service or leave the default, which is the same name with `.default` appended. The default services in turn use the service discovery mechanism to provide the best available implementation. You can specify a class for each of the default services to use instead of discovery, as long as those classes can be instantiated without arguments.*
5858

@@ -134,6 +134,34 @@ httpug:
134134
base_uri: 'http://google.se/'
135135
```
136136
137+
#### Authentication
138+
139+
```yaml
140+
// config.yml
141+
httpug:
142+
plugins:
143+
authentication:
144+
my_basic:
145+
type: 'basic'
146+
username: 'my_username'
147+
password: 'p4ssw0rd'
148+
my_wsse:
149+
type: 'wsse'
150+
username: 'my_username'
151+
password: 'p4ssw0rd'
152+
my_brearer:
153+
type: 'bearer'
154+
token: 'authentication_token_hash'
155+
my_service:
156+
type: 'service'
157+
service: 'my_authentication_service'
158+
159+
clients:
160+
acme:
161+
factory: 'httplug.factory.guzzle6'
162+
plugins: ['httplug.plugin.authentication.my_wsse']
163+
```
164+
137165
138166
### Use for Reusable Bundles
139167

Resources/config/plugins.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55

66
<services>
7-
<service id="httplug.plugin.authentication" class="Http\Client\Plugin\AuthenticationPlugin" public="false">
8-
<argument />
9-
</service>
107
<service id="httplug.plugin.cache" class="Http\Client\Plugin\CachePlugin" public="false">
118
<argument />
129
<argument />

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public function testEmptyConfiguration()
4242
'formatter' => null,
4343
],
4444
'plugins' => [
45-
'authentication' => [
46-
'enabled' => false,
47-
],
45+
'authentication' => [],
4846
'cache' => [
4947
'enabled' => false,
5048
'stream_factory' => 'httplug.stream_factory',
@@ -118,9 +116,7 @@ public function testSupportsAllConfigFormats()
118116
'formatter' => null,
119117
],
120118
'plugins' => [
121-
'authentication' => [
122-
'enabled' => false,
123-
],
119+
'authentication' => [],
124120
'cache' => [
125121
'enabled' => false,
126122
'stream_factory' => 'httplug.stream_factory',

0 commit comments

Comments
 (0)