Skip to content
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ final class CookieLoginIdentityRepository implements IdentityRepositoryInterface
The `CookieLoginMiddleware` will check for the existence of a cookie in the request,
validate it and login the user automatically.

> [!note]
> The auto-login cookie value isn't protected against tampering by this package. This isn't only about
> the end user editing their own cookie — anyone who steals the cookie value can modify it too, e.g. to
> remove its expiration. Use `CookieMiddleware` from [`yiisoft/cookies`](https://github.com/yiisoft/cookies)
> to sign or encrypt it if you want to prevent that.

#### Creating a cookie

By default, you should set cookie for auto login manually in your application after logging user in:
Expand Down
5 changes: 5 additions & 0 deletions src/Login/Cookie/CookieLoginMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RuntimeException;
use Throwable;
use Yiisoft\Auth\IdentityRepositoryInterface;
use Yiisoft\Cookies\CookieMiddleware;
use Yiisoft\User\CurrentUser;

use function array_key_exists;
Expand All @@ -26,6 +27,10 @@

/**
* `CookieLoginMiddleware` automatically logs user in based on cookie.
*
* The auto-login cookie value isn't protected against tampering by this package. This isn't only about
* the end user editing their own cookie — anyone who steals the cookie value can modify it too, e.g. to
* remove its expiration. Use {@see CookieMiddleware} to sign or encrypt it if you want to prevent that.
*/
final class CookieLoginMiddleware implements MiddlewareInterface
{
Expand All @@ -41,7 +46,7 @@
private IdentityRepositoryInterface $identityRepository,
private LoggerInterface $logger,
private CookieLogin $cookieLogin,
private bool $forceAddCookie = false,

Check warning on line 49 in src/Login/Cookie/CookieLoginMiddleware.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": @@ @@ private IdentityRepositoryInterface $identityRepository, private LoggerInterface $logger, private CookieLogin $cookieLogin, - private bool $forceAddCookie = false, + private bool $forceAddCookie = true, ) {} /**
) {}

/**
Expand Down Expand Up @@ -94,7 +99,7 @@
}

try {
$data = json_decode((string) $cookies[$cookieName], true, 512, JSON_THROW_ON_ERROR);

Check warning on line 102 in src/Login/Cookie/CookieLoginMiddleware.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": @@ @@ } try { - $data = json_decode((string) $cookies[$cookieName], true, 512, JSON_THROW_ON_ERROR); + $data = json_decode((string) $cookies[$cookieName], true, 513, JSON_THROW_ON_ERROR); } catch (Throwable) { $this->logger->warning('Unable to authenticate user by cookie. Invalid cookie.'); return;

Check warning on line 102 in src/Login/Cookie/CookieLoginMiddleware.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": @@ @@ } try { - $data = json_decode((string) $cookies[$cookieName], true, 512, JSON_THROW_ON_ERROR); + $data = json_decode((string) $cookies[$cookieName], true, 511, JSON_THROW_ON_ERROR); } catch (Throwable) { $this->logger->warning('Unable to authenticate user by cookie. Invalid cookie.'); return;
} catch (Throwable) {
$this->logger->warning('Unable to authenticate user by cookie. Invalid cookie.');
return;
Expand Down Expand Up @@ -129,12 +134,12 @@

if (!$identity->validateCookieLoginKey($key)) {
$this->logger->warning('Unable to authenticate user by cookie. Invalid key.');
return;

Check warning on line 137 in src/Login/Cookie/CookieLoginMiddleware.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ if (!$identity->validateCookieLoginKey($key)) { $this->logger->warning('Unable to authenticate user by cookie. Invalid key.'); - return; + } if ($expires !== 0 && $expires < time()) {
}

if ($expires !== 0 && $expires < time()) {

Check warning on line 140 in src/Login/Cookie/CookieLoginMiddleware.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "LessThan": @@ @@ return; } - if ($expires !== 0 && $expires < time()) { + if ($expires !== 0 && $expires <= time()) { $this->logger->warning('Unable to authenticate user by cookie. Lifetime has expired.'); return; }
$this->logger->warning('Unable to authenticate user by cookie. Lifetime has expired.');
return;

Check warning on line 142 in src/Login/Cookie/CookieLoginMiddleware.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ if ($expires !== 0 && $expires < time()) { $this->logger->warning('Unable to authenticate user by cookie. Lifetime has expired.'); - return; + } $this->currentUser->login($identity);
}

$this->currentUser->login($identity);
Expand Down
Loading