From 4a0dc552411c88f2d6ce1547b891e7d443f0a3b4 Mon Sep 17 00:00:00 2001 From: Jamie Hannaford Date: Thu, 4 Dec 2014 10:53:15 +0000 Subject: [PATCH] Ensure token header is reset before authenticating; fixes #483 --- lib/OpenCloud/OpenStack.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/OpenCloud/OpenStack.php b/lib/OpenCloud/OpenStack.php index fc1ad9c49..9f6cbf36c 100644 --- a/lib/OpenCloud/OpenStack.php +++ b/lib/OpenCloud/OpenStack.php @@ -370,6 +370,10 @@ public function getUser() */ public function authenticate() { + // OpenStack APIs will return a 401 if an expired X-Auth-Token is sent, + // so we need to reset the value before authenticating for another one. + $this->updateTokenHeader(''); + $identity = IdentityService::factory($this); $response = $identity->generateToken($this->getCredentials()); @@ -384,7 +388,7 @@ public function authenticate() } // Set X-Auth-Token HTTP request header - $this->updateTokenHeader(); + $this->updateTokenHeader($this->getToken()); } /** @@ -422,7 +426,7 @@ public function importCredentials(array $values) { if (!empty($values['token'])) { $this->setToken($values['token']); - $this->updateTokenHeader(); + $this->updateTokenHeader($this->getToken()); } if (!empty($values['expiration'])) { $this->setExpiration($values['expiration']); @@ -438,13 +442,12 @@ public function importCredentials(array $values) /** * Sets the X-Auth-Token header. If no value is explicitly passed in, the current token is used. * - * @param string $token Optional value of token. + * @param string $token Value of header. * @return void */ - private function updateTokenHeader($token = null) + private function updateTokenHeader($token) { - $token = $token ? : $this->getToken(); - $this->setDefaultOption('headers/X-Auth-Token', (string)$token); + $this->setDefaultOption('headers/X-Auth-Token', (string) $token); } /**