Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 5 additions & 8 deletions src/Method/ApiAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions src/Method/WebAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
}

/**
Expand Down
10 changes: 3 additions & 7 deletions src/UserAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,19 +17,15 @@
*
* @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';

public function __construct(private CurrentUser $currentUser, private ResponseFactoryInterface $responseFactory) {}

public function authenticate(ServerRequestInterface $request): ?IdentityInterface
{
if ($this->currentUser->isGuest()) {
return null;
}

return $this->currentUser->getIdentity();
return (new UserAuthenticator($this->currentUser))->authenticate($request);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/UserAuthenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yiisoft\User;

use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Auth\AuthenticatorInterface;
use Yiisoft\Auth\IdentityInterface;

/**
* Implementation of the AuthenticatorInterface for the user.
*/
final class UserAuthenticator implements AuthenticatorInterface
{
public function __construct(private CurrentUser $currentUser) {}

public function authenticate(ServerRequestInterface $request): ?IdentityInterface
{
if ($this->currentUser->isGuest()) {
return null;
}

return $this->currentUser->getIdentity();
}
}
42 changes: 42 additions & 0 deletions tests/UserAuthenticatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Yiisoft\User\Tests;

use HttpSoft\Message\ServerRequest;
use PHPUnit\Framework\TestCase;
use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher;
use Yiisoft\User\Tests\Support\MockArraySessionStorage;
use Yiisoft\User\Tests\Support\MockIdentity;
use Yiisoft\User\Tests\Support\MockIdentityRepository;
use Yiisoft\User\CurrentUser;
use Yiisoft\User\UserAuthenticator;

final class UserAuthenticatorTest extends TestCase
{
public function testSuccessfulAuthentication(): void
{
$user = $this->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());
}
}
Loading