Skip to content

Commit c221e0b

Browse files
committed
support client specific authentication
1 parent 048ab72 commit c221e0b

File tree

10 files changed

+82
-15
lines changed

10 files changed

+82
-15
lines changed

DependencyInjection/Configuration.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,13 @@ private function configureClientPlugins(ArrayNodeDefinition $pluginNode)
234234

235235
->validate()
236236
->always(function ($plugins) {
237-
if (isset($plugins['authentication']) && !count($plugins['authentication'])) {
238-
unset($plugins['authentication']);
239-
}
237+
240238
foreach ($plugins as $name => $definition) {
241-
if (!$definition['enabled']) {
239+
if ('authentication' === $name) {
240+
if (!count($definition)) {
241+
unset($plugins['authentication']);
242+
}
243+
} elseif (!$definition['enabled']) {
242244
unset($plugins[$name]);
243245
}
244246
}

DependencyInjection/HttplugExtension.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,15 @@ private function configurePluginByName($name, Definition $definition, array $con
202202
/**
203203
* @param ContainerBuilder $container
204204
* @param array $config
205+
*
206+
* @return array List of service ids for the authentication plugins.
205207
*/
206-
private function configureAuthentication(ContainerBuilder $container, array $config)
208+
private function configureAuthentication(ContainerBuilder $container, array $config, $servicePrefix = 'httplug.plugin.authentication')
207209
{
210+
$pluginServices = [];
211+
208212
foreach ($config as $name => $values) {
209-
$authServiceKey = sprintf('httplug.plugin.authentication.%s.auth', $name);
213+
$authServiceKey = sprintf($servicePrefix.'.%s.auth', $name);
210214
switch ($values['type']) {
211215
case 'bearer':
212216
$container->register($authServiceKey, Bearer::class)
@@ -229,9 +233,14 @@ private function configureAuthentication(ContainerBuilder $container, array $con
229233
throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type']));
230234
}
231235

232-
$container->register('httplug.plugin.authentication.'.$name, AuthenticationPlugin::class)
233-
->addArgument(new Reference($authServiceKey));
236+
$pluginServiceKey = $servicePrefix.'.'.$name;
237+
$container->register($pluginServiceKey, AuthenticationPlugin::class)
238+
->addArgument(new Reference($authServiceKey))
239+
;
240+
$pluginServices[] = $pluginServiceKey;
234241
}
242+
243+
return $pluginServices;
235244
}
236245

237246
/**
@@ -250,7 +259,7 @@ private function configureClient(ContainerBuilder $container, $clientName, array
250259
if ('reference' === $pluginName) {
251260
$plugins[] = $pluginConfig['id'];
252261
} elseif ('authentication' === $pluginName) {
253-
// TODO handle custom authentication
262+
$plugins = array_merge($plugins, $this->configureAuthentication($container, $pluginConfig, $serviceId.'.authentication'));
254263
} else {
255264
$pluginServiceId = $serviceId.'.plugin.'.$pluginName;
256265
$def = clone $container->getDefinition('httplug.plugin'.'.'.$pluginName);

Resources/config/plugins.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<argument />
2929
</service>
3030

31-
<!-- extra plugin definitions -->
31+
<!-- client specific plugin definition prototypes -->
3232

3333
<service id="httplug.plugin.add_host" class="Http\Client\Common\Plugin\AddHostPlugin" public="false" abstract="true">
3434
<argument/>

Tests/Functional/ServiceInstantiationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function testDebugToolbar()
4646
'httplug.client.acme.plugin.decoder',
4747
'httplug.plugin.redirect',
4848
'httplug.client.acme.plugin.add_host',
49+
'httplug.client.acme.authentication.my_basic',
4950
], $plugins);
5051
}
5152
}

Tests/Resources/Fixtures/config/full.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'factory' => 'httplug.factory.guzzle6',
1919
'http_methods_client' => true,
2020
'plugins' => [
21+
'httplug.plugin.redirect',
2122
[
2223
'add_host' => [
2324
'host' => 'http://localhost',
@@ -37,6 +38,15 @@
3738
],
3839
],
3940
],
41+
[
42+
'authentication' => [
43+
'my_basic' => [
44+
'type' => 'basic',
45+
'username' => 'foo',
46+
'password' => 'bar',
47+
],
48+
],
49+
]
4050
],
4151
],
4252
],

Tests/Resources/Fixtures/config/full.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<stream-factory>Http\Message\StreamFactory\GuzzleStreamFactory</stream-factory>
1616
</classes>
1717
<client name="test" factory="httplug.factory.guzzle6" http-methods-client="true">
18+
<plugin>httplug.plugin.redirect</plugin>
1819
<plugin>
1920
<add-host host="http://localhost"/>
2021
</plugin>
@@ -28,6 +29,11 @@
2829
<header>X-FOO</header>
2930
</header-remove>
3031
</plugin>
32+
<plugin>
33+
<authentication>
34+
<my_basic type="basic" username="foo" password="bar"/>
35+
</authentication>
36+
</plugin>
3137
</client>
3238
<profiling enabled="true" formatter="my_toolbar_formatter" captured_body_length="0"/>
3339
<plugins>

Tests/Resources/Fixtures/config/full.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ httplug:
1414
factory: httplug.factory.guzzle6
1515
http_methods_client: true
1616
plugins:
17+
- 'httplug.plugin.redirect'
1718
-
1819
add_host:
1920
host: http://localhost
@@ -24,7 +25,12 @@ httplug:
2425
-
2526
header_remove:
2627
headers: [X-FOO]
27-
28+
-
29+
authentication:
30+
my_basic:
31+
type: basic
32+
username: foo
33+
password: bar
2834
profiling:
2935
enabled: true
3036
formatter: my_toolbar_formatter

Tests/Resources/app/config/config.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ httplug:
77
acme:
88
factory: httplug.factory.curl
99
plugins:
10-
- decoder:
11-
use_content_encoding: false
10+
-
11+
decoder:
12+
use_content_encoding: false
1213
- httplug.plugin.redirect
13-
- add_host:
14-
host: "http://localhost:8000"
14+
-
15+
add_host:
16+
host: "http://localhost:8000"
17+
-
18+
authentication:
19+
my_basic:
20+
type: basic
21+
username: foo
22+
password: bar

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ public function testSupportsAllConfigFormats()
122122
'flexible_client' => false,
123123
'batch_client' => false,
124124
'plugins' => [
125+
[
126+
'reference' => [
127+
'enabled' => true,
128+
'id' => 'httplug.plugin.redirect',
129+
],
130+
],
125131
[
126132
'add_host' => [
127133
'enabled' => true,
@@ -145,6 +151,15 @@ public function testSupportsAllConfigFormats()
145151
],
146152
],
147153
],
154+
[
155+
'authentication' => [
156+
'my_basic' => [
157+
'type' => 'basic',
158+
'username' => 'foo',
159+
'password' => 'bar',
160+
],
161+
],
162+
]
148163
],
149164
'config' => [],
150165
],

Tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ public function testClientPlugins()
101101
'headers' => ['X-FOO'],
102102
],
103103
],
104+
[
105+
'authentication' => [
106+
'my_basic' => [
107+
'type' => 'basic',
108+
'username' => 'foo',
109+
'password' => 'bar',
110+
],
111+
],
112+
],
104113
],
105114
],
106115
],
@@ -115,6 +124,7 @@ public function testClientPlugins()
115124
'httplug.client.acme.plugin.header_defaults',
116125
'httplug.client.acme.plugin.header_set',
117126
'httplug.client.acme.plugin.header_remove',
127+
'httplug.client.acme.authentication.my_basic',
118128
];
119129
$pluginReferences = array_map(function ($id) {
120130
return new Reference($id);

0 commit comments

Comments
 (0)