Skip to content

Commit

Permalink
Merge pull request #29 from wisp-x/master
Browse files Browse the repository at this point in the history
feat: Adapt to hyperf 3.0.
  • Loading branch information
qbhy authored Jun 28, 2022
2 parents 93bee9f + 3e85e3f commit 435de3f
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 135 deletions.
25 changes: 13 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
}
},
"require": {
"php": ">=7.2",
"php": ">=8.0",
"ext-swoole": ">=4.4",
"96qbhy/simple-jwt": "^v1.4.0",
"hyperf/cache": "^2.0",
"hyperf/di": "^2.0",
"hyperf/cache": "^3.0",
"hyperf/di": "^3.0",
"ext-json": "*",
"ext-redis": "*"
},
Expand All @@ -37,21 +37,22 @@
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.8",
"friendsofphp/php-cs-fixer": "^2.14",
"hyperf/command": "^2.2",
"hyperf/config": "^2.0",
"hyperf/database": "^2.0",
"hyperf/event": "^2.2",
"hyperf/framework": "^2.0",
"hyperf/redis": "^2.0",
"hyperf/session": "^2.0",
"hyperf/testing": "^2.0",
"hyperf/utils": "^2.2",
"hyperf/command": "^3.0",
"hyperf/config": "^3.0",
"hyperf/database": "^3.0",
"hyperf/event": "^3.0",
"hyperf/framework": "^3.0",
"hyperf/redis": "^3.0",
"hyperf/session": "^3.0",
"hyperf/testing": "^3.0",
"hyperf/utils": "^3.0",
"itsgoingd/clockwork": "^5.0",
"phpstan/phpstan": "^0.12",
"swoft/swoole-ide-helper": "dev-master",
"symfony/console": "^5.3",
"symfony/var-dumper": "^5.3"
},
"minimum-stability": "dev",
"config": {
"sort-packages": true
},
Expand Down
14 changes: 7 additions & 7 deletions publish/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

/*
* 以下是 simple-jwt 配置
* 必填
* jwt 服务端身份标识
*/
* 必填
* jwt 服务端身份标识
*/
'secret' => env('SSO_JWT_SECRET'),

/*
Expand Down Expand Up @@ -104,9 +104,9 @@

/*
* 以下是 simple-jwt 配置
* 必填
* jwt 服务端身份标识
*/
* 必填
* jwt 服务端身份标识
*/
'secret' => env('SIMPLE_JWT_SECRET'),

/*
Expand Down Expand Up @@ -175,7 +175,7 @@
'providers' => [
'users' => [
'driver' => \Qbhy\HyperfAuth\Provider\EloquentProvider::class,
'model' => App\Model\User::class, // 需要实现 Qbhy\HyperfAuth\Authenticatable 接口
'model' => App\Model\User::class, // 需要实现 Qbhy\HyperfAuth\Authenticatable 接口
],
],
];
11 changes: 1 addition & 10 deletions src/Annotation/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,12 @@
namespace Qbhy\HyperfAuth\Annotation;

use Attribute;
use Doctrine\Common\Annotations\Annotation\Target;
use Hyperf\Di\Annotation\AbstractAnnotation;

/**
* @Annotation
* @Target({"METHOD", "CLASS"})
* Class Auth
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
class Auth extends AbstractAnnotation
{
/**
* @var string
*/
public $value;
public string $value;

