Skip to content

Commit

Permalink
php 8 and laravel 9 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jszobody committed Feb 10, 2022
1 parent 642d5fa commit 99f3f12
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 41 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"description": "Simple helper package that makes it easy to generate and consume JWT tokens",
"type": "library",
"require": {
"php": "^8.0",
"lcobucci/jwt": "3.2.*|3.3.*",
"illuminate/support": "^5.4|^6.0|^7.0|^8.0"
"illuminate/support": "^5.4|^6.0|^7.0|^8.0|^9.0"
},
"require-dev": {
"orchestra/testbench": "^5.0",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "~9.0"
},
"license": "MIT",
Expand Down
53 changes: 15 additions & 38 deletions src/ParsedToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,12 @@
*/
class ParsedToken
{
/** @var Token */
protected $token;
protected bool $isValid = false;

/** @var bool */
protected $isValid = false;
public function __construct(protected Token $token)
{}

/**
* @param Token $token
*/
public function __construct(Token $token)
{
$this->token = $token;
}

/**
* @param string $jwt
*
* @return ParsedToken
*/
public static function fromString($jwt)
public static function fromString($jwt): ParsedToken
{
return new static(
(new Parser())->parse($jwt)
Expand All @@ -53,7 +39,7 @@ public static function fromString($jwt)
* @throws JwtExpiredException
* @throws JwtValidationException
*/
public function validate($validationInput, $signingKey = null)
public function validate($validationInput, $signingKey = null): ParsedToken
{
$this->validateRequiredClaims();
$this->validateExpiration();
Expand All @@ -70,10 +56,9 @@ public function validate($validationInput, $signingKey = null)
*
* @param $validationInput
* @param null $signingKey
*
* @return bool
*/
public function isValid($validationInput, $signingKey = null)
public function isValid($validationInput, $signingKey = null): bool
{
try {
$this->validate($validationInput, $signingKey);
Expand All @@ -87,7 +72,7 @@ public function isValid($validationInput, $signingKey = null)
/**
* @throws JwtValidationException
*/
protected function validateRequiredClaims()
protected function validateRequiredClaims(): void
{
if (!$this->token->hasClaim('exp')) {
throw new JwtValidationException("Token expiration is missing", $this->token);
Expand All @@ -105,7 +90,7 @@ protected function validateRequiredClaims()
/**
* @throws JwtExpiredException
*/
protected function validateExpiration()
protected function validateExpiration(): void
{
// Yes this will be validated in the `validateData` loop, however I like having a dedicated error message
// for this quite-common scenario
Expand All @@ -119,7 +104,7 @@ protected function validateExpiration()
*
* @throws JwtValidationException
*/
protected function validateData(ValidationData $validationData)
protected function validateData(ValidationData $validationData): void
{
foreach ($this->getValidatableClaims() as $claim) {
if (!$claim->validate($validationData)) {
Expand All @@ -133,7 +118,7 @@ protected function validateData(ValidationData $validationData)
*
* @throws JwtValidationException
*/
protected function verifySignature($signingKey = null)
protected function verifySignature($signingKey = null): void
{
if (!$this->token->verify(new Sha256(), $signingKey ?? JWTFacade::getSigningKey())) {
throw new JwtValidationException("JWT signature is invalid", $this->token);
Expand All @@ -143,7 +128,7 @@ protected function verifySignature($signingKey = null)
/**
* @return \Generator
*/
protected function getValidatableClaims()
protected function getValidatableClaims(): \Generator
{
foreach ($this->token->getClaims() as $claim) {
if ($claim instanceof Validatable) {
Expand All @@ -157,7 +142,7 @@ protected function getValidatableClaims()
*
* @return ValidationData
*/
protected function buildValidationData($validationInput)
protected function buildValidationData($validationInput): ValidationData
{
if (is_string($validationInput)) {
$validationData = new ValidationData();
Expand All @@ -175,20 +160,12 @@ protected function buildValidationData($validationInput)
return $validationData;
}

/**
* @return array
*/
public function toArray()
public function toArray(): array
{
return array_map(function($claim) {
return (string) $claim;
}, $this->token->getClaims());
return array_map(fn($claim) => (string) $claim, $this->token->getClaims());
}

/**
* @return array
*/
public function getPayload()
public function getPayload(): array
{
return array_diff_key($this->toArray(), array_flip(['jti','iss','aud','sub','iat','nbf','exp']));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testIdFromRouteName()
// Set the jwt id to match our route name
$route->parameters = ['jwt' => JWT::get('my.route')];
$route->action = ['as' => 'my.route'];
$request->setRouteResolver(function() use($route) { return $route; });
$request->setRouteResolver(fn() => $route);

$this->assertEquals("success", $middleware->handle($request, function() { return "success"; }));

Expand Down

0 comments on commit 99f3f12

Please sign in to comment.