mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 10:20:16 +08:00
first commit
This commit is contained in:
33
app/Lib/Jwt/BlackListConstraint.php
Normal file
33
app/Lib/Jwt/BlackListConstraint.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Lib\Jwt;
|
||||
|
||||
use Hyperf\Cache\Driver\DriverInterface;
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Validation\Constraint;
|
||||
use Lcobucci\JWT\Validation\ConstraintViolation;
|
||||
use Psr\SimpleCache\InvalidArgumentException;
|
||||
|
||||
class BlackListConstraint implements Constraint
|
||||
{
|
||||
/**
|
||||
* @param bool $enable
|
||||
* @param DriverInterface $cache
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly bool $enable,
|
||||
private readonly DriverInterface $cache
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param Token $token
|
||||
* @return void
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function assert(Token $token): void
|
||||
{
|
||||
if ($this->enable !== true) return;
|
||||
|
||||
if ($this->cache->has($token->toString())) throw ConstraintViolation::error('Token is in blacklist', $this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user