mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 18:30:16 +08:00
33 lines
817 B
PHP
33 lines
817 B
PHP
<?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);
|
|
}
|
|
} |