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/Factory.php b/src/Claims/Factory.php index 8476de5b..417935c4 100644 --- a/src/Claims/Factory.php +++ b/src/Claims/Factory.php @@ -20,32 +20,26 @@ 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. * * @var array */ - private $classMap = [ + private array $classMap = [ 'aud' => Audience::class, 'exp' => Expiration::class, 'iat' => IssuedAt::class, @@ -57,8 +51,6 @@ class Factory /** * Constructor. - * - * @return void */ public function __construct(Request $request) { @@ -68,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); @@ -89,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); } @@ -102,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; @@ -179,11 +144,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 +155,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/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; } 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/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')); }); } 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; 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); 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(); }