From 63b19fdb3468d1a3392dcfb934cb209b8996c260 Mon Sep 17 00:00:00 2001 From: klsoft-web Date: Fri, 21 Aug 2026 18:01:32 +0300 Subject: [PATCH] Fix deprecated classes usage --- CHANGELOG.md | 1 + composer.json | 2 +- src/Method/ApiAuth.php | 13 ++++------ src/Method/WebAuth.php | 13 ++++------ src/UserAuth.php | 10 +++----- src/UserAuthenticator.php | 26 ++++++++++++++++++++ tests/UserAuthenticatorTest.php | 42 +++++++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 src/UserAuthenticator.php create mode 100644 tests/UserAuthenticatorTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b016b76..bdc2a1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 2.3.3 under development - Enh #120: Explicitly import classes and constants in "use" section (@vjik) +- Enh #127: Fix deprecated classes usage (@klsoft-web) ## 2.3.2 December 23, 2025 diff --git a/composer.json b/composer.json index 2345ad2..830a9dc 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "psr/http-server-middleware": "^1.0", "psr/log": "^1.1 || ^2.0 || ^3.0", "yiisoft/access": "^2.0", - "yiisoft/auth": "^2.0 || ^3.0", + "yiisoft/auth": "^3.3.0", "yiisoft/cookies": "^1.2", "yiisoft/session": "^1.0 || ^2.0 || ^3.0", "yiisoft/http": "^1.2" diff --git a/src/Method/ApiAuth.php b/src/Method/ApiAuth.php index e43fe8a..f09aafd 100644 --- a/src/Method/ApiAuth.php +++ b/src/Method/ApiAuth.php @@ -6,24 +6,21 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Yiisoft\Auth\AuthenticationMethodInterface; +use Yiisoft\Auth\AuthenticatorWithChallengeInterface; use Yiisoft\Auth\IdentityInterface; use Yiisoft\User\CurrentUser; +use Yiisoft\User\UserAuthenticator; /** - * Implementation of the `AuthenticationMethodInterface` for authenticating users in the API clients. + * Implementation of the `AuthenticatorWithChallengeInterface` for authenticating users in the API clients. */ -final class ApiAuth implements AuthenticationMethodInterface +final class ApiAuth implements AuthenticatorWithChallengeInterface { public function __construct(private readonly CurrentUser $currentUser) {} public function authenticate(ServerRequestInterface $request): ?IdentityInterface { - if ($this->currentUser->isGuest()) { - return null; - } - - return $this->currentUser->getIdentity(); + return (new UserAuthenticator($this->currentUser))->authenticate($request); } public function challenge(ResponseInterface $response): ResponseInterface diff --git a/src/Method/WebAuth.php b/src/Method/WebAuth.php index 90097b6..bd626ff 100644 --- a/src/Method/WebAuth.php +++ b/src/Method/WebAuth.php @@ -7,15 +7,16 @@ use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Yiisoft\Auth\AuthenticationMethodInterface; +use Yiisoft\Auth\AuthenticatorWithChallengeInterface; use Yiisoft\Auth\IdentityInterface; use Yiisoft\Http\Status; use Yiisoft\User\CurrentUser; +use Yiisoft\User\UserAuthenticator; /** - * Implementation of the `AuthenticationMethodInterface` for authenticating users in the web applications. + * Implementation of the `AuthenticatorWithChallengeInterface` for authenticating users in the web applications. */ -final class WebAuth implements AuthenticationMethodInterface +final class WebAuth implements AuthenticatorWithChallengeInterface { private string $authUrl = '/login'; @@ -26,11 +27,7 @@ public function __construct( public function authenticate(ServerRequestInterface $request): ?IdentityInterface { - if ($this->currentUser->isGuest()) { - return null; - } - - return $this->currentUser->getIdentity(); + return (new UserAuthenticator($this->currentUser))->authenticate($request); } /** diff --git a/src/UserAuth.php b/src/UserAuth.php index a473a4b..1d802d4 100644 --- a/src/UserAuth.php +++ b/src/UserAuth.php @@ -7,7 +7,7 @@ use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Yiisoft\Auth\AuthenticationMethodInterface; +use Yiisoft\Auth\AuthenticatorWithChallengeInterface; use Yiisoft\Auth\IdentityInterface; use Yiisoft\Http\Status; use Yiisoft\User\Method\WebAuth; @@ -17,7 +17,7 @@ * * @deprecated Use {@see WebAuth}. This class will be removed in the next major version. */ -final class UserAuth implements AuthenticationMethodInterface +final class UserAuth implements AuthenticatorWithChallengeInterface { private string $authUrl = '/login'; @@ -25,11 +25,7 @@ public function __construct(private CurrentUser $currentUser, private ResponseFa public function authenticate(ServerRequestInterface $request): ?IdentityInterface { - if ($this->currentUser->isGuest()) { - return null; - } - - return $this->currentUser->getIdentity(); + return (new UserAuthenticator($this->currentUser))->authenticate($request); } /** diff --git a/src/UserAuthenticator.php b/src/UserAuthenticator.php new file mode 100644 index 0000000..ca1b65d --- /dev/null +++ b/src/UserAuthenticator.php @@ -0,0 +1,26 @@ +currentUser->isGuest()) { + return null; + } + + return $this->currentUser->getIdentity(); + } +} diff --git a/tests/UserAuthenticatorTest.php b/tests/UserAuthenticatorTest.php new file mode 100644 index 0000000..4d023c3 --- /dev/null +++ b/tests/UserAuthenticatorTest.php @@ -0,0 +1,42 @@ +createCurrentUser(); + $user->login(new MockIdentity('test-id')); + $result = (new UserAuthenticator($user))->authenticate(new ServerRequest()); + + $this->assertNotNull($result); + $this->assertSame('test-id', $result->getId()); + } + + public function testIdentityNotAuthenticated(): void + { + $user = $this->createCurrentUser(); + $result = (new UserAuthenticator($user))->authenticate(new ServerRequest()); + + $this->assertNull($result); + } + + + private function createCurrentUser(): CurrentUser + { + return (new CurrentUser(new MockIdentityRepository(), new SimpleEventDispatcher())) + ->withSession(new MockArraySessionStorage()); + } +}