Skip to content

Commit

Permalink
Set leeway on Datetime Claims creation
Browse files Browse the repository at this point in the history
Copied from this PR:
tymondesigns#2182
  • Loading branch information
ArthurYdalgo committed Jun 1, 2023
1 parent 44982a7 commit cdd9dc5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/Claims/DatetimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ trait DatetimeTrait
*/
protected $leeway = 0;

/**
* @param mixed $value
* @param int $leeway
*
* @return void
*/
public function __construct($value, $leeway = 0)
{
$this->leeway = $leeway;

parent::__construct($value);
}

/**
* Set the claim value, and call a validate method.
*
* @param mixed $value
* @return $this
*
* @throws \Tymon\JWTAuth\Exceptions\InvalidClaimException
*
* @return $this
*/
public function setValue($value)
{
Expand Down Expand Up @@ -62,6 +76,7 @@ public function validateCreate($value)
* Determine whether the value is in the future.
*
* @param mixed $value
*
* @return bool
*/
protected function isFuture($value)
Expand All @@ -73,6 +88,7 @@ protected function isFuture($value)
* Determine whether the value is in the past.
*
* @param mixed $value
*
* @return bool
*/
protected function isPast($value)
Expand All @@ -84,6 +100,7 @@ protected function isPast($value)
* Set the leeway in seconds.
*
* @param int $leeway
*
* @return $this
*/
public function setLeeway($leeway)
Expand Down
14 changes: 9 additions & 5 deletions src/Claims/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Factory
* Constructor.
*
* @param \Illuminate\Http\Request $request
*
* @return void
*/
public function __construct(Request $request)
Expand All @@ -69,16 +70,13 @@ public function __construct(Request $request)
*
* @param string $name
* @param mixed $value
*
* @return \Tymon\JWTAuth\Claims\Claim
*/
public function get($name, $value)
{
if ($this->has($name)) {
$claim = new $this->classMap[$name]($value);

return method_exists($claim, 'setLeeway') ?
$claim->setLeeway($this->leeway) :
$claim;
return new $this->classMap[$name]($value, $this->leeway);
}

return new Custom($name, $value);
Expand All @@ -88,6 +86,7 @@ public function get($name, $value)
* Check whether the claim exists.
*
* @param string $name
*
* @return bool
*/
public function has($name)
Expand All @@ -99,6 +98,7 @@ public function has($name)
* Generate the initial value and return the Claim instance.
*
* @param string $name
*
* @return \Tymon\JWTAuth\Claims\Claim
*/
public function make($name)
Expand Down Expand Up @@ -161,6 +161,7 @@ public function jti()
*
* @param string $name
* @param string $classPath
*
* @return $this
*/
public function extend($name, $classPath)
Expand All @@ -174,6 +175,7 @@ public function extend($name, $classPath)
* Set the request instance.
*
* @param \Illuminate\Http\Request $request
*
* @return $this
*/
public function setRequest(Request $request)
Expand All @@ -187,6 +189,7 @@ public function setRequest(Request $request)
* Set the token ttl (in minutes).
*
* @param int $ttl
*
* @return $this
*/
public function setTTL($ttl)
Expand All @@ -210,6 +213,7 @@ public function getTTL()
* Set the leeway in seconds.
*
* @param int $leeway
*
* @return $this
*/
public function setLeeway($leeway)
Expand Down

0 comments on commit cdd9dc5

Please sign in to comment.