-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathCookieStorage.php
More file actions
94 lines (75 loc) · 2.09 KB
/
Copy pathCookieStorage.php
File metadata and controls
94 lines (75 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php declare(strict_types=1);
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Bridges\SecurityHttp;
use Nette;
use Nette\Http;
use Nette\Security\IIdentity;
use function is_string, strlen;
/**
* Cookie storage for Nette\Security\User object.
*/
final class CookieStorage implements Nette\Security\UserStorage
{
private const MinLength = 13;
private ?string $uid = null;
private string $cookieName = 'userid';
private ?string $cookieDomain = null;
private string $cookieSameSite = 'Lax';
private ?string $cookieExpiration = null;
public function __construct(
private readonly Http\IRequest $request,
private readonly Http\IResponse $response,
) {
}
public function saveAuthentication(IIdentity $identity): void
{
$uid = (string) $identity->getId();
if (strlen($uid) < self::MinLength) {
throw new \LogicException('UID is too short.');
}
$this->uid = $uid;
$this->response->setCookie(
$this->cookieName,
$uid,
$this->cookieExpiration,
domain: $this->cookieDomain,
sameSite: $this->cookieSameSite,
);
}
public function clearAuthentication(bool $clearIdentity): void
{
$this->uid = '';
$this->response->deleteCookie(
$this->cookieName,
domain: $this->cookieDomain,
);
}
public function getState(): array
{
if ($this->uid === null) {
$uid = $this->request->getCookie($this->cookieName);
$this->uid = is_string($uid) && strlen($uid) >= self::MinLength ? $uid : '';
}
return $this->uid
? [true, new Nette\Security\SimpleIdentity($this->uid), null]
: [false, null, null];
}
public function setExpiration(?string $expire, bool $clearIdentity): void
{
$this->cookieExpiration = $expire;
}
/** @param 'Lax'|'Strict'|'None'|null $sameSite */
public function setCookieParameters(
?string $name = null,
?string $domain = null,
?string $sameSite = null,
): void
{
$this->cookieName = $name ?? $this->cookieName;
$this->cookieDomain = $domain ?? $this->cookieDomain;
$this->cookieSameSite = $sameSite ?? $this->cookieSameSite;
}
}