Skip to content

Commit

Permalink
Merge pull request #4 from zeng444/master
Browse files Browse the repository at this point in the history
修复异常抛出格式
  • Loading branch information
inhere authored Sep 3, 2021
2 parents f30602a + b7acf88 commit 57ebb64
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "swoft/auth",
"name": "swoft/swoft-auth",
"type": "library",
"keywords": [
"php",
Expand Down
18 changes: 9 additions & 9 deletions src/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -228,35 +228,35 @@ 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) {
return false;
}

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) {
try {
$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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/AuthUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Middleware/AclMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/AuthorizationHeaderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/Parser/JWTTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
*/
Expand Down

0 comments on commit 57ebb64

Please sign in to comment.