-
Notifications
You must be signed in to change notification settings - Fork 754
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
Allow time to be fetched from a universal clock ⏱ #866
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace League\OAuth2\Client\Provider; | ||
|
||
/** | ||
* Represents an implementation of a Clock. | ||
*/ | ||
class Clock | ||
{ | ||
|
||
/** | ||
* Get the current time. | ||
* | ||
* @return \DateTimeImmutable | ||
*/ | ||
public function now() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this method should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
return new \DateTimeImmutable(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
namespace League\OAuth2\Client\Token; | ||
|
||
use InvalidArgumentException; | ||
use League\OAuth2\Client\Provider\Clock; | ||
use RuntimeException; | ||
|
||
/** | ||
|
@@ -50,12 +51,21 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter | |
protected $values = []; | ||
|
||
/** | ||
* @var int | ||
* The current time, or NULL to get the true current time via PHP. | ||
* | ||
* @var int|null | ||
*/ | ||
private static $timeNow; | ||
|
||
/** | ||
* Set the time now. This should only be used for testing purposes. | ||
* The clock. | ||
* | ||
* @var Clock | ||
*/ | ||
protected $clock; | ||
|
||
/** | ||
* Sets the current time. | ||
* | ||
* @param int $timeNow the time in seconds since epoch | ||
* @return void | ||
|
@@ -66,7 +76,7 @@ public static function setTimeNow($timeNow) | |
} | ||
|
||
/** | ||
* Reset the time now if it was set for test purposes. | ||
* Reset the current time so the true current time via PHP is used. | ||
* | ||
* @return void | ||
*/ | ||
|
@@ -76,11 +86,25 @@ public static function resetTimeNow() | |
} | ||
|
||
/** | ||
* @return int | ||
* @inheritdoc | ||
*/ | ||
public function setClock(Clock $clock) | ||
{ | ||
$this->clock = $clock; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getTimeNow() | ||
{ | ||
return self::$timeNow ? self::$timeNow : time(); | ||
if (self::$timeNow) { | ||
return self::$timeNow; | ||
} elseif (isset($this->clock)) { | ||
return $this->clock->now()->getTimestamp(); | ||
} else { | ||
return time(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you implement a if (self::$timeNow) {
return self::$timeNow;
}
return $this->getClock()->now()->getTimestamp(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better for |
||
} | ||
} | ||
|
||
/** | ||
|
@@ -106,6 +130,10 @@ public function __construct(array $options = []) | |
$this->refreshToken = $options['refresh_token']; | ||
} | ||
|
||
if (!empty($options['clock'])) { | ||
$this->clock = $options['clock']; | ||
} | ||
|
||
// 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. | ||
|
@@ -196,7 +224,7 @@ public function hasExpired() | |
throw new RuntimeException('"expires" is not set on the token'); | ||
} | ||
|
||
return $expires < time(); | ||
return $expires < $this->getTimeNow(); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
namespace League\OAuth2\Client\Token; | ||
|
||
use JsonSerializable; | ||
use League\OAuth2\Client\Provider\Clock; | ||
use RuntimeException; | ||
|
||
interface AccessTokenInterface extends JsonSerializable | ||
|
@@ -69,4 +70,20 @@ public function __toString(); | |
* @return array | ||
*/ | ||
public function jsonSerialize(); | ||
|
||
/** | ||
* Sets the clock. | ||
* | ||
* @param Clock $clock a clock. | ||
* | ||
* @return void | ||
*/ | ||
public function setClock(Clock $clock); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typically, we want interfaces to define things we can fetch, not necessarily how they are set. The way something is set could be left open to each implementation, but we'd want to ensure that all implementations have a way to get the clock (i.e., There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I dont understand what you're saying here. If this is about clock interfaces, im all for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm saying I would prefer to define a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes to this interface don't make sense. An access token does not have a "clock", it has an expiry date/time. I think the only reasonable modification here would be |
||
|
||
/** | ||
* Get the current time, whether real or simulated. | ||
* | ||
* @return int | ||
*/ | ||
public function getTimeNow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both of these methods added to the interface represent a BC break. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, of course There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you choose to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont see how both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you add a |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace League\OAuth2\Client\Test\Provider; | ||
|
||
use League\OAuth2\Client\Provider\Clock; | ||
|
||
/** | ||
* A clock with a frozen time for testing. | ||
*/ | ||
class FrozenClock extends Clock | ||
{ | ||
|
||
/** | ||
* The simulated time. | ||
* | ||
* Evaluates to 1st January 2015 @ 12pm. | ||
* | ||
* @var int | ||
*/ | ||
const NOW = 1420113600; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function now() | ||
{ | ||
return (new \DateTimeImmutable('@' . static::NOW)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace League\OAuth2\Client\Test\Provider; | ||
|
||
use League\OAuth2\Client\Provider\Clock; | ||
|
||
/** | ||
* A clock which must be initialised, and may be changed at any time. | ||
*/ | ||
class ProgrammableClock extends Clock | ||
{ | ||
|
||
/** | ||
* @var \DateTimeImmutable|null | ||
*/ | ||
protected $time = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why wouldn't this be initialized via |
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function now() | ||
{ | ||
if (!isset($this->time)) { | ||
throw new \LogicException('Time must be set explicitly'); | ||
} | ||
return $this->time; | ||
} | ||
|
||
/** | ||
* Sets the current time. | ||
* | ||
* @param \DateTimeImmutable|null the current time. | ||
* @return self | ||
*/ | ||
public function setTime($time) | ||
{ | ||
$this->time = $time; | ||
|
||
return $this; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason not to have something like
ClockInterface
here to require thenow()
method?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im all for it but interfaces seem to be used sparingly on this project... I wish providers had an interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an interface is appropriate here, because the implementation of
ClockInterface
should befinal class ...
.