diff options
Diffstat (limited to 'server/core/LibOverride/genTokenOptions.php')
| -rwxr-xr-x | server/core/LibOverride/genTokenOptions.php | 268 |
1 files changed, 208 insertions, 60 deletions
diff --git a/server/core/LibOverride/genTokenOptions.php b/server/core/LibOverride/genTokenOptions.php index 394db31..7134887 100755 --- a/server/core/LibOverride/genTokenOptions.php +++ b/server/core/LibOverride/genTokenOptions.php @@ -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,53 +19,83 @@ 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, - ]); + 'base_uri' => Utils::normalizeUrl($options['authUrl']), + 'handler' => $stack, + ]); $this->httpClient = $httpClient; - $options['identityService'] = Service::factory($httpClient); + $options['identityService'] = Service::factory($httpClient); $options['authHandler'] = function () use ($options) { return $options['identityService']->generateToken($options); - }; + }; $this->optionsGlobal['Common'] = $options; } - - /** - * @codeCoverageIgnore - */ - private function addDebugMiddleware(array $options, HandlerStack &$stack) - { - if (!empty($options['debugLog']) - && !empty($options['logger']) - && !empty($options['messageFormatter']) - ) { - $stack->push(GuzzleMiddleware::log($options['logger'], $options['messageFormatter'])); - } - } + /** + * 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']) + && !empty($options['logger']) + && !empty($options['messageFormatter']) + ) { + $stack->push(GuzzleMiddleware::log($options['logger'], $options['messageFormatter'])); + } + } + + /** + * 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'; @@ -73,20 +113,32 @@ class genTokenOptions $this->addDebugMiddleware($options, $stack); $options['httpClient'] = new Client([ - 'base_uri' => Utils::normalizeUrl($baseUrl), - 'handler' => $stack, - ]); + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); $this->saveBackup('Identity', array('token' => $token, 'baseUrl' => $baseUrl )); $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'; @@ -98,20 +150,25 @@ class genTokenOptions $baseUrl = $this->backup['Identity']['baseUrl']; $stack = HandlerStack::create(); - + $stack->push(Middleware::authHandler($options['authHandler'], $token)); $this->addDebugMiddleware($options, $stack); $options['httpClient'] = new Client([ - 'base_uri' => Utils::normalizeUrl($baseUrl), - 'handler' => $stack, - ]); + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); $this->saveBackup('Identity', array('token' => $token, 'baseUrl' => $baseUrl )); $this->optionsGlobal['Identity'] = $options; } + /** + * Generate a new token for the Image service + * + * @return void + */ public function genImageToken(){ $options = $this->optionsGlobal['Common']; $options['catalogName'] = 'glance'; @@ -122,25 +179,37 @@ class genTokenOptions $stack = HandlerStack::create(); - $stack->push(Middleware::authHandler($options['authHandler'], $token)); + $stack->push(Middleware::authHandler($options['authHandler'], $token)); $this->addDebugMiddleware($options, $stack); $options['httpClient'] = new Client([ - 'base_uri' => Utils::normalizeUrl($baseUrl), - 'handler' => $stack, - ]); + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); $this->saveBackup('Image', array('token' => $token, 'baseUrl' => $baseUrl )); $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'; @@ -150,7 +219,7 @@ class genTokenOptions $this->backup['Image'] = $opt; $token = $this->unserializeToken($this->backup['Image']['token']); $baseUrl = $this->backup['Image']['baseUrl']; - + $stack = HandlerStack::create(); $stack->push(Middleware::authHandler($options['authHandler'], $token)); @@ -158,13 +227,18 @@ class genTokenOptions $this->addDebugMiddleware($options, $stack); $options['httpClient'] = new Client([ - 'base_uri' => Utils::normalizeUrl($baseUrl), - 'handler' => $stack, - ]); + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); $this->saveBackup('Image', array('token' => $token, 'baseUrl' => $baseUrl )); $this->optionsGlobal['Image'] = $options; } + /** + * Generate a new token for the Metwork service + * + * @return void + */ public function genNetworkToken(){ $options = $this->optionsGlobal['Common']; $options['catalogName'] = 'neutron'; @@ -175,25 +249,37 @@ class genTokenOptions $stack = HandlerStack::create(); - $stack->push(Middleware::authHandler($options['authHandler'], $token)); + $stack->push(Middleware::authHandler($options['authHandler'], $token)); $this->addDebugMiddleware($options, $stack); $options['httpClient'] = new Client([ - 'base_uri' => Utils::normalizeUrl($baseUrl), - 'handler' => $stack, - ]); + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); $this->saveBackup('Network', array('token' => $token, 'baseUrl' => $baseUrl )); $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'; @@ -203,21 +289,26 @@ class genTokenOptions $this->backup['Network'] = $opt; $token = $this->unserializeToken($this->backup['Network']['token']); $baseUrl = $this->backup['Network']['baseUrl']; - + $stack = HandlerStack::create(); - + $stack->push(Middleware::authHandler($options['authHandler'], $token)); $this->addDebugMiddleware($options, $stack); $options['httpClient'] = new Client([ - 'base_uri' => Utils::normalizeUrl($baseUrl), - 'handler' => $stack, - ]); + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); $this->saveBackup('Network', array('token' => $token, 'baseUrl' => $baseUrl )); $this->optionsGlobal['Network'] = $options; } + /** + * Generate a new token for the Compute service + * + * @return void + */ public function genComputeToken(){ $options = $this->optionsGlobal['Common']; $options['catalogName'] = 'nova'; @@ -228,25 +319,37 @@ class genTokenOptions $stack = HandlerStack::create(); - $stack->push(Middleware::authHandler($options['authHandler'], $token)); + $stack->push(Middleware::authHandler($options['authHandler'], $token)); $this->addDebugMiddleware($options, $stack); $options['httpClient'] = new Client([ - 'base_uri' => Utils::normalizeUrl($baseUrl), - 'handler' => $stack, - ]); + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); $this->saveBackup('Compute', array('token' => $token, 'baseUrl' => $baseUrl )); $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']; @@ -257,26 +360,34 @@ class genTokenOptions $this->backup['Compute'] = $opt; $token = $this->unserializeToken($this->backup['Compute']['token']); $baseUrl = $this->backup['Compute']['baseUrl']; - + $stack = HandlerStack::create(); - + $stack->push(Middleware::authHandler($options['authHandler'], $token)); $this->addDebugMiddleware($options, $stack); $options['httpClient'] = new Client([ - 'base_uri' => Utils::normalizeUrl($baseUrl), - 'handler' => $stack, - ]); + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); $this->saveBackup('Compute', array('token' => $token, 'baseUrl' => $baseUrl )); $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'])); + $ret = file_put_contents("core/LibOverride/projectTokenData/".$token['saved']["project"]["name"], serialize($token['saved'])); + if($ret === FALSE) + die("Internal Server Error : File Rights"); $this->backup['time'] = $token['time']; $this->backup["roles"] = $token["roles"]; $this->backup["project"] = $token['saved']["project"]["name"]; @@ -284,10 +395,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 +425,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,8 +495,19 @@ 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"]); + if($Saved === FALSE) + die("Internal Server Error : File Access"); $Saved = unserialize($Saved); $api = new Api(); @@ -426,7 +574,7 @@ class genTokenOptions $token->user->description = unserialize($this->backup["user"]["description"]); $token->issued = unserialize($tokenSerialized["issued"]); $token->id = unserialize($tokenSerialized["id"]); - + return $token; } } |
