示例#1
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;
 }
 /**
  * @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();
 }
示例#3
0
}
$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();
foreach ($services as $service) {
    print "\t" . $service['name'] . PHP_EOL;