diff --git a/composer.json b/composer.json index 29bd444..a8efe81 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "php": "^7.0", "api-clients/client-services": "^1.3", "api-clients/foundation": "^1.0", + "api-clients/middleware-basic-authorization": "^3.0", "api-clients/middleware-http-exceptions": "^2.0", "api-clients/middleware-user-agent": "^2.0", "api-clients/middleware-xml": "^1.0", diff --git a/composer.lock b/composer.lock index ccefd11..55f4041 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "e415955e028950638786a59e245d1157", + "content-hash": "f01f7dfaa287b6cf842a5fae7a4147d2", "packages": [ { "name": "api-clients/client-services", @@ -254,6 +254,51 @@ "description": "Request middleware", "time": "2017-06-28T19:24:19+00:00" }, + { + "name": "api-clients/middleware-basic-authorization", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-api-clients/middleware-basic-authorization.git", + "reference": "2b874321b7eb55783d6b772b9d29f4a18c7af87c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-api-clients/middleware-basic-authorization/zipball/2b874321b7eb55783d6b772b9d29f4a18c7af87c", + "reference": "2b874321b7eb55783d6b772b9d29f4a18c7af87c", + "shasum": "" + }, + "require": { + "api-clients/middleware": "^4.0", + "php": "^7.0" + }, + "require-dev": { + "api-clients/test-utilities": "^3.0.1", + "ringcentral/psr7": "^1.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "ApiClients\\Middleware\\BasicAuthorization\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Cees-Jan Kiewiet", + "email": "ceesjank@gmail.com" + }, + { + "name": "Jaap van Otterdijk" + } + ], + "description": "Basic authorization middleware", + "homepage": "https://php-api-clients.org/middlewares/basic-authorization/", + "time": "2017-07-02T14:15:20+00:00" + }, { "name": "api-clients/middleware-http-exceptions", "version": "2.0.1", diff --git a/examples/authenticated-state-async.php b/examples/authenticated-state-async.php new file mode 100644 index 0000000..565517a --- /dev/null +++ b/examples/authenticated-state-async.php @@ -0,0 +1,25 @@ + 'username', + Options::PASSWORD => 'password', +]; + +$loop = Factory::create(); +$client = AsyncClient::create(require __DIR__ . DIRECTORY_SEPARATOR . 'resolve_host.php', $loop, $options); + +$client->state()->done(function (StateInterface $state) { + resource_pretty_print($state); +}, function ($et) { + echo (string)$et; +}); + +$loop->run(); diff --git a/examples/authenticated-state.php b/examples/authenticated-state.php new file mode 100644 index 0000000..52d770c --- /dev/null +++ b/examples/authenticated-state.php @@ -0,0 +1,16 @@ + 'username', + Options::PASSWORD => 'password', +]; + +$client = Client::create(require __DIR__ . DIRECTORY_SEPARATOR . 'resolve_host.php', $options); + +resource_pretty_print($client->state()); diff --git a/src/ApiSettings.php b/src/ApiSettings.php index 52507e5..b3ce0f3 100644 --- a/src/ApiSettings.php +++ b/src/ApiSettings.php @@ -7,6 +7,8 @@ use ApiClients\Foundation\Hydrator\Options as HydratorOptions; use ApiClients\Foundation\Options as FoundationOptions; use ApiClients\Foundation\Transport\Options as TransportOptions; +use ApiClients\Middleware\BasicAuthorization\BasicAuthorizationHeaderMiddleware; +use ApiClients\Middleware\BasicAuthorization\Options as BasicAuthorizationHeaderMiddlewareOptions; use ApiClients\Middleware\HttpExceptions\HttpExceptionsMiddleware; use ApiClients\Middleware\UserAgent\Options as UserAgentMiddlewareOptions; use ApiClients\Middleware\UserAgent\UserAgentMiddleware; @@ -46,10 +48,21 @@ public static function getOptions(string $host, array $suppliedOptions, string $ $options = options_merge(self::TRANSPORT_OPTIONS, $suppliedOptions); $options[FoundationOptions::HYDRATOR_OPTIONS][HydratorOptions::NAMESPACE_SUFFIX] = $suffix; + $transportOptions = $options[FoundationOptions::TRANSPORT_OPTIONS]; + list($ip, $port) = explode(':', $host); + $transportOptions[TransportOptions::HOST] = $ip; + $transportOptions[TransportOptions::PORT] = $port; + + if (isset($suppliedOptions[Options::USERNAME])) { + $transportOptions[TransportOptions::MIDDLEWARE][] = BasicAuthorizationHeaderMiddleware::class; + $transportOptions[TransportOptions::DEFAULT_REQUEST_OPTIONS][BasicAuthorizationHeaderMiddleware::class] = [ + BasicAuthorizationHeaderMiddlewareOptions::USERNAME => $suppliedOptions[Options::USERNAME], + BasicAuthorizationHeaderMiddlewareOptions::PASSWORD => $suppliedOptions[Options::PASSWORD] ?? '', + ]; + } - $options[FoundationOptions::TRANSPORT_OPTIONS][TransportOptions::HOST] = $ip; - $options[FoundationOptions::TRANSPORT_OPTIONS][TransportOptions::PORT] = $port; + $options[FoundationOptions::TRANSPORT_OPTIONS] = $transportOptions; return $options; } diff --git a/src/Options.php b/src/Options.php new file mode 100644 index 0000000..ae01488 --- /dev/null +++ b/src/Options.php @@ -0,0 +1,11 @@ +