From 70dc4dee8ba70fac3ae23ba849121970caa8ecce Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 6 Aug 2021 19:09:39 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8A=9B=E5=87=BA=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AuthManager.php | 18 +++++++++--------- src/AuthUserService.php | 4 ++-- src/Middleware/AclMiddleware.php | 6 +++--- src/Parser/AuthorizationHeaderParser.php | 4 ++-- src/Parser/JWTTokenParser.php | 5 +++-- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/AuthManager.php b/src/AuthManager.php index fd808b6..aef33eb 100644 --- a/src/AuthManager.php +++ b/src/AuthManager.php @@ -106,12 +106,12 @@ public function isLoggedIn(): bool public function login(string $accountTypeName, array $data): AuthSession { if (!$account = $this->getAccountType($accountTypeName)) { - throw new AuthException(ErrorCode::AUTH_INVALID_ACCOUNT_TYPE); + throw new AuthException('AUTH_INVALID_ACCOUNT_TYPE', ErrorCode::AUTH_INVALID_ACCOUNT_TYPE); } $result = $account->login($data); if (!$result instanceof AuthResult || $result->getIdentity() === '') { - throw new AuthException(ErrorCode::AUTH_LOGIN_FAILED); + throw new AuthException('AUTH_LOGIN_FAILED', ErrorCode::AUTH_LOGIN_FAILED); } $session = $this->generateSession($accountTypeName, $result->getIdentity(), $result->getExtendedData()); @@ -123,7 +123,7 @@ public function login(string $accountTypeName, array $data): AuthSession $session->getToken(), $this->getSessionDuration()); } catch (\InvalidArgumentException $e) { $err = sprintf('%s Invalid Argument : %s', $session->getIdentity(), $e->getMessage()); - throw new AuthException(ErrorCode::POST_DATA_NOT_PROVIDED, $err); + throw new AuthException('POST_DATA_NOT_PROVIDED', ErrorCode::POST_DATA_NOT_PROVIDED, $err); } } @@ -228,7 +228,7 @@ public function authenticateToken(string $token): bool /** @var AuthSession $session */ $session = $this->getTokenParser()->getSession($token); } catch (Throwable $e) { - throw new AuthException(ErrorCode::AUTH_TOKEN_INVALID); + throw new AuthException('AUTH_TOKEN_INVALID', ErrorCode::AUTH_TOKEN_INVALID); } if (!$session) { @@ -236,15 +236,15 @@ public function authenticateToken(string $token): bool } if ($session->getExpirationTime() < time()) { - throw new AuthException(ErrorCode::AUTH_SESSION_EXPIRED); + throw new AuthException('AUTH_SESSION_EXPIRED', ErrorCode::AUTH_SESSION_EXPIRED); } if (!$account = $this->getAccountType($session->getAccountTypeName())) { - throw new AuthException(ErrorCode::AUTH_SESSION_INVALID); + throw new AuthException('AUTH_SESSION_INVALID', ErrorCode::AUTH_SESSION_INVALID); } if (!$account->authenticate($session->getIdentity())) { - throw new AuthException(ErrorCode::AUTH_TOKEN_INVALID); + throw new AuthException('AUTH_TOKEN_INVALID', ErrorCode::AUTH_TOKEN_INVALID); } if ($this->cacheEnable === true) { @@ -252,11 +252,11 @@ public function authenticateToken(string $token): bool $cache = $this->getCacheClient()->get($this->getCacheKey($session->getIdentity(), $session->getExtendedData())); if (!$cache || $cache !== $token) { - throw new AuthException(ErrorCode::AUTH_TOKEN_INVALID); + throw new AuthException('AUTH_TOKEN_INVALID', ErrorCode::AUTH_TOKEN_INVALID); } } catch (InvalidArgumentException $e) { $err = sprintf('Identity : %s ,err : %s', $session->getIdentity(), $e->getMessage()); - throw new AuthException(ErrorCode::POST_DATA_NOT_PROVIDED, $err); + throw new AuthException('POST_DATA_NOT_PROVIDED', ErrorCode::POST_DATA_NOT_PROVIDED, $err); } } diff --git a/src/AuthUserService.php b/src/AuthUserService.php index 40f2f6b..2343f16 100644 --- a/src/AuthUserService.php +++ b/src/AuthUserService.php @@ -65,8 +65,8 @@ public function getSession(): ?AuthSession */ public function auth(string $requestHandler, ServerRequestInterface $request): bool { - throw new AuthException(ErrorCode::POST_DATA_NOT_PROVIDED, - sprintf('AuthUserService::auth() method should be implemented in %s', static::class)); + throw new AuthException( + sprintf('AuthUserService::auth() method should be implemented in %s', static::class), ErrorCode::POST_DATA_NOT_PROVIDED); } /** diff --git a/src/Middleware/AclMiddleware.php b/src/Middleware/AclMiddleware.php index 697835f..e885564 100644 --- a/src/Middleware/AclMiddleware.php +++ b/src/Middleware/AclMiddleware.php @@ -40,12 +40,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $service = Swoft::getBean(AuthServiceInterface::class); if (!$service instanceof AuthServiceInterface) { - throw new AuthException(ErrorCode::POST_DATA_NOT_PROVIDED, - 'AuthService should implement Swoft\Auth\Contract\AuthServiceInterface'); + throw new AuthException( + 'AuthService should implement Swoft\Auth\Contract\AuthServiceInterface', ErrorCode::POST_DATA_NOT_PROVIDED); } if (!$service->auth($requestHandler, $request)) { - throw new AuthException(ErrorCode::ACCESS_DENIED); + throw new AuthException('ACCESS_DENIED', ErrorCode::ACCESS_DENIED); } return $handler->handle($request); diff --git a/src/Parser/AuthorizationHeaderParser.php b/src/Parser/AuthorizationHeaderParser.php index e5f7248..c2f86e5 100644 --- a/src/Parser/AuthorizationHeaderParser.php +++ b/src/Parser/AuthorizationHeaderParser.php @@ -59,9 +59,9 @@ public function parse(ServerRequestInterface $request): ServerRequestInterface $handler = Swoft::getBean($this->mergeTypes()[$type]); if (!$handler instanceof AuthHandlerInterface) { - throw new AuthException(ErrorCode::POST_DATA_NOT_PROVIDED, + throw new AuthException( sprintf('%s should implement Swoft\Auth\Contract\AuthHandlerInterface', - $this->mergeTypes()[$type])); + $this->mergeTypes()[$type]), ErrorCode::POST_DATA_NOT_PROVIDED); } $request = $handler->handle($request); diff --git a/src/Parser/JWTTokenParser.php b/src/Parser/JWTTokenParser.php index 337aedb..c31f46f 100644 --- a/src/Parser/JWTTokenParser.php +++ b/src/Parser/JWTTokenParser.php @@ -14,6 +14,7 @@ use Swoft\Auth\AuthSession; use Swoft\Auth\Contract\TokenParserInterface; use Swoft\Bean\Annotation\Mapping\Bean; +use Swoft\Config\Annotation\Mapping\Config; /** * @Bean() @@ -29,14 +30,14 @@ class JWTTokenParser implements TokenParserInterface public const ALGORITHM_RS256 = 'RS256'; /** - * Value("auth.jwt.algorithm") + * @Config("auth.jwt.algorithm") * * @var string */ protected $algorithm = self::ALGORITHM_HS256; /** - * Value("auth.jwt.secret") + * @Config("auth.jwt.secret") * * @var string */ From e5823ac41ee21c3a97de4b5f3901bcfee972ea7e Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 6 Aug 2021 19:15:15 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ce82540..235bdd2 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "swoft/auth", + "name": "janfish/swoft-auth", "type": "library", "keywords": [ "php", From b7acf888083299eb6cf7522eec2dc14d9e7526f8 Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 3 Sep 2021 15:12:27 +0800 Subject: [PATCH 3/3] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 235bdd2..cc4759e 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "janfish/swoft-auth", + "name": "swoft/swoft-auth", "type": "library", "keywords": [ "php",