diff --git a/CHANGELOG.md b/CHANGELOG.md index d9efcb6..cd5affb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 2.0.1 under development -- no changes in this release. +- Chg #71: Add token logging when login was failed (@xepozz) ## 2.0.0 February 15, 2023 diff --git a/src/CurrentUser.php b/src/CurrentUser.php index 4efc4a1..71d8f78 100644 --- a/src/CurrentUser.php +++ b/src/CurrentUser.php @@ -340,6 +340,7 @@ private function getExpire(): ?int { /** * @var mixed $expire + * * @psalm-suppress PossiblyNullReference */ $expire = $this->authTimeout !== null @@ -354,6 +355,7 @@ private function getExpireAbsolute(): ?int { /** * @var mixed $expire + * * @psalm-suppress PossiblyNullReference */ $expire = $this->absoluteAuthTimeout !== null diff --git a/src/Login/LoginMiddleware.php b/src/Login/LoginMiddleware.php index 650fa5c..191980c 100644 --- a/src/Login/LoginMiddleware.php +++ b/src/Login/LoginMiddleware.php @@ -15,7 +15,7 @@ /** * `LoginMiddleware` automatically logs user in if {@see IdentityInterface} instance presents in a request - * attribute. It is usually put there by {@see \Yiisoft\Auth\Middleware\Authentication}. + * attribute. It is usually put there by {@see Authentication}. */ final class LoginMiddleware implements MiddlewareInterface { @@ -35,7 +35,7 @@ public function __construct(CurrentUser $currentUser, LoggerInterface $logger) /** * {@inheritDoc} * - * Before this middleware, there should be {@see \Yiisoft\Auth\Middleware\Authentication} in the middleware stack. + * Before this middleware, there should be {@see Authentication} in the middleware stack. * It authenticates the user and places {@see IdentityInterface} instance in the corresponding request attribute. */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface @@ -50,7 +50,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if ($identity instanceof IdentityInterface) { $this->currentUser->login($identity); } else { - $this->logger->warning('Unable to authenticate user by token. Identity not found.'); + $this->logger->warning(sprintf( + 'Unable to authenticate user by token %s. Identity not found.', + is_scalar($identity) ? ('"' . $identity . '"') : ('of type ' . get_debug_type($identity)), + )); } return $handler->handle($request); diff --git a/tests/Login/LoginMiddlewareTest.php b/tests/Login/LoginMiddlewareTest.php index 12b590d..78180b7 100644 --- a/tests/Login/LoginMiddlewareTest.php +++ b/tests/Login/LoginMiddlewareTest.php @@ -10,12 +10,12 @@ use Psr\Http\Server\RequestHandlerInterface; use Yiisoft\Auth\Middleware\Authentication; use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher; +use Yiisoft\User\CurrentUser; use Yiisoft\User\Event\AfterLogin; use Yiisoft\User\Event\BeforeLogin; use Yiisoft\User\Guest\GuestIdentity; use Yiisoft\User\Login\LoginMiddleware; use Yiisoft\User\Tests\Support\LastMessageLogger; -use Yiisoft\User\CurrentUser; use Yiisoft\User\Tests\Support\MockIdentity; use Yiisoft\User\Tests\Support\MockIdentityRepository; @@ -85,7 +85,7 @@ public function testIdentityNotFound(): void $this->assertNull($this->currentUser ->getIdentity() ->getId()); - $this->assertSame('Unable to authenticate user by token. Identity not found.', $this->logger->getLastMessage()); + $this->assertSame('Unable to authenticate user by token of type null. Identity not found.', $this->logger->getLastMessage()); } private function createServerRequest(bool $withIdentity = true): ServerRequestInterface