Skip to content

Draft: Adds strong typing, more typing, php 8.2 features #280

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
7 changes: 3 additions & 4 deletions src/Claims/Custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
95 changes: 24 additions & 71 deletions src/Claims/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -57,8 +51,6 @@ class Factory

/**
* Constructor.
*
* @return void
*/
public function __construct(Request $request)
{
Expand All @@ -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);
Expand All @@ -89,101 +77,76 @@ 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);
}

/**
* 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;

return $this;
}

/**
* 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;

Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Claims/NotBefore.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Claims/NotBefore.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Method PHPOpenSourceSaver\\JWTAuth\\Claims\\NotBefore\:\:validatePayload\(\) should return bool but return statement is missing\.$# in path /home/runner/work/jwt-auth/jwt-auth/src/Claims/NotBefore.php was not matched in reported errors.

/*
* This file is part of jwt-auth.
Expand All @@ -18,12 +18,12 @@
{
use DatetimeTrait;

protected $name = 'nbf';
protected string $name = 'nbf';

Check failure on line 21 in src/Claims/NotBefore.php

View workflow job for this annotation

GitHub Actions / phpstan

Property PHPOpenSourceSaver\JWTAuth\Claims\NotBefore::$name (string) overriding property PHPOpenSourceSaver\JWTAuth\Claims\Claim::$name should not have a native type.

/**
* @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');
Expand Down
24 changes: 7 additions & 17 deletions src/Contracts/Claim.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 2 additions & 4 deletions src/Contracts/Http/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 4 additions & 3 deletions src/Providers/AbstractServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
}

Expand Down
34 changes: 18 additions & 16 deletions src/Support/CustomClaims.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,42 @@ 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;

return $this;
}

/**
* 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);
}
}
Loading
Loading