Added an IdentityServices factory to Bootstrap from Bootstrap config. Calling Bootstrap::identity() will return an IdentityServices object. If the token has expired it will get a new one.

This commit is contained in:
Matt Farina
2012-06-12 09:52:58 -04:00
parent 0d8c38ef7a
commit 51a2872512
2 changed files with 107 additions and 0 deletions

View File

@@ -29,6 +29,9 @@ SOFTWARE.
namespace HPCloud;
use HPCloud\Services\IdentityServices;
use HPCloud\Exception;
/**
* Bootstrapping services.
*
@@ -128,6 +131,12 @@ class Bootstrap {
'transport' => '\HPCloud\Transport\CURLTransport',
);
/**
* An identity services object created from the global settings.
* @var [type]
*/
public static $identity = NULL;
/**
* Add the autoloader to PHP's autoloader list.
*
@@ -313,4 +322,61 @@ class Bootstrap {
public static function hasConfig($name) {
return isset(self::$config[$name]);
}
/**
* Get a HPCloud::Services::IdentityService object from the bootstrap config.
*
* A factory helper function that uses the bootstrap configuration to create
* a ready to use HPCloud::Services::IdentityService object.
*
* @param bool $force
* Whether to force the generation of a new object even if one is already
* cached.
* @retval HPCloud::Services::IdentityService
* An authenticated ready to use HPCloud::Services::IdentityService object.
* @throws HPCloud::Exception
* When the needed configuration to authenticate is not available.
*/
public static function identity($force = FALSE) {
// If we already have an identity make sure the token is not expired.
$expired = FALSE;
if (!is_null(self::$identity)) {
// Make sure the token we have is not expired.
$tokenDetails = self::$identity->tokenDetails();
$tokenExpires = new \DateTime($tokenDetails['expires']);
$currentDateTime = new \DateTime('now');
if ($currentDateTime > $tokenExpires) {
$expired = TRUE;
}
}
if (is_null(self::$identity) || $expired || $force) {
// Make sure we have an endpoint to use
if (!self::hasConfig('endpoint')) {
throw new Exception('Unable to authenticate. No endpoint supplied.');
}
// Check if we have a username/password
if (self::hasConfig('username') && self::hasConfig('password')) {
$is = new IdentityServices(self::config('endpoint'));
$is->authenticateAsUser(self::config('username'), self::config('password'), self::config('tenantid', NULL));
self::$identity = $is;
}
// Otherwise we go with access/secret keys
elseif (self::hasConfig('account') && self::hasConfig('key')) {
$is = new IdentityServices(self::config('endpoint'));
$is->authenticateAsAccount(self::config('account'), self::config('key'), self::config('tenantid', NULL));
self::$identity = $is;
}
else {
throw new Exception('Unable to authenticate. No account credentials supplied.');
}
}
return self::$identity;
}
}

View File

@@ -30,6 +30,7 @@ require_once 'src/HPCloud/Bootstrap.php';
require_once 'test/TestCase.php';
use \HPCloud\Services\IdentityServices;
use \HPCloud\Bootstrap;
class IdentityServicesTest extends \HPCloud\Tests\TestCase {
@@ -335,4 +336,44 @@ class IdentityServicesTest extends \HPCloud\Tests\TestCase {
$this->assertEquals(1, count($catalog));
}
/**
* Test the bootstrap identity factory.
* @depends testAuthenticateAsAccount
* @depends testAuthenticateAsUser
*/
function testBootstrap() {
// Test authenticating as a user.
$settings = array(
'username' => self::conf('hpcloud.identity.username'),
'password' => self::conf('hpcloud.identity.password'),
'endpoint' => self::conf('hpcloud.identity.url'),
'tenantid' => self::conf('hpcloud.identity.tenantId'),
);
Bootstrap::setConfiguration($settings);
$is = Bootstrap::identity(TRUE);
$this->assertInstanceOf('\HPCloud\Services\IdentityServices', $is);
// Test authenticating as an account.
$settings = array(
'account' => self::conf('hpcloud.identity.account'),
'key' => self::conf('hpcloud.identity.secret'),
'endpoint' => self::conf('hpcloud.identity.url'),
'tenantid' => self::conf('hpcloud.identity.tenantId'),
);
Bootstrap::setConfiguration($settings);
$is = Bootstrap::identity(TRUE);
$this->assertInstanceOf('\HPCloud\Services\IdentityServices', $is);
// Test getting a second instance from the cache.
$is2 = Bootstrap::identity();
$this->assertEquals($is, $is2);
// Test that forcing a refresh does so.
$is2 = Bootstrap::identity(TRUE);
$this->assertNotEquals($is, $is2);
}
}