Skip to content

Commit f5815b6

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

File tree

5 files changed

+97
-20
lines changed

5 files changed

+97
-20
lines changed

DependencyInjection/Configuration.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,18 @@ protected function configurePlugins(ArrayNodeDefinition $root)
120120
->arrayNode('plugins')
121121
->addDefaultsIfNotSet()
122122
->children()
123-
124123
->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()
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()
131135
->end()
132136
->end()
133137
->end() // End authentication plugin

DependencyInjection/HttplugExtension.php

Lines changed: 58 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,53 @@ 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+
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']));
197+
}
198+
199+
$container->register('httplug.plugin.authentication.'.$name, AuthenticationPlugin::class)
200+
->addArgument(new Reference($authServiceKey));
201+
}
202+
}
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+
}
165220
}

README.md

Lines changed: 26 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,31 @@ 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: 'foo'
147+
password: 'bar'
148+
my_wsse:
149+
type: 'wsse'
150+
username: 'foo'
151+
password: 'bar'
152+
my_brearer:
153+
type: 'bearer'
154+
token: 'foo'
155+
156+
clients:
157+
acme:
158+
factory: 'httplug.factory.guzzle6'
159+
plugins: ['httplug.plugin.authentication.my_wsse']
160+
```
161+
137162
138163
### Use for Reusable Bundles
139164

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)