Add strict return types to TokenService
This commit is contained in:
parent
a7ebc9a24f
commit
8ac32f5783
1 changed files with 8 additions and 3 deletions
|
@ -1,8 +1,11 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
use Lcobucci\JWT\Token;
|
||||||
use Lcobucci\JWT\Parser;
|
use Lcobucci\JWT\Parser;
|
||||||
use Lcobucci\JWT\Builder;
|
use Lcobucci\JWT\Builder;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
@ -27,7 +30,7 @@ class TokenService
|
||||||
->sign($signer, config('app.key'))
|
->sign($signer, config('app.key'))
|
||||||
->getToken();
|
->getToken();
|
||||||
|
|
||||||
return $token;
|
return (string) $token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,17 +39,19 @@ class TokenService
|
||||||
* @param string The token
|
* @param string The token
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function validateToken($token)
|
public function validateToken(string $token): ?Token
|
||||||
{
|
{
|
||||||
$signer = new Sha256();
|
$signer = new Sha256();
|
||||||
try {
|
try {
|
||||||
$token = (new Parser())->parse((string) $token);
|
$token = (new Parser())->parse((string) $token);
|
||||||
} catch (InvalidArgumentException | RuntimeException $e) {
|
} catch (InvalidArgumentException | RuntimeException $e) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
if ($token->verify($signer, config('app.key'))) {
|
if ($token->verify($signer, config('app.key'))) {
|
||||||
//signuture valid
|
//signuture valid
|
||||||
return $token;
|
return $token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue