From 9017830bfecf2ce6c4d6668e9a1b30a8d9913207 Mon Sep 17 00:00:00 2001 From: Max Snow Date: Tue, 11 Feb 2025 08:24:49 +1100 Subject: [PATCH 1/6] Start with Factory, ttl and leeway --- src/Claims/Factory.php | 40 +++++++---------------- src/Providers/AbstractServiceProvider.php | 7 ++-- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/Claims/Factory.php b/src/Claims/Factory.php index 8476de5b..8af27a91 100644 --- a/src/Claims/Factory.php +++ b/src/Claims/Factory.php @@ -20,25 +20,19 @@ class Factory { /** - * The request. - * - * @var Request + * The Laravel request. */ - protected $request; + protected Request $request; /** - * The TTL. - * - * @var int|null + * The time to live in minutes. */ - protected $ttl = 60; + protected int $ttl = 60; /** * Time leeway in seconds. - * - * @var int */ - protected $leeway = 0; + protected int $leeway = 0; /** * The classes map. @@ -179,11 +173,9 @@ public function extend($name, $classPath) } /** - * Set the request instance. - * - * @return $this + * Set the Laravel request instance. */ - public function setRequest(Request $request) + public function setRequest(Request $request): self { $this->request = $request; @@ -192,36 +184,26 @@ public function setRequest(Request $request) /** * Set the token ttl (in minutes). - * - * @param int|null $ttl - * - * @return $this */ - public function setTTL($ttl) + public function setTTL(int $ttl): self { - $this->ttl = $ttl ? (int) $ttl : $ttl; + $this->ttl = $ttl; return $this; } /** * Get the token ttl. - * - * @return int|null */ - public function getTTL() + public function getTTL(): int { return $this->ttl; } /** * Set the leeway in seconds. - * - * @param int $leeway - * - * @return $this */ - public function setLeeway($leeway) + public function setLeeway(int $leeway): self { $this->leeway = $leeway; diff --git a/src/Providers/AbstractServiceProvider.php b/src/Providers/AbstractServiceProvider.php index c1d730fa..2fab4887 100644 --- a/src/Providers/AbstractServiceProvider.php +++ b/src/Providers/AbstractServiceProvider.php @@ -300,14 +300,15 @@ protected function registerPayloadValidator() * * @return void */ - protected function registerClaimFactory() + protected function registerClaimFactory(): void { $this->app->singleton('tymon.jwt.claim.factory', function ($app) { $factory = new ClaimFactory($app['request']); $app->refresh('request', $factory, 'setRequest'); + $config = $app->make('config'); - return $factory->setTTL($app->make('config')->get('jwt.ttl')) - ->setLeeway($app->make('config')->get('jwt.leeway')); + return $factory->setTTL((int) $config->get('jwt.ttl')) + ->setLeeway((int) $config->get('jwt.leeway')); }); } From 8278e9b0a93b6a9416b216174185c37b09ba2825 Mon Sep 17 00:00:00 2001 From: Max Snow Date: Tue, 11 Feb 2025 10:48:21 +1100 Subject: [PATCH 2/6] Updates types and docblocks in util --- src/Support/Utils.php | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/src/Support/Utils.php b/src/Support/Utils.php index 79daf282..aa855539 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -17,36 +17,25 @@ class Utils { /** - * Get the Carbon instance for the current time. - * - * @return Carbon + * Get the Carbon instance for the current time, in the UTC timezone */ - public static function now() + public static function now(): Carbon { return Carbon::now('UTC'); } /** - * Get the Carbon instance for the timestamp. - * - * @param int $timestamp - * - * @return Carbon + * Get the Carbon instance for a unix timestamp, in UTC */ - public static function timestamp($timestamp) + public static function timestamp(int $timestamp): Carbon { return Carbon::createFromTimestampUTC($timestamp)->timezone('UTC'); } /** - * Checks if a timestamp is in the past. - * - * @param int $timestamp - * @param int $leeway - * - * @return bool + * Checks if a unix timestamp is in the past. */ - public static function isPast($timestamp, $leeway = 0) + public static function isPast(int $timestamp, int $leeway = 0): bool { $timestamp = static::timestamp($timestamp); @@ -56,14 +45,9 @@ public static function isPast($timestamp, $leeway = 0) } /** - * Checks if a timestamp is in the future. - * - * @param int $timestamp - * @param int $leeway - * - * @return bool + * Checks if a unix timestamp is in the future. */ - public static function isFuture($timestamp, $leeway = 0) + public static function isFuture(int $timestamp, int $leeway = 0): bool { $timestamp = static::timestamp($timestamp); From 91bb3875d02324144530c4b9eeb874342e3eb49c Mon Sep 17 00:00:00 2001 From: Max Snow Date: Tue, 11 Feb 2025 10:53:24 +1100 Subject: [PATCH 3/6] Updates Factory for types & docblocks --- src/Claims/Factory.php | 55 ++++++++++-------------------------------- 1 file changed, 13 insertions(+), 42 deletions(-) diff --git a/src/Claims/Factory.php b/src/Claims/Factory.php index 8af27a91..417935c4 100644 --- a/src/Claims/Factory.php +++ b/src/Claims/Factory.php @@ -39,7 +39,7 @@ class Factory * * @var array */ - private $classMap = [ + private array $classMap = [ 'aud' => Audience::class, 'exp' => Expiration::class, 'iat' => IssuedAt::class, @@ -51,8 +51,6 @@ class Factory /** * Constructor. - * - * @return void */ public function __construct(Request $request) { @@ -62,13 +60,9 @@ public function __construct(Request $request) /** * Get the instance of the claim when passing the name and value. * - * @param string $name - * - * @return Claim - * * @throws InvalidClaimException */ - public function get($name, $value) + public function get(string $name, mixed $value): Custom { if ($this->has($name)) { $claim = new $this->classMap[$name]($value); @@ -83,12 +77,8 @@ public function get($name, $value) /** * Check whether the claim exists. - * - * @param string $name - * - * @return bool */ - public function has($name) + public function has(string $name): bool { return array_key_exists($name, $this->classMap); } @@ -96,76 +86,57 @@ public function has($name) /** * Generate the initial value and return the Claim instance. * - * @param string $name - * - * @return Claim - * * @throws InvalidClaimException */ - public function make($name) + public function make(string $name): Claim { return $this->get($name, $this->$name()); } /** * Get the Issuer (iss) claim. - * - * @return string */ - public function iss() + public function iss(): string { - return $this->request->url(); + return $this->request->url() ?? ''; } /** * Get the Issued At (iat) claim. - * - * @return int */ - public function iat() + public function iat(): int { return Utils::now()->getTimestamp(); } /** - * Get the Expiration (exp) claim. - * - * @return int + * Get the Expiration (exp) claim as a unix timestamp */ - public function exp() + public function exp(): int { return Utils::now()->addMinutes($this->ttl)->getTimestamp(); } /** - * Get the Not Before (nbf) claim. - * - * @return int + * Get the Not Before (nbf) claim as a unix timestamp */ - public function nbf() + public function nbf(): int { return Utils::now()->getTimestamp(); } /** * Get the JWT Id (jti) claim. - * - * @return string */ - public function jti() + public function jti(): string { return Str::random(); } /** * Add a new claim mapping. - * - * @param string $name - * @param string $classPath - * - * @return $this */ - public function extend($name, $classPath) + public function extend(string $name, string $classPath): self { $this->classMap[$name] = $classPath; From 8610006cf490ca65d3b84c460476a925e3b22512 Mon Sep 17 00:00:00 2001 From: Max Snow Date: Tue, 11 Feb 2025 11:07:49 +1100 Subject: [PATCH 4/6] Update custom claims and refresh flow --- src/Support/CustomClaims.php | 34 ++++++++++++++++++---------------- src/Support/RefreshFlow.php | 10 ++-------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/Support/CustomClaims.php b/src/Support/CustomClaims.php index 5d3e6e88..34e70956 100644 --- a/src/Support/CustomClaims.php +++ b/src/Support/CustomClaims.php @@ -16,17 +16,13 @@ trait CustomClaims { /** * Custom claims. - * - * @var array */ - protected $customClaims = []; + protected array $customClaims = []; /** * Set the custom claims. - * - * @return $this */ - public function customClaims(array $customClaims) + public function setCustomClaims(array $customClaims): self { $this->customClaims = $customClaims; @@ -34,22 +30,28 @@ public function customClaims(array $customClaims) } /** - * Alias to set the custom claims. - * - * @return $this + * Get the custom claims. */ - public function claims(array $customClaims) + public function getCustomClaims(): array { - return $this->customClaims($customClaims); + return $this->customClaims; } /** - * Get the custom claims. - * - * @return array + * Alias of setCustomClaims. + * @deprecated Please use setCustomClaims(array) */ - public function getCustomClaims() + public function customClaims(array $customClaims): self { - return $this->customClaims; + return $this->setCustomClaims($customClaims); + } + + /** + * Alias of setCustomClaims. + * @deprecated Please use setCustomClaims(array) + */ + public function claims(array $customClaims): self + { + return $this->setCustomClaims($customClaims); } } diff --git a/src/Support/RefreshFlow.php b/src/Support/RefreshFlow.php index c8db9e76..a07059b0 100644 --- a/src/Support/RefreshFlow.php +++ b/src/Support/RefreshFlow.php @@ -16,19 +16,13 @@ trait RefreshFlow { /** * The refresh flow flag. - * - * @var bool */ - protected $refreshFlow = false; + protected bool $refreshFlow = false; /** * Set the refresh flow flag. - * - * @param bool $refreshFlow - * - * @return $this */ - public function setRefreshFlow($refreshFlow = true) + public function setRefreshFlow(bool $refreshFlow = true): static { $this->refreshFlow = $refreshFlow; From 92c777926838ba04bba63865e233e924a0329ade Mon Sep 17 00:00:00 2001 From: Max Snow Date: Wed, 12 Feb 2025 10:59:27 +1100 Subject: [PATCH 5/6] Done these files --- src/Contracts/Http/Parser.php | 6 ++---- src/Token.php | 18 +++++------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Contracts/Http/Parser.php b/src/Contracts/Http/Parser.php index 01588e7d..4aea3d50 100644 --- a/src/Contracts/Http/Parser.php +++ b/src/Contracts/Http/Parser.php @@ -17,9 +17,7 @@ interface Parser { /** - * Parse the request. - * - * @return string|null + * Parse the request, and return the desired value from it. */ - public function parse(Request $request); + public function parse(Request $request): string|null; } diff --git a/src/Token.php b/src/Token.php index 8084e2b6..f0e0466d 100644 --- a/src/Token.php +++ b/src/Token.php @@ -21,33 +21,25 @@ class Token /** * Create a new JSON Web Token. * - * @param string $value - * - * @return void - * * @throws Exceptions\TokenInvalidException */ - public function __construct($value) + public function __construct(string $value) { - $this->value = (string) (new TokenValidator())->check($value); + $this->value = (new TokenValidator())->check($value); } /** - * Get the token. - * - * @return string + * Get the token as string. */ - public function get() + public function get(): string { return $this->value; } /** * Get the token when casting to string. - * - * @return string */ - public function __toString() + public function __toString(): string { return $this->get(); } From 65206ea20e4aef9af39178c01595d945b60c5052 Mon Sep 17 00:00:00 2001 From: Max Snow Date: Wed, 12 Feb 2025 11:00:05 +1100 Subject: [PATCH 6/6] Done these files --- src/Claims/Custom.php | 7 +++---- src/Claims/NotBefore.php | 4 ++-- src/Contracts/Claim.php | 24 +++++++----------------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/Claims/Custom.php b/src/Claims/Custom.php index 242765b0..0ad2a8ba 100644 --- a/src/Claims/Custom.php +++ b/src/Claims/Custom.php @@ -17,15 +17,14 @@ class Custom extends Claim { /** - * @param string $name - * - * @return void + * Creates a custom claim * * @throws InvalidClaimException */ - public function __construct($name, $value) + public function __construct(string $name, mixed $value) { parent::__construct($value); + $this->setName($name); } } diff --git a/src/Claims/NotBefore.php b/src/Claims/NotBefore.php index ee0878a2..4d981305 100644 --- a/src/Claims/NotBefore.php +++ b/src/Claims/NotBefore.php @@ -18,12 +18,12 @@ class NotBefore extends Claim { use DatetimeTrait; - protected $name = 'nbf'; + protected string $name = 'nbf'; /** * @throws TokenInvalidException */ - public function validatePayload() + public function validatePayload(): void { if ($this->isFuture($this->getValue())) { throw new TokenInvalidException('Not Before (nbf) timestamp cannot be in the future'); diff --git a/src/Contracts/Claim.php b/src/Contracts/Claim.php index d2aba815..8d68d9c9 100644 --- a/src/Contracts/Claim.php +++ b/src/Contracts/Claim.php @@ -18,38 +18,28 @@ interface Claim { /** * Set the claim value, and call a validate method. - * - * @return $this - * + * @throws InvalidClaimException */ - public function setValue($value); + public function setValue(mixed $value): self; /** * Get the claim value. */ - public function getValue(); + public function getValue(): mixed; /** * Set the claim name. - * - * @param string $name - * - * @return $this */ - public function setName($name); + public function setName(string $name): self; /** * Get the claim name. - * - * @return string */ - public function getName(); + public function getName(): string; /** - * Validate the Claim value. - * - * @return bool + * Validate the Claim value, and return it */ - public function validateCreate($value); + public function validateCreate(mixed $value): mixed; }