Initial commit
This commit is contained in:
9
src/Validator/JwtClaimException.php
Normal file
9
src/Validator/JwtClaimException.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt\Validator;
|
||||
|
||||
|
||||
class JwtClaimException extends JwtValidatorException
|
||||
{
|
||||
|
||||
}
|
8
src/Validator/JwtHeaderException.php
Normal file
8
src/Validator/JwtHeaderException.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt\Validator;
|
||||
|
||||
class JwtHeaderException extends JwtValidatorException
|
||||
{
|
||||
|
||||
}
|
8
src/Validator/JwtTokenException.php
Normal file
8
src/Validator/JwtTokenException.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt\Validator;
|
||||
|
||||
class JwtTokenException extends JwtValidatorException
|
||||
{
|
||||
|
||||
}
|
57
src/Validator/JwtValidator.php
Normal file
57
src/Validator/JwtValidator.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt\Validator;
|
||||
|
||||
use NoccyLabs\SimpleJwt\JwtToken;
|
||||
use NoccyLabs\SimpleJwt\Key\KeyInterface;
|
||||
|
||||
class JwtValidator
|
||||
{
|
||||
private $requireHeaders = [];
|
||||
|
||||
private $requireClaims = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->requireHeaders = [
|
||||
'alg',
|
||||
'typ',
|
||||
];
|
||||
}
|
||||
|
||||
public function addRequiredClaim(string $name)
|
||||
{
|
||||
$this->requireClaims[$name] = true;
|
||||
}
|
||||
|
||||
public function addRequiredClaimWithValue(string $name, $value)
|
||||
{
|
||||
$this->requireClaims[$name] = [ $value ];
|
||||
}
|
||||
|
||||
public function validateToken(JwtToken $token)
|
||||
{
|
||||
if (!$token->isValid()) {
|
||||
throw new JwtTokenException("The token is not valid");
|
||||
}
|
||||
|
||||
if (!$token->header->hasAll($this->requireHeaders)) {
|
||||
throw new JwtHeaderException("The token is missing one or more required headers");
|
||||
}
|
||||
|
||||
if (!$token->claims->hasAll($this->requireClaims)) {
|
||||
throw new JwtHeaderException("The token is missing one or more required claims");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate(KeyInterface $key, string $raw)
|
||||
{
|
||||
$token = new JwtToken($key, $raw);
|
||||
if ($this->validateToken($token)) {
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
8
src/Validator/JwtValidatorException.php
Normal file
8
src/Validator/JwtValidatorException.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt\Validator;
|
||||
|
||||
class JwtValidatorException extends \RuntimeException
|
||||
{
|
||||
|
||||
}
|
Reference in New Issue
Block a user