Skip to content

Implement some changes based on parent branch PR review #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: universal-clock
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -6,3 +6,4 @@ testing/
nbproject/private/
test/log
build
.phpunit.result.cache
9 changes: 4 additions & 5 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@
*
* @link http://tools.ietf.org/html/rfc6749#section-1.1 Roles (RFC 6749, §1.1)
*/
abstract class AbstractProvider
abstract class AbstractProvider implements ClockAwareInterface
{
use ArrayAccessorTrait;
use GuardedPropertyTrait;
@@ -265,11 +265,10 @@ public function getOptionProvider()
/**
* Sets the clock.
*
* @param Clock $clock
*
* @param ClockInterface $clock the clock
* @return self
*/
public function setClock(Clock $clock)
public function setClock(ClockInterface $clock)
{
$this->clock = $clock;

@@ -284,7 +283,7 @@ public function setClock(Clock $clock)
*/
public function getClock()
{
return $this->clock;
return $this->clock ? $this->clock : new Clock();
}

/**
2 changes: 1 addition & 1 deletion src/Provider/Clock.php
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
/**
* Represents an implementation of a Clock.
*/
class Clock
final class Clock implements ClockInterface
{

/**
13 changes: 13 additions & 0 deletions src/Provider/ClockAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace League\OAuth2\Client\Provider;

interface ClockAwareInterface
{
/**
* Get the clock.
*
* @return ClockInterface
*/
public function getClock();
}
19 changes: 19 additions & 0 deletions src/Provider/ClockInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace League\OAuth2\Client\Provider;

/**
* A common interface for accessing the clock.
*
* NOTE: We don't have a return type hint because this library still supports PHP 5.6.
* @see https://github.com/php-fig/fig-standards/blob/master/proposed/clock.md
*/
interface ClockInterface
{
/**
* Return the current time as a DateTimeImmutable Object.
*
* @return \DateTimeImmutable
*/
public function now(); /* \DateTimeImmutable */
}
64 changes: 14 additions & 50 deletions src/Token/AccessToken.php
Original file line number Diff line number Diff line change
@@ -16,14 +16,18 @@

use InvalidArgumentException;
use League\OAuth2\Client\Provider\Clock;
use League\OAuth2\Client\Provider\ClockAwareInterface;
use League\OAuth2\Client\Provider\ClockInterface;
use RuntimeException;



/**
* Represents an access token.
*
* @link http://tools.ietf.org/html/rfc6749#section-1.4 Access Token (RFC 6749, §1.4)
*/
class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInterface
class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInterface, ClockAwareInterface
{
/**
* @var string
@@ -50,61 +54,19 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter
*/
protected $values = [];

/**
* The current time, or NULL to get the true current time via PHP.
*
* @var int|null
*/
private static $timeNow;

/**
* The clock.
*
* @var Clock
* @var ClockInterface
*/
protected $clock;

/**
* Sets the current time.
*
* @param int $timeNow the time in seconds since epoch
* @return void
*/
public static function setTimeNow($timeNow)
{
self::$timeNow = $timeNow;
}

/**
* Reset the current time so the true current time via PHP is used.
*
* @return void
* {@inheritDoc}
*/
public static function resetTimeNow()
public function getClock()
{
self::$timeNow = null;
}

/**
* @inheritdoc
*/
public function setClock(Clock $clock)
{
$this->clock = $clock;
}

/**
* @inheritdoc
*/
public function getTimeNow()
{
if (self::$timeNow) {
return self::$timeNow;
} elseif (isset($this->clock)) {
return $this->clock->now()->getTimestamp();
} else {
return time();
}
return $this->clock ? $this->clock : new Clock();
}

/**
@@ -134,6 +96,8 @@ public function __construct(array $options = [])
$this->clock = $options['clock'];
}

$ts = $this->getClock()->now()->getTimestamp();

// We need to know when the token expires. Show preference to
// 'expires_in' since it is defined in RFC6749 Section 5.1.
// Defer to 'expires' if it is provided instead.
@@ -142,14 +106,14 @@ public function __construct(array $options = [])
throw new \InvalidArgumentException('expires_in value must be an integer');
}

$this->expires = $options['expires_in'] != 0 ? $this->getTimeNow() + $options['expires_in'] : 0;
$this->expires = $options['expires_in'] != 0 ? $ts + $options['expires_in'] : 0;
} elseif (!empty($options['expires'])) {
// Some providers supply the seconds until expiration rather than
// the exact timestamp. Take a best guess at which we received.
$expires = $options['expires'];

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

$this->expires = $expires;
@@ -224,7 +188,7 @@ public function hasExpired()
throw new RuntimeException('"expires" is not set on the token');
}

return $expires < $this->getTimeNow();
return $expires < $this->getClock()->now()->getTimestamp();
}

/**
18 changes: 1 addition & 17 deletions src/Token/AccessTokenInterface.php
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
namespace League\OAuth2\Client\Token;

use JsonSerializable;
use League\OAuth2\Client\Provider\Clock;
use League\OAuth2\Client\Provider\ClockInterface;
use RuntimeException;

interface AccessTokenInterface extends JsonSerializable
@@ -70,20 +70,4 @@ public function __toString();
* @return array
*/
public function jsonSerialize();

/**
* Sets the clock.
*
* @param Clock $clock a clock.
*
* @return void
*/
public function setClock(Clock $clock);

/**
* Get the current time, whether real or simulated.
*
* @return int
*/
public function getTimeNow();
}
2 changes: 1 addition & 1 deletion src/Tool/MacAuthorizationTrait.php
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@

namespace League\OAuth2\Client\Tool;

use League\OAuth2\Client\Provider\Clock;
use League\OAuth2\Client\Provider\ClockInterface;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Token\AccessTokenInterface;

2 changes: 1 addition & 1 deletion test/src/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
@@ -571,7 +571,7 @@ public function testGetAccessToken($method)
$newTime = new \DateTimeImmutable('2nd February 2013 1pm');
$clock->setTime($newTime);

$this->assertEquals($newTime->getTimestamp(), $token->getTimeNow());
$this->assertEquals($newTime->getTimestamp(), $token->getClock()->now()->getTimestamp());

$client
->shouldHaveReceived('send')
4 changes: 3 additions & 1 deletion test/src/Provider/FrozenClock.php
Original file line number Diff line number Diff line change
@@ -3,11 +3,13 @@
namespace League\OAuth2\Client\Test\Provider;

use League\OAuth2\Client\Provider\Clock;
use League\OAuth2\Client\Provider\ClockInterface;


/**
* A clock with a frozen time for testing.
*/
class FrozenClock extends Clock
class FrozenClock implements ClockInterface
{

/**
15 changes: 13 additions & 2 deletions test/src/Provider/ProgrammableClock.php
Original file line number Diff line number Diff line change
@@ -2,12 +2,13 @@

namespace League\OAuth2\Client\Test\Provider;

use League\OAuth2\Client\Provider\Clock;
use League\OAuth2\Client\Provider\ClockInterface;


/**
* A clock which must be initialised, and may be changed at any time.
*/
class ProgrammableClock extends Clock
class ProgrammableClock implements ClockInterface
{

/**
@@ -38,4 +39,14 @@ public function setTime($time)

return $this;
}

/**
* @param int $time seconds since epoch
* @return self
*/
public static function newFromTimestamp($time)
{
return (new self())
->setTime(new \DateTimeImmutable('@' . $time));
}
}
Loading
Oops, something went wrong.