-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The list is generated automatically, and except the framework cookies, you can't create a cookie without adding an item into the `CookieName` enum. Which means that a test can be written, and was written, that makes sure that all cookies are described. Have to admit this is mostly a flex 😁
- Loading branch information
Showing
25 changed files
with
479 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\Cookies; | ||
|
||
use Nette\Utils\Html; | ||
|
||
class CookieDescription | ||
{ | ||
|
||
public function __construct( | ||
private readonly string $name, | ||
private readonly bool $internal, | ||
private readonly Html $description, | ||
private readonly ?int $validDays, | ||
) { | ||
} | ||
|
||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
|
||
public function isInternal(): bool | ||
{ | ||
return $this->internal; | ||
} | ||
|
||
|
||
public function getDescription(): Html | ||
{ | ||
return $this->description; | ||
} | ||
|
||
|
||
public function getValidDays(): ?int | ||
{ | ||
return $this->validDays; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\Cookies; | ||
|
||
use MichalSpacekCz\Application\Theme; | ||
use MichalSpacekCz\DateTime\DateTime; | ||
use MichalSpacekCz\Formatter\TexyFormatter; | ||
use MichalSpacekCz\ShouldNotHappenException; | ||
use MichalSpacekCz\User\Manager; | ||
use Nette\Http\Helpers; | ||
use Nette\Http\Session; | ||
|
||
class CookieDescriptions | ||
{ | ||
|
||
public function __construct( | ||
private readonly Manager $authenticator, | ||
private readonly Theme $theme, | ||
private readonly Session $sessionHandler, | ||
private readonly TexyFormatter $texyFormatter, | ||
private readonly DateTime $dateTime, | ||
) { | ||
} | ||
|
||
|
||
/** | ||
* @return list<CookieDescription> | ||
*/ | ||
public function get(): array | ||
{ | ||
$options = $this->sessionHandler->getOptions(); | ||
$cookieLifetime = $options['cookie_lifetime']; | ||
if (!is_int($cookieLifetime)) { | ||
throw new ShouldNotHappenException("The cookie_lifetime option should be an int, but it's a " . get_debug_type($cookieLifetime)); | ||
} | ||
/** @noinspection PhpInternalEntityUsedInspection */ | ||
return [ | ||
new CookieDescription( | ||
CookieName::PermanentLogin->value, | ||
true, | ||
$this->texyFormatter->translate('messages.cookies.cookie.permanentLogin'), | ||
$this->dateTime->getDaysFromString($this->authenticator->getPermanentLoginCookieLifetime()), | ||
), | ||
new CookieDescription( | ||
CookieName::ReturningUser->value, | ||
true, | ||
$this->texyFormatter->translate('messages.cookies.cookie.returningUser'), | ||
$this->dateTime->getDaysFromString($this->authenticator->getReturningUserCookieLifetime()), | ||
), | ||
new CookieDescription( | ||
CookieName::Theme->value, | ||
false, | ||
$this->texyFormatter->translate('messages.cookies.cookie.theme'), | ||
$this->dateTime->getDaysFromString($this->theme->getCookieLifetime()), | ||
), | ||
new CookieDescription( | ||
$this->sessionHandler->getName(), | ||
false, | ||
$this->texyFormatter->translate('messages.cookies.cookie.netteSession'), | ||
$this->dateTime->getDaysFromString($cookieLifetime . ' seconds'), | ||
), | ||
new CookieDescription( | ||
Helpers::StrictCookieName, | ||
false, | ||
$this->texyFormatter->translate('messages.cookies.cookie.netteSameSiteCheck'), | ||
null, | ||
), | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\Cookies; | ||
|
||
enum CookieName: string | ||
{ | ||
|
||
case PermanentLogin = '__Secure-permanent'; | ||
case ReturningUser = '__Secure-beenhere'; | ||
case Theme = 'future'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\Cookies; | ||
|
||
use DateTimeInterface; | ||
use Nette\Http\IRequest; | ||
use Nette\Http\IResponse; | ||
use Nette\Http\Response; | ||
|
||
class Cookies | ||
{ | ||
|
||
public function __construct( | ||
private readonly IRequest $request, | ||
private readonly IResponse $response, | ||
) { | ||
} | ||
|
||
|
||
public function getString(CookieName $name): ?string | ||
{ | ||
$cookie = $this->request->getCookie($name->value); | ||
if (!is_string($cookie)) { | ||
return null; | ||
} | ||
return $cookie; | ||
} | ||
|
||
|
||
public function set( | ||
CookieName $name, | ||
string $value, | ||
DateTimeInterface|int|string $expire, | ||
?string $path = null, | ||
?string $domain = null, | ||
?bool $secure = null, | ||
?bool $httpOnly = null, | ||
?string $sameSite = null, | ||
): void { | ||
/** @var Response $response Not IResponse because https://github.com/nette/http/issues/200, can't use instanceof check because it's a different Response in tests */ | ||
$response = $this->response; | ||
$response->setCookie($name->value, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite); | ||
} | ||
|
||
|
||
public function delete(CookieName $name, ?string $path = null, ?string $domain = null, ?bool $secure = null): void | ||
{ | ||
$this->response->deleteCookie($name->value, $path, $domain, $secure); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.