diff --git a/src/HPCloud/Services/IdentityServices.php b/src/HPCloud/Services/IdentityServices.php index 9ed9c65..492e03f 100644 --- a/src/HPCloud/Services/IdentityServices.php +++ b/src/HPCloud/Services/IdentityServices.php @@ -483,6 +483,31 @@ class IdentityServices { return $this->tokenDetails; } + /** + * Check whether the current identity has an expired token. + * + * This does not perform a round-trip to the server. Instead, it compares the + * machine's local timestamp with the server's expiration time stamp. A + * mis-configured machine timestamp could give spurious results. + * + * @retval boolean + * This will return FALSE if there is a current token and it has + * not yet expired (according to the date info). In all other cases + * it returns TRUE. + */ + public function isExpired() { + $details = $this->tokenDetails(); + + if (empty($details['expires'])) { + return TRUE; + } + + $currentDateTime = new \DateTime('now'); + $expireDateTime = new \DateTime($details['expires']); + + return $currentDateTime > $expireDateTime; + } + /** * Get the service catalog, optionaly filtering by type. * diff --git a/test/Tests/IdentityServicesTest.php b/test/Tests/IdentityServicesTest.php index 88559de..d624e04 100644 --- a/test/Tests/IdentityServicesTest.php +++ b/test/Tests/IdentityServicesTest.php @@ -170,6 +170,16 @@ class IdentityServicesTest extends \HPCloud\Tests\TestCase { $this->assertNotEmpty($service->token()); } + /** + * @depends testAuthenticateAsAccount + */ + public function testIsExpired($service) { + $this->assertFalse($service->isExpired()); + + $service2 = new IdentityServices(self::conf('hpcloud.identity.url')); + $this->assertTrue($service2->isExpired()); + } + /** * @depends testAuthenticateAsAccount */