Skip to content

feat: header authentication service configuration #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

- Added configuration for the `header` authentication plugin (#437).

# 1.30.1 - 2023-09-07

- Added alias to allow autowiring the `AsyncHttpClient` interface (#436).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"php-http/discovery": "^1.14",
"php-http/httplug": "^2.0",
"php-http/logger-plugin": "^1.1",
"php-http/message": "^1.4",
"php-http/message": "^1.9",
"php-http/message-factory": "^1.0.2",
"php-http/stopwatch-plugin": "^1.2",
"psr/http-message": "^1.0 || ^2.0",
Expand Down
8 changes: 7 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,10 @@ private function createAuthenticationPluginNode(): NodeDefinition
case 'query_param':
$this->validateAuthenticationType(['params'], $config, 'query_param');

break;
case 'header':
$this->validateAuthenticationType(['header_name', 'header_value'], $config, 'header');

break;
}

Expand All @@ -670,14 +674,16 @@ private function createAuthenticationPluginNode(): NodeDefinition
->end()
->children()
->enumNode('type')
->values(['basic', 'bearer', 'wsse', 'service', 'query_param'])
->values(['basic', 'bearer', 'wsse', 'service', 'query_param', 'header'])
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('username')->end()
->scalarNode('password')->end()
->scalarNode('token')->end()
->scalarNode('service')->end()
->scalarNode('header_name')->end()
->scalarNode('header_value')->end()
->arrayNode('params')->prototype('scalar')->end()
->end()
->end()
Expand Down
7 changes: 7 additions & 0 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Http\Client\Plugin\Vcr\ReplayPlugin;
use Http\Message\Authentication\BasicAuth;
use Http\Message\Authentication\Bearer;
use Http\Message\Authentication\Header;
use Http\Message\Authentication\QueryParam;
use Http\Message\Authentication\Wsse;
use Http\Mock\Client as MockClient;
Expand Down Expand Up @@ -380,6 +381,12 @@ private function configureAuthentication(ContainerBuilder $container, array $con
$container->register($authServiceKey, QueryParam::class)
->addArgument($values['params']);

break;
case 'header':
$container->register($authServiceKey, Header::class)
->addArgument($values['header_name'])
->addArgument($values['header_value']);

break;
case 'service':
$authServiceKey = $values['service'];
Expand Down
5 changes: 5 additions & 0 deletions tests/Resources/Fixtures/config/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
'type' => 'bearer',
'token' => 'foo',
],
'my_header' => [
'type' => 'header',
'header_name' => 'foo',
'header_value' => 'bar',
],
'my_service' => [
'type' => 'service',
'service' => 'my_auth_service',
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<my_basic type="basic" username="foo" password="bar"/>
<my_wsse type="wsse" username="foo" password="bar"/>
<my_bearer type="bearer" token="foo"/>
<my_header type="header" header_name="foo" header_value="bar" />
<my_service type="service" service="my_auth_service"/>
</authentication>
<cache cache-pool="my_cache_pool" stream-factory="my_other_stream_factory">
Expand Down
4 changes: 4 additions & 0 deletions tests/Resources/Fixtures/config/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ httplug:
my_bearer:
type: bearer
token: foo
my_header:
type: header
header_name: foo
header_value: bar
my_service:
type: service
service: my_auth_service
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ public function testSupportsAllConfigFormats(): void
'token' => 'foo',
'params' => [],
],
'my_header' => [
'type' => 'header',
'header_name' => 'foo',
'header_value' => 'bar',
'params' => [],
],
'my_service' => [
'type' => 'service',
'service' => 'my_auth_service',
Expand Down