Skip to content

Commit

Permalink
Merge pull request #2 from php-api-clients/authentication
Browse files Browse the repository at this point in the history
Authentication
  • Loading branch information
WyriHaximus authored Jul 7, 2018
2 parents 384d742 + 6d9f107 commit db86cb4
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
47 changes: 46 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions examples/authenticated-state-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

use ApiClients\Client\Supervisord\AsyncClient;
use ApiClients\Client\Supervisord\Options;
use ApiClients\Client\Supervisord\Resource\StateInterface;
use React\EventLoop\Factory;
use function ApiClients\Foundation\resource_pretty_print;

require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';

$options = [
Options::USERNAME => '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();
16 changes: 16 additions & 0 deletions examples/authenticated-state.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

use ApiClients\Client\Supervisord\Client;
use ApiClients\Client\Supervisord\Options;
use function ApiClients\Foundation\resource_pretty_print;

require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';

$options = [
Options::USERNAME => 'username',
Options::PASSWORD => 'password',
];

$client = Client::create(require __DIR__ . DIRECTORY_SEPARATOR . 'resolve_host.php', $options);

resource_pretty_print($client->state());
17 changes: 15 additions & 2 deletions src/ApiSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace ApiClients\Client\Supervisord;

final class Options
{
const USERNAME = 'username';
const PASSWORD = 'password';
}

0 comments on commit db86cb4

Please sign in to comment.