End of comments

This commit is contained in:
EoleDev 2016-04-27 14:22:59 +02:00
parent 5263cf00a2
commit 1d42345e07
13 changed files with 491 additions and 264 deletions

View file

@ -1,4 +1,14 @@
<?php
/**
* File containing the override of the authentication for the Library.
*
* @version 1.0 Initialisation of this file
* @since 1.0 Core application's file
*
* @author Eole 'eoledev at outlook . fr'
*
* @todo Check with the API, the condition and test the revoke token implementation
*/
use GuzzleHttp\Client;
use OpenCloud\Common\Transport\HandlerStack;
@ -9,21 +19,38 @@ use OpenCloud\Common\Auth\Token;
use OpenCloud\Common\Transport\Utils;
use OpenStack\Identity\v3\Models;
/**
* genTokenOptions Class
*
* This class allow the generation of tokens for openstack, and to inject
* those tokens into the library. Which allow to do a proper login only once
* and not for each request
*
*/
class genTokenOptions
{
/** @var Array $optionsGlobal private, contains the options common for the different tokens */
private $optionsGlobal;
private $stack;
/** @var Array $backup private, contains all the informations about the different tokens. It contains the information send to the clients */
private $backup = [];
/** @var GuzzleHttp\Client $httpClient private, contains a default Client to construct some OpenStack library object */
private $httpClient;
/**
* genTokenOptions constructor
*
* @param Array $options Options to create the objects in the library
* AuthUrl is the main options required
*
* @return genTokenOptions Object
*/
public function __construct($options){
$this->stack = HandlerStack::create();
$stack = HandlerStack::create();
$httpClient = new Client([
'base_uri' => Utils::normalizeUrl($options['authUrl']),
'handler' => $this->stack,
'handler' => $stack,
]);
$this->httpClient = $httpClient;
@ -38,8 +65,13 @@ class genTokenOptions
}
/**
* @codeCoverageIgnore
*/
* Add a debug for the library
*
* @param array $options Debug options, cf library
* @param HandlerStack $stack pointer to a HandlerStack object
*
* @return void
*/
private function addDebugMiddleware(array $options, HandlerStack &$stack)
{
if (!empty($options['debugLog'])
@ -50,12 +82,20 @@ class genTokenOptions
}
}
/**
* Check the expiration time of a token
*
* @return boolean if the token is not expired
*/
public function checkToken(){
//error_log(print_r($this->backup['time'], true), 0);
return $this->backup['time'] > time();
//return true;
}
/**
* Generate a new token for the Identity service
*
* @return void
*/
public function genIdentityToken(){
$options = $this->optionsGlobal['Common'];
$options['catalogName'] = 'false';
@ -81,12 +121,24 @@ class genTokenOptions
$this->optionsGlobal['Identity'] = $options;
}
/**
* Revoke the token for the Identity Service
*
* @return void
*/
public function revokeIdentityToken(){
$token = $this->unserializeToken($this->backup['Identity']['token']);
$this->optionsGlobal['Common']['identityService']->revokeToken($token->id);
}
/**
* Load a token for the Identity Service
*
* @param String $opt serialized token
*
* @return void
*/
public function loadIdentityBackup($opt){
$options = $this->optionsGlobal['Common'];
$options['catalogName'] = 'false';
@ -112,6 +164,11 @@ class genTokenOptions
}
/**
* Generate a new token for the Image service
*
* @return void
*/
public function genImageToken(){
$options = $this->optionsGlobal['Common'];
$options['catalogName'] = 'glance';
@ -135,12 +192,24 @@ class genTokenOptions
$this->optionsGlobal['Image'] = $options;
}
/**
* Revoke the token for the Image Service
*
* @return void
*/
public function revokeImageToken(){
$token = $this->unserializeToken($this->backup['Image']['token']);
$this->optionsGlobal['Common']['identityService']->revokeToken($token->id);
}
/**
* Load a token for the Image Service
*
* @param String $opt serialized token
*
* @return void
*/
public function loadImageBackup($opt){
$options = $this->optionsGlobal['Common'];
$options['catalogName'] = 'glance';
@ -165,6 +234,11 @@ class genTokenOptions
$this->optionsGlobal['Image'] = $options;
}
/**
* Generate a new token for the Metwork service
*
* @return void
*/
public function genNetworkToken(){
$options = $this->optionsGlobal['Common'];
$options['catalogName'] = 'neutron';
@ -188,12 +262,24 @@ class genTokenOptions
$this->optionsGlobal['Network'] = $options;
}
/**
* Revoke the token for the Network Service
*
* @return void
*/
public function revokeNetworkToken(){
$token = $this->unserializeToken($this->backup['Network']['token']);
$this->optionsGlobal['Common']['identityService']->revokeToken($token->id);
}
/**
* Load a token for the Network Service
*
* @param String $opt serialized token
*
* @return void
*/
public function loadNetworkBackup($opt){
$options = $this->optionsGlobal['Common'];
$options['catalogName'] = 'neutron';
@ -218,6 +304,11 @@ class genTokenOptions
$this->optionsGlobal['Network'] = $options;
}
/**
* Generate a new token for the Compute service
*
* @return void
*/
public function genComputeToken(){
$options = $this->optionsGlobal['Common'];
$options['catalogName'] = 'nova';
@ -241,12 +332,24 @@ class genTokenOptions
$this->optionsGlobal['Compute'] = $options;
}
/**
* Revoke the token for the Compute Service
*
* @return void
*/
public function revokeComputeToken(){
$token = $this->unserializeToken($this->backup['Compute']['token']);
$this->optionsGlobal['Common']['identityService']->revokeToken($token->id);
}
/**
* Load a token for the Compute Service
*
* @param String $opt serialized token
*
* @return void
*/
public function loadComputeBackup($opt){
$options = $this->optionsGlobal['Common'];
@ -272,10 +375,16 @@ class genTokenOptions
$this->optionsGlobal['Compute'] = $options;
}
/**
* Save the token given a service name
*
* @param String $name name of the service to save
* @param Array $data token and baseUrl for the service
*
* @return void
*/
private function saveBackup($name, $data){
$token = $this->serializeToken($data["token"]);
//$path = "core/LibOverride/projectTokenData/".$token['saved']["project"]["name"];
//error_log("Path a ecrire ".print_r($path, true), 0);
file_put_contents("core/LibOverride/projectTokenData/".$token['saved']["project"]["name"], serialize($token['saved']));
$this->backup['time'] = $token['time'];
$this->backup["roles"] = $token["roles"];
@ -284,10 +393,22 @@ class genTokenOptions
$this->backup[$name] = array('token' => $token["token"], 'baseUrl' => $data["baseUrl"] );
}
/**
* Retrieve the tokens saved
*
* @return String tokens serialized
*/
public function getBackup(){
return serialize($this->backup);
}
/**
* Load tokens into the library
*
* @param String $back tokens serialized
*
* @return void
*/
public function loadBackup($back){
$backup = unserialize($back);
@ -302,10 +423,24 @@ class genTokenOptions
}
/**
* Retrieve the common options for a service
*
* @param String $service name of the service
*
* @return array Options to create the library class corresponding to this service
*/
public function getOptions($service){
return $this->optionsGlobal[$service];
}
/**
* Serialize a given token
*
* @param Array $token token to be serialized
*
* @return String token serialized
*/
private function serializeToken($token){
global $config;
$tokenSerialized = [];
@ -358,6 +493,15 @@ class genTokenOptions
return $tokenSerialized;
}
/**
* Unserialize a token
*
* Unserialize a token and recreate the architecture of the library token
*
* @param String $tokenSerialized the token to be unserialized
*
* @return OpenCloud\Common\Auth\Token the token unserialized
*/
private function unserializeToken($tokenSerialized){
$Saved = file_get_contents("core/LibOverride/projectTokenData/".$this->backup["project"]);
$Saved = unserialize($Saved);