This patch achieves three interrelated changes 1. an update to the ClientInterface, making it more efficient and consistent with other HTTP libraries. PSR-FIG messages have also been added as separate classes, rather than lumped as one. Guzzle functionality has also been moved to its own namespace; 2. the refactoring of `GuzzleClient` to `GuzzleAdapter`, including relevant changes needed for the interface change (1). We now have ADAPTERS that bridge our interfaces with Guzzle's - making that difference much clearer, extensible, less tightly coupled and less brittle; 3. moving "bad request" error handling into its own dedicated space, based on how the new Adapter functionality (2). Previously the client tried to do all the exception logic - but this is not strictly its responsibility. This logic has been shipped out to a base RequestException which instantiates one of its children exception based on the HTTP status code. Although I have attempted to keep the scope of this patch to a minimum, as granular as possible, because the interface is a core internal API, various other files need to be modified to reflect the change. In terms of the other two changes, these are inextricably linked to the interface change, so cannot be teased out into their own patches. Change-Id: Ibc1b50cec125c11d32ee6e4f0dbb395fcaea864e
94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
<?php
|
|
/* ============================================================================
|
|
(c) Copyright 2012-2014 Hewlett-Packard Development Company, L.P.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
============================================================================ */
|
|
/**
|
|
* This is a simple command-line test for authentication.
|
|
*
|
|
* You can run the test with `php tests/AuthTest.php username key`.
|
|
*/
|
|
|
|
$base = dirname(__DIR__);
|
|
require_once $base . '/vendor/autoloader.php';
|
|
|
|
use \OpenStack\ObjectStore\v1\ObjectStorage;
|
|
use \OpenStack\Identity\v2\IdentityService;
|
|
|
|
$config = array(
|
|
'transport' => 'OpenStack\Common\Transport\Guzzle\GuzzleAdapter',
|
|
'transport.timeout' => 240,
|
|
//'transport.debug' => 1,
|
|
'transport.ssl.verify' => 0,
|
|
);
|
|
|
|
\OpenStack\Autoloader::useAutoloader();
|
|
\OpenStack\Bootstrap::setConfiguration($config);
|
|
|
|
$help = "Authenticate against OpenStack Identity Service.
|
|
|
|
You can authenticate using a username and password.
|
|
|
|
While Tenant ID is optional, it is recommended.
|
|
|
|
In both cases, you must supply a URL to the Identity Services endpoint.
|
|
";
|
|
|
|
$usage = "php {$argv[0]} USERNAME PASSWORD URL [TENANT_ID]";
|
|
|
|
if ($argc > 1 && $argv[1] == '--help') {
|
|
print PHP_EOL . "\t" . $usage . PHP_EOL;
|
|
print PHP_EOL . $help . PHP_EOL;
|
|
exit(1);
|
|
} elseif ($argc < 4) {
|
|
print 'USERNAME, PASSWORD, and URL are all required.' . PHP_EOL;
|
|
print $usage . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
$offset = 0;
|
|
|
|
$user = $argv[1 + $offset];
|
|
$password = $argv[2 + $offset];
|
|
$uri = $argv[3 + $offset];
|
|
|
|
$tenantId = null;
|
|
if (!empty($argv[4 + $offset])) {
|
|
$tenantId = $argv[4 + $offset];
|
|
}
|
|
|
|
$cs = new IdentityService($uri);
|
|
|
|
$token = $cs->authenticateAsUser($user, $password, $tenantId);
|
|
|
|
if (empty($token)) {
|
|
print "Authentication seemed to succeed, but no token was returned." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
$t = "You are logged in as %s with token %s (good until %s)." . PHP_EOL;
|
|
$tokenDetails = $cs->tokenDetails();
|
|
$user = $cs->user();
|
|
|
|
printf($t, $user['name'], $cs->token(), $tokenDetails['expires']);
|
|
|
|
print "The following services are available on this user:" . PHP_EOL;
|
|
|
|
$services = $cs->serviceCatalog();
|
|
foreach ($services as $service) {
|
|
print "\t" . $service['name'] . PHP_EOL;
|
|
}
|
|
|
|
//print_r($services);
|