Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['8.1', '8.2', '8.3']
['8.1', '8.2', '8.3', '8.4']
2 changes: 1 addition & 1 deletion .github/workflows/composer-require-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2', '8.3']
['8.1', '8.2', '8.3', '8.4']
2 changes: 1 addition & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.3']
['8.4']
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2', '8.3']
['8.1', '8.2', '8.3', '8.4']
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

- Chg #101: Rename `UserAuth` to `WebAuth`, mark `UserAuth` as deprecated (@olegbaturin)
- New #101: Add `ApiAuth` authentication method (@olegbaturin)
- Chg #109: Change PHP constraint in `composer.json` to `~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0` (@vjik)
- Bug #109: Explicitly mark nullable parameters (@vjik)
- Enh #109: Improve debug message "Unable to authenticate user…" in `LogginMiddleware` (@vjik)

## 2.2.0 May 07, 2024

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ The package handles user-related functionality:
## Requirements

- PHP 8.1 or higher.
- `JSON` PHP extension.

## Installation

Expand Down
30 changes: 15 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,29 @@
}
],
"require": {
"php": "^8.1",
"ext-json": "*",
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
"psr/event-dispatcher": "^1.0",
"psr/http-message": "^1.0|^2.0",
"psr/http-message": "^1.0 || ^2.0",
"psr/http-factory": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"psr/log": "^1.1|^2.0|^3.0",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"yiisoft/access": "^2.0",
"yiisoft/auth": "^2.0|^3.0",
"yiisoft/auth": "^2.0 || ^3.0",
"yiisoft/cookies": "^1.2",
"yiisoft/session": "^1.0|^2.0",
"yiisoft/session": "^1.0 || ^2.0",
"yiisoft/http": "^1.2"
},
"require-dev": {
"httpsoft/http-message": "^1.0",
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^10.5",
"rector/rector": "^2.0.0",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.23",
"yiisoft/di": "^1.0",
"yiisoft/test-support": "^3.0"
"httpsoft/http-message": "^1.1.6",
"maglnet/composer-require-checker": "^4.7.1",
"phpunit/phpunit": "^10.5.45",
"rector/rector": "^2.0.9",
"roave/infection-static-analysis-plugin": "^1.35",
"spatie/phpunit-watcher": "^1.24",
"vimeo/psalm": "^5.26.1 || ^6.8.6",
"yiisoft/di": "^1.3",
"yiisoft/test-support": "^3.0.2"
},
"autoload": {
"psr-4": {
Expand All @@ -73,6 +72,7 @@
},
"config": {
"sort-packages": true,
"bump-after-update": "dev",
"allow-plugins": {
"infection/extension-installer": true,
"composer/package-versions-deprecated": true
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
ensureOverrideAttribute="false"
strictBinaryOperands="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php81\Rector\ClassMethod\NewInInitializerRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Set\ValueObject\LevelSetList;
Expand All @@ -27,5 +28,6 @@
ClosureToArrowFunctionRector::class,
ReadOnlyPropertyRector::class,
NullToStrictStringFuncCallArgRector::class,
NewInInitializerRector::class,
]);
};
2 changes: 1 addition & 1 deletion src/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class CurrentUser
public function __construct(
private IdentityRepositoryInterface $identityRepository,
private EventDispatcherInterface $eventDispatcher,
GuestIdentityFactoryInterface $guestIdentityFactory = null
?GuestIdentityFactoryInterface $guestIdentityFactory = null
) {
$this->guestIdentityFactory = $guestIdentityFactory ?? new GuestIdentityFactory();
}
Expand Down
18 changes: 14 additions & 4 deletions src/Login/LoginMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
use Yiisoft\Auth\Middleware\Authentication;
use Yiisoft\User\CurrentUser;

use function is_bool;
use function is_scalar;
use function sprintf;

/**
* `LoginMiddleware` automatically logs user in if {@see IdentityInterface} instance presents in a request
* attribute. It is usually put there by {@see Authentication}.
Expand Down Expand Up @@ -47,10 +51,16 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
if ($identity instanceof IdentityInterface) {
$this->currentUser->login($identity);
} else {
$this->logger->debug(sprintf(
'Unable to authenticate user by token %s. Identity not found.',
is_scalar($identity) ? ('"' . $identity . '"') : ('of type ' . get_debug_type($identity)),
));
if (is_scalar($identity)) {
$token = is_bool($identity)
? ($identity ? 'true' : 'false')
: ('"' . $identity . '"');
} else {
$token = 'of type ' . get_debug_type($identity);
}
$this->logger->debug(
sprintf('Unable to authenticate user by token %s. Identity not found.', $token)
);
}

return $handler->handle($request);
Expand Down
2 changes: 1 addition & 1 deletion tests/Login/Cookie/CookieLoginMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ private function getIdentityRepository(IdentityInterface $identity): IdentityRep

private function getRequestWithAutoLoginCookie(
string $authKey = CookieLoginIdentity::KEY_CORRECT,
int $expires = null
?int $expires = null
): ServerRequestInterface {
return $this->getRequestWithCookies([
'autoLogin' => json_encode([CookieLoginIdentity::ID, $authKey, $expires ?? time() + 3600], JSON_THROW_ON_ERROR),
Expand Down
35 changes: 31 additions & 4 deletions tests/Login/LoginMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Yiisoft\User\Tests\Login;

use HttpSoft\Message\ServerRequest;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use stdClass;
use Yiisoft\Auth\Middleware\Authentication;
use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher;
use Yiisoft\User\CurrentUser;
Expand Down Expand Up @@ -82,10 +84,35 @@ public function testIdentityNotFound(): void
$middleware->process($this->createServerRequest(false), $this->createRequestHandler());

$this->assertInstanceOf(GuestIdentity::class, $this->currentUser->getIdentity());
$this->assertNull($this->currentUser
->getIdentity()
->getId());
$this->assertSame('Unable to authenticate user by token of type null. Identity not found.', $this->logger->getLastMessage());
$this->assertNull($this->currentUser->getIdentity()->getId());
}

public static function dataUnableToAuthenticateUserDebugMessage(): iterable
{
yield ['true', true];
yield ['false', false];
yield ['"123"', 123];
yield ['"10.4"', 10.4];
yield ['"hello"', 'hello'];
yield ['of type null', null];
yield ['of type stdClass', new stdClass()];
yield ['of type array', ['a', 'b']];
}

#[DataProvider('dataUnableToAuthenticateUserDebugMessage')]
public function testUnableToAuthenticateUserDebugMessage(string $expectedType, mixed $identity): void
{
$middleware = new LoginMiddleware($this->currentUser, $this->logger);

$middleware->process(
(new ServerRequest())->withAttribute(Authentication::class, $identity),
$this->createRequestHandler()
);

$this->assertSame(
'Unable to authenticate user by token ' . $expectedType . '. Identity not found.',
$this->logger->getLastMessage()
);
}

private function createServerRequest(bool $withIdentity = true): ServerRequestInterface
Expand Down