示例#1
0
 /**
  * @test
  */
 public function testConstruction()
 {
     $authToken = AuthToken::loadJson('{"access":{"token":{"expires": "2012-09-14T15:11:57.585-05:00","id": "858fb4c2-bf15-4dac-917d-8ec750ae9baa","tenant": {"id": "010101","name": "0101012"}}}}');
     $this->checkToken($authToken);
     $d = $authToken->getAuthDocument();
     $authToken = new AuthToken();
     $authToken->setExpires(new \DateTime('2012-09-14T15:11:57.585-05:00'))->setId('858fb4c2-bf15-4dac-917d-8ec750ae9baa')->setTenantId('010101')->setTenantName('0101012')->setAuthDocument($d)->setRegionEndpoints(array())->setZones(array());
     $this->checkToken($authToken);
     unset($authToken);
     unset($authToken);
 }
示例#2
0
    /**
     * @test
     */
    public function testConstruction()
    {
        $authToken = LoaderFactory::makeToken(new RestClientResponse(new HttpMessage('HTTP/1.1 200 OK
Content-Type: application/json; charset utf-8

{"access":{"token":{"expires": "2012-09-14T15:11:57.585-05:00","id": "858fb4c2-bf15-4dac-917d-8ec750ae9baa","tenant": {"id": "010101","name": "0101012"}}}}'), AppFormat::json()));
        $this->checkToken($authToken);
        $d = $authToken->getAuthDocument();
        $authToken = new AuthToken();
        $authToken->setExpires(new \DateTime('2012-09-14T15:11:57.585-05:00'))->setId('858fb4c2-bf15-4dac-917d-8ec750ae9baa')->setTenantId('010101')->setTenantName('0101012')->setAuthDocument($d)->setRegionEndpoints(array())->setZones(array());
        $this->checkToken($authToken);
        unset($authToken);
        unset($authToken);
    }
示例#3
0
文件: LoaderV3.php 项目: mheydt/scalr
 /**
  * {@inheritdoc}
  * @see LoaderInterface::loadJson()
  */
 public static function loadJson(ClientResponseInterface $response)
 {
     $token = $response->getHeader('X-Subject-Token');
     $jsonString = $response->getContent();
     $obj = json_decode($jsonString);
     if (empty($token)) {
         $invalid = true;
     }
     if (isset($invalid) || !isset($obj->token->expires_at)) {
         throw new InvalidArgumentException("Malformed JSON document " . (string) $jsonString);
     }
     $regions = $services = [];
     if (!empty($obj->token->catalog)) {
         foreach ($obj->token->catalog as $srv) {
             foreach ($srv->endpoints as $srvEndpoint) {
                 $url = $srvEndpoint->url;
                 $srvVersion = OpenStackConfig::parseIdentityVersion($url);
                 if (isset($srvEndpoint->region)) {
                     $regions[$srvEndpoint->region] = true;
                     $endpointRegion = $srvEndpoint->region;
                 } else {
                     $endpointRegion = '';
                 }
                 if (!isset($services[$srv->type][$endpointRegion][$srvVersion])) {
                     $services[$srv->type][$endpointRegion][$srvVersion] = [];
                 }
                 $srvEndpoint->publicURL = $url;
                 //Interface - can be public, internal or admin
                 $services[$srv->type][$endpointRegion][$srvVersion][$srvEndpoint->interface] = $srvEndpoint;
             }
         }
     }
     $regions = array_keys($regions);
     $ret = new AuthToken();
     $ret->setExpires(new DateTime($obj->token->expires_at))->setId($token)->setAuthDocument($obj)->setRegionEndpoints($services)->setZones($regions);
     if (isset($obj->token->project->id)) {
         $ret->setTenantId($obj->token->project->id);
     }
     if (isset($obj->token->project->name)) {
         $ret->setTenantName($obj->token->project->name);
     }
     return $ret;
 }
示例#4
0
 /**
  * {@inheritdoc}
  * @see LoaderInterface::loadJson()
  */
 public static function loadJson(ClientResponseInterface $response)
 {
     $jsonString = $response->getContent();
     $obj = json_decode($jsonString);
     if (!isset($obj->access->token)) {
         $invalid = true;
     }
     if (isset($invalid) || !isset($obj->access->token->expires) || !isset($obj->access->token->id)) {
         throw new InvalidArgumentException("Malformed JSON document " . (string) $jsonString);
     }
     $services = array();
     $regions = array();
     if (!empty($obj->access->serviceCatalog)) {
         foreach ($obj->access->serviceCatalog as $srv) {
             foreach ($srv->endpoints as $srvEndpoint) {
                 $srvVersion = isset($srvEndpoint->versionId) ? $srvEndpoint->versionId . '' : '';
                 if (isset($srvEndpoint->region)) {
                     $regions[$srvEndpoint->region] = true;
                     $endpointRegion = $srvEndpoint->region;
                 } else {
                     $endpointRegion = '';
                 }
                 if (!isset($services[$srv->type][$endpointRegion][$srvVersion])) {
                     $services[$srv->type][$endpointRegion][$srvVersion] = array();
                 }
                 $services[$srv->type][$endpointRegion][$srvVersion][] = $srvEndpoint;
             }
         }
     }
     $regions = array_keys($regions);
     $ret = new AuthToken();
     $ret->setExpires(new DateTime($obj->access->token->expires))->setId($obj->access->token->id)->setAuthDocument($obj)->setRegionEndpoints($services)->setZones($regions);
     if (isset($obj->access->token->tenant->id)) {
         $ret->setTenantId($obj->access->token->tenant->id);
     }
     if (isset($obj->access->token->tenant->name)) {
         $ret->setTenantName($obj->access->token->tenant->name);
     }
     return $ret;
 }
示例#5
0
 /**
  * Performs an authentication request
  *
  * @return  object Returns result
  */
 public function auth()
 {
     $cfg = $this->getConfig();
     $options = ['auth' => $cfg->getAuthQueryString()];
     $response = $this->call($cfg->getIdentityEndpoint(), '/tokens', $options, 'POST', null, false);
     if ($response->hasError() === false) {
         $str = $response->getContent();
         $result = json_decode($str);
         $cfg->setAuthToken(AuthToken::loadJson($str));
         //Trying to fetch stage keystone
         $regionEndpoints = $cfg->getAuthToken()->getRegionEndpoints();
         /*
         if (isset($regionEndpoints['identity'][$cfg->getRegion()][''][0]->publicURL)) {
             $regionKeystoneURL = $regionEndpoints['identity'][$cfg->getRegion()][''][0]->publicURL;
         
             if ($cfg->getIdentityEndpoint() != $regionKeystoneURL) {
                 $response = $this->call($regionKeystoneURL, '/tokens', $options, 'POST', null, false);
         
                 if ($response->hasError() === false) {
                     $str = $response->getContent();
                     $result = json_decode($str);
         
                     //Sets original auth token for current region
                     $cfg->setAuthToken(AuthToken::loadJson($str));
                     $cfg->setIdentityEndpoint($regionKeystoneURL);
                 }
             }
         }
         */
     }
     return $result;
 }
示例#6
0
 /**
  * Performs an authentication request
  *
  * @return  object Returns result
  */
 public function auth()
 {
     $cfg = $this->getConfig();
     $options = array('auth' => $cfg->getAuthQueryString());
     $response = $this->call($cfg->getIdentityEndpoint(), '/tokens', $options, 'POST', null, false);
     if ($response->hasError() === false) {
         $str = $response->getContent();
         $result = json_decode($str);
         $cfg->setAuthToken(AuthToken::loadJson($str));
         if (($cb = $cfg->getUpdateTokenCallback()) !== null && is_callable($cb)) {
             //It should save token in database
             $cb($cfg->getAuthToken());
         }
     }
     return $result;
 }