Skip to content

Commit dbb8460

Browse files
committed
Merge pull request #484 from jamiehannaford/token-fix
Ensure token header is reset before authenticating; fixes #483
2 parents 1b0366a + 4a0dc55 commit dbb8460

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/OpenCloud/OpenStack.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ public function getUser()
370370
*/
371371
public function authenticate()
372372
{
373+
// OpenStack APIs will return a 401 if an expired X-Auth-Token is sent,
374+
// so we need to reset the value before authenticating for another one.
375+
$this->updateTokenHeader('');
376+
373377
$identity = IdentityService::factory($this);
374378
$response = $identity->generateToken($this->getCredentials());
375379

@@ -384,7 +388,7 @@ public function authenticate()
384388
}
385389

386390
// Set X-Auth-Token HTTP request header
387-
$this->updateTokenHeader();
391+
$this->updateTokenHeader($this->getToken());
388392
}
389393

390394
/**
@@ -422,7 +426,7 @@ public function importCredentials(array $values)
422426
{
423427
if (!empty($values['token'])) {
424428
$this->setToken($values['token']);
425-
$this->updateTokenHeader();
429+
$this->updateTokenHeader($this->getToken());
426430
}
427431
if (!empty($values['expiration'])) {
428432
$this->setExpiration($values['expiration']);
@@ -438,13 +442,12 @@ public function importCredentials(array $values)
438442
/**
439443
* Sets the X-Auth-Token header. If no value is explicitly passed in, the current token is used.
440444
*
441-
* @param string $token Optional value of token.
445+
* @param string $token Value of header.
442446
* @return void
443447
*/
444-
private function updateTokenHeader($token = null)
448+
private function updateTokenHeader($token)
445449
{
446-
$token = $token ? : $this->getToken();
447-
$this->setDefaultOption('headers/X-Auth-Token', (string)$token);
450+
$this->setDefaultOption('headers/X-Auth-Token', (string) $token);
448451
}
449452

450453
/**

0 commit comments

Comments
 (0)