public function __construct($value = null)
{
Expand Down
13 changes: 4 additions & 9 deletions src/AuthAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@
use Qbhy\HyperfAuth\Annotation\Auth;
use Qbhy\HyperfAuth\Exception\UnauthorizedException;

/**
* @Aspect
*/
#[Aspect]
class AuthAspect extends AbstractAspect
{
public $annotations = [
public array $annotations = [
Auth::class,
];

/**
* @Inject
* @var AuthManager
*/
protected $auth;
#[Inject(AuthManager::class)]
protected AuthManager $auth;

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
Expand Down
9 changes: 2 additions & 7 deletions src/AuthCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;

/**
* @Command
* Class AuthCommand
*/
#[Command]
class AuthCommand extends HyperfCommand
{
/**
* 执行的命令行.
*
* @var string
*/
protected $name = 'gen:auth-env';
protected ?string $name = 'gen:auth-env';

public function handle()
{
Expand Down
20 changes: 4 additions & 16 deletions src/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,13 @@
*/
class AuthManager
{
/**
* @var string
*/
protected $defaultDriver = 'default';
protected string $defaultDriver = 'default';

/**
* @var array
*/
protected $guards = [];
protected array $guards = [];

/**
* @var array
*/
protected $providers = [];
protected array $providers = [];

/**
* @var array
*/
protected $config;
protected array $config;

public function __construct(ConfigInterface $config)
{
Expand Down
9 changes: 3 additions & 6 deletions src/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
*/
class AuthMiddleware implements MiddlewareInterface
{
protected $guards = [null];
protected array $guards = [null];

/**
* @Inject
* @var AuthManager
*/
protected $auth;
#[Inject(AuthManager::class)]
protected AuthManager $auth;

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
Expand Down
6 changes: 2 additions & 4 deletions src/Events/ForcedOfflineEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ class ForcedOfflineEvent
{
/**
* 用户实例.
* @var Authenticatable
*/
public $user;
public Authenticatable $user;

/**
* 客户端标识.
* @var string
*/
public $client;
public string $client;

/**
* ForcedOfflineEvent constructor.
Expand Down
6 changes: 3 additions & 3 deletions src/Exception/UnauthorizedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

class UnauthorizedException extends AuthException
{
protected $guard;
protected ?AuthGuard $guard;

protected $statusCode = 401;
protected int $statusCode = 401;

public function __construct(string $message, AuthGuard $guard = null, Throwable $previous = null)
{
Expand All @@ -31,7 +31,7 @@ public function getStatusCode(): int
return $this->statusCode;
}

public function setStatusCode(int $statusCode)
public function setStatusCode(int $statusCode): static
{
$this->statusCode = $statusCode;
return $this;
Expand Down
15 changes: 3 additions & 12 deletions src/Guard/AbstractAuthGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,11 @@

abstract class AbstractAuthGuard implements AuthGuard
{
/**
* @var array
*/
protected $config;
protected array $config;

/**
* @var string
*/
protected $name;
protected string $name;

/**
* @var UserProvider
*/
protected $userProvider;
protected UserProvider $userProvider;

/**
* AbstractAuthGuard constructor.
Expand Down
14 changes: 4 additions & 10 deletions src/Guard/JwtGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
*/
namespace Qbhy\HyperfAuth\Guard;

use Hyperf\Context\Context;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\Context;
use Hyperf\Utils\Str;
use Qbhy\HyperfAuth\Authenticatable;
use Qbhy\HyperfAuth\Exception\AuthException;
Expand All @@ -23,17 +23,11 @@

class JwtGuard extends AbstractAuthGuard
{
/**
* @var JWTManager
*/
protected $jwtManager;
protected JWTManager $jwtManager;

/**
* @var RequestInterface
*/
protected $request;
protected RequestInterface $request;

protected $headerName = 'Authorization';
protected mixed $headerName = 'Authorization';

/**
* JwtGuardAbstract constructor.
Expand Down
7 changes: 2 additions & 5 deletions src/Guard/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@
*/
namespace Qbhy\HyperfAuth\Guard;

use Hyperf\Context\Context;
use Hyperf\Contract\SessionInterface;
use Hyperf\Utils\Context;
use Qbhy\HyperfAuth\Authenticatable;
use Qbhy\HyperfAuth\Exception\AuthException;
use Qbhy\HyperfAuth\Exception\UnauthorizedException;
use Qbhy\HyperfAuth\UserProvider;

class SessionGuard extends AbstractAuthGuard
{
/**
* @var SessionInterface
*/
protected $session;
protected SessionInterface $session;

/**
* JwtGuardAbstract constructor.
Expand Down
8 changes: 4 additions & 4 deletions src/Guard/SsoGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/
namespace Qbhy\HyperfAuth\Guard;

use Hyperf\Context\Context;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Hyperf\Utils\Context;
use Psr\EventDispatcher\EventDispatcherInterface;
use Qbhy\HyperfAuth\Authenticatable;
use Qbhy\HyperfAuth\Events\ForcedOfflineEvent;
Expand All @@ -24,12 +24,12 @@ class SsoGuard extends JwtGuard
/**
* @var Redis
*/
protected $redis;
protected mixed $redis;

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
protected mixed $eventDispatcher;

public function __construct(array $config, string $name, UserProvider $userProvider, RequestInterface $request)
{
Expand Down Expand Up @@ -67,7 +67,7 @@ public function refresh(?string $token = null, string $client = null): ?string
{
$token = parent::refresh($token);

if ($token){
if ($token) {
$client = $client ?: $this->getClients()[0]; // 需要至少配置一个客户端
$redisKey = str_replace('{uid}', (string) $this->id($token), $this->config['redis_key'] ?? 'u:token:{uid}');
$this->redis->hSet($redisKey, $client, $token);
Expand Down
Loading

0 comments on commit 435de3f

Please sign in to comment.