Skip to content

Commit

Permalink
Fix #86: Add optional parameter $duration to `CookieLogin::addCooki…
Browse files Browse the repository at this point in the history
…e()`
  • Loading branch information
vjik authored Nov 22, 2023
1 parent cb4704e commit 907129f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Chg #58: Raise the minimum version of PHP to 8.0 and did refactoring using the features of it (@xepozz, @rustamwin)
- Chg #58: Raise version of `yiisoft/access` to `^2.0` (@rustamwin)
- Enh #83: Allow to create a session cookie via `CookieLogin` (@rustamwin)
- New #86: Add optional parameter `$duration` to `CookieLogin::addCookie()` (@vjik)

## 2.0.0 February 15, 2023

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The package handles user-related functionality:
The package could be installed with composer:

```shell
composer require yiisoft/user --prefer-dist
composer require yiisoft/user
```

## General usage
Expand Down
16 changes: 12 additions & 4 deletions src/Login/Cookie/CookieLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,25 @@ public function withCookieName(string $name): self
*
* @param CookieLoginIdentityInterface $identity The cookie login identity instance.
* @param ResponseInterface $response Response for adding auto-login cookie.
* @param DateInterval|false|null $duration Interval until the auto-login cookie expires. If it is null it means
* the auto-login cookie is session cookie that expires when browser is closed. If it is false (by default) will be
* used default value of duration.
*
* @throws JsonException If an error occurs during JSON encoding of the cookie value.
*
* @return ResponseInterface Response with added auto-login cookie.
*/
public function addCookie(CookieLoginIdentityInterface $identity, ResponseInterface $response): ResponseInterface
{
public function addCookie(
CookieLoginIdentityInterface $identity,
ResponseInterface $response,
DateInterval|null|false $duration = false,
): ResponseInterface {
$duration = $duration === false ? $this->duration : $duration;

$data = [$identity->getId(), $identity->getCookieLoginKey()];

if ($this->duration !== null) {
$expires = (new DateTimeImmutable())->add($this->duration);
if ($duration !== null) {
$expires = (new DateTimeImmutable())->add($duration);
$data[] = $expires->getTimestamp();
} else {
$expires = null;
Expand Down
36 changes: 36 additions & 0 deletions tests/Login/Cookie/CookieLoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,40 @@ public function testRemoveCookieWithCustomName(): void
$response->getHeaderLine('Set-Cookie'),
);
}

public function dataAddCookieWithCustomDuration(): array
{
return [
'false' => [
'#testName=%5B%2242%22%2C%22auto-login-key-correct%22%2C[0-9]{10}%5D; Expires=.*?; Max-Age=604800; Path=/; Secure; HttpOnly; SameSite=Lax#',
false,
],
'null' => [
'#testName=%5B%2242%22%2C%22auto-login-key-correct%22%2C0%5D; Path=/; Secure; HttpOnly; SameSite=Lax#',
null,
],
'p1d' => [
'#testName=%5B%2242%22%2C%22auto-login-key-correct%22%2C[0-9]{10}%5D; Expires=.*?; Max-Age=86400; Path=/; Secure; HttpOnly; SameSite=Lax#',
new DateInterval('P1D'),
],
];
}

/**
* @dataProvider dataAddCookieWithCustomDuration
*/
public function testAddCookieWithCustomDuration(string $expectedRegExp, DateInterval|null|false $duration): void
{
$cookieLogin = (new CookieLogin(new DateInterval('P1W')))->withCookieName('testName');

$identity = new CookieLoginIdentity();

$response = new Response();
$response = $cookieLogin->addCookie($identity, $response, $duration);

$this->assertMatchesRegularExpression(
$expectedRegExp,
$response->getHeaderLine('Set-Cookie'),
);
}
}

0 comments on commit 907129f

Please sign in to comment.