/**
  * @group tenant
  * @depends testTenants
  */
 function testRescopeByTenantName()
 {
     $service = new IdentityServices(self::conf('hpcloud.identity.url'));
     $user = self::conf('hpcloud.identity.username');
     $pass = self::conf('hpcloud.identity.password');
     $tenantName = self::conf('hpcloud.identity.tenantName');
     // Authenticate without a tenant ID.
     $token = $service->authenticateAsUser($user, $pass);
     $this->assertNotEmpty($token);
     $details = $service->tokenDetails();
     $this->assertFalse(isset($details['tenant']));
     $service->rescopeUsingTenantName($tenantName);
     $details = $service->tokenDetails();
     $this->assertEquals($tenantName, $details['tenant']['name']);
     $catalog = $service->serviceCatalog();
     $this->assertGreaterThan(1, count($catalog));
     // Test unscoping
     $service->rescope('');
     $details = $service->tokenDetails();
     $this->assertFalse(isset($details['tenant']));
     $catalog = $service->serviceCatalog();
 }
Exemple #2
0
    $asUser = TRUE;
    ++$offset;
}
$user = $argv[1 + $offset];
$key = $argv[2 + $offset];
$uri = $argv[3 + $offset];
$tenantId = NULL;
if (!empty($argv[4 + $offset])) {
    $tenantId = $argv[4 + $offset];
}
/*
$store = ObjectStorage::newFromSwiftAuth($user, $key, $uri);

$token = $store->token();
*/
$cs = new IdentityServices($uri);
if ($asUser) {
    $token = $cs->authenticateAsUser($user, $key, $tenantId);
} else {
    $token = $cs->authenticateAsAccount($user, $key, $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 account:" . PHP_EOL;
$services = $cs->serviceCatalog();
Exemple #3
0
 /**
  * Get a HPCloud::Services::IdentityService object from the bootstrap config.
  *
  * A factory helper function that uses the bootstrap configuration to create
  * a ready to use HPCloud::Services::IdentityService object.
  *
  * @param bool $force
  *   Whether to force the generation of a new object even if one is already
  *   cached.
  * @retval HPCloud::Services::IdentityService
  * @return \HPCloud\Services\:IdentityService
  *   An authenticated ready to use HPCloud::Services::IdentityService object.
  * @throws HPCloud::Exception
  *   When the needed configuration to authenticate is not available.
  */
 public static function identity($force = FALSE)
 {
     // If we already have an identity make sure the token is not expired.
     if ($force || is_null(self::$identity) || self::$identity->isExpired()) {
         // Make sure we have an endpoint to use
         if (!self::hasConfig('endpoint')) {
             throw new Exception('Unable to authenticate. No endpoint supplied.');
         }
         // Neither user nor account can be an empty string, so we need
         // to do more checking than self::hasConfig(), which returns TRUE
         // if an item exists and is an empty string.
         $user = self::config('username', NULL);
         $account = self::config('account', NULL);
         // Check if we have a username/password
         if (!empty($user) && self::hasConfig('password')) {
             $is = new IdentityServices(self::config('endpoint'));
             $is->authenticateAsUser($user, self::config('password'), self::config('tenantid', NULL), self::config('tenantname', NULL));
             self::$identity = $is;
         } elseif (!empty($account) && self::hasConfig('secret')) {
             $is = new IdentityServices(self::config('endpoint'));
             $is->authenticateAsAccount($account, self::config('secret'), self::config('tenantid', NULL), self::config('tenantname', NULL));
             self::$identity = $is;
         } else {
             throw new Exception('Unable to authenticate. No account credentials supplied.');
         }
     }
     return self::$identity;
 }
<?php

require_once __DIR__ . '/../src/HPCloud/Bootstrap.php';
use HPCloud\Bootstrap;
use HPCloud\Services\IdentityServices;
use HPCloud\Storage\ObjectStorage;
use HPCloud\Storage\ObjectStorage\Object;
Bootstrap::useAutoloader();
// Load these from an ini file.
$ini = parse_ini_file(getenv('HOME') . '/.hpcloud.ini');
$username = $ini['username'];
$password = $ini['password'];
$tenantId = $ini['tenantId'];
$endpoint = $ini['url'];
$idService = new IdentityServices($endpoint);
$token = $idService->authenticateAsAccount($username, $password, $tenantId);
$catalog = $idService->serviceCatalog();
$store = ObjectStorage::newFromServiceCatalog($catalog, $token);
$store->createContainer('Example');
$container = $store->container('Example');
$name = 'hello.txt';
$content = 'Hello World';
$mime = 'text/plain';
$localObject = new Object($name, $content, $mime);
$container->save($localObject);
$object = $container->object('hello.txt');
printf("Name: %s \n", $object->name());
printf("Size: %d \n", $object->contentLength());
printf("Type: %s \n", $object->contentType());
print $object->content() . PHP_EOL;