Skip to content

Commit

Permalink
Merge pull request #434 from shadowhand/hotfix/431-access-token-stora…
Browse files Browse the repository at this point in the history
…ge-expiration

Allow access tokens to be created from storage
  • Loading branch information
ramsey committed Sep 22, 2015
2 parents 7d9e3f6 + 3916c41 commit f9099b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Token/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,28 @@ public function __construct(array $options = [])
// Some providers supply the seconds until expiration rather than
// the exact timestamp. Take a best guess at which we received.
$expires = $options['expires'];
$expiresInFuture = $expires > time();
$this->expires = $expiresInFuture ? $expires : time() + ((int) $expires);

if (!$this->isExpirationTimestamp($expires)) {
$expires += time();
}

$this->expires = $expires;
}
}

/**
* Check if a value is an expiration timestamp or second value.
*
* @return boolean
*/
protected function isExpirationTimestamp($value)
{
// If the given value is larger than the original OAuth 2 draft date,
// assume that it is meant to be a (possible expired) timestamp.
$oauth2InceptionDate = 1349067600; // 2012-10-01
return ($value > $oauth2InceptionDate);
}

/**
* Returns the access token string of this instance.
*
Expand Down
13 changes: 13 additions & 0 deletions test/src/Token/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public function testExpiresInCorrection()
$this->assertLessThan(time() + 200, $expires);
}

public function testExpiresPastTimestamp()
{
$options = ['access_token' => 'access_token', 'expires' => strtotime('5 days ago')];
$token = $this->getAccessToken($options);

$this->assertTrue($token->hasExpired());

$options = ['access_token' => 'access_token', 'expires' => 3600];
$token = $this->getAccessToken($options);

$this->assertFalse($token->hasExpired());
}

public function testGetRefreshToken()
{
$options = [
Expand Down

0 comments on commit f9099b2

Please sign in to comment.