1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use HansOtt\PSR7Cookies\RequestCookies; |
6
|
|
|
use HansOtt\PSR7Cookies\SetCookie; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use React\Cache\CacheInterface; |
10
|
|
|
use React\Promise\PromiseInterface; |
11
|
|
|
use function React\Promise\resolve; |
12
|
|
|
use Throwable; |
13
|
|
|
use WyriHaximus\React\Http\Middleware\SessionId\RandomBytes; |
14
|
|
|
|
15
|
|
|
final class SessionMiddleware |
16
|
|
|
{ |
17
|
|
|
const ATTRIBUTE_NAME = 'wyrihaximus.react.http.middleware.session'; |
18
|
|
|
|
19
|
|
|
const DEFAULT_COOKIE_PARAMS = [ |
20
|
|
|
0, |
21
|
|
|
'', |
22
|
|
|
'', |
23
|
|
|
false, |
24
|
|
|
false, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $cookieName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var CacheInterface |
34
|
|
|
*/ |
35
|
|
|
private $cache; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $cookieParams; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var SessionIdInterface |
44
|
|
|
*/ |
45
|
|
|
private $sessionId; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $cookieName |
49
|
|
|
* @param CacheInterface $cache |
50
|
|
|
* @param array $cookieParams |
51
|
|
|
* @param SessionIdInterface|null $sessionId |
52
|
|
|
*/ |
53
|
19 |
|
public function __construct( |
54
|
|
|
string $cookieName, |
55
|
|
|
CacheInterface $cache, |
56
|
|
|
array $cookieParams = [], |
57
|
|
|
SessionIdInterface $sessionId = null |
58
|
|
|
) { |
59
|
19 |
|
$this->cookieName = $cookieName; |
60
|
19 |
|
$this->cache = $cache; |
61
|
19 |
|
$this->cookieParams = \array_replace(self::DEFAULT_COOKIE_PARAMS, $cookieParams); |
62
|
|
|
|
63
|
19 |
|
if ($sessionId === null) { |
64
|
19 |
|
$sessionId = new RandomBytes(); |
65
|
|
|
} |
66
|
19 |
|
$this->sessionId = $sessionId; |
67
|
19 |
|
} |
68
|
|
|
|
69
|
19 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
70
|
|
|
{ |
71
|
|
|
return $this->fetchSessionFromRequest($request)->then(function (Session $session) use ($next, $request) { |
72
|
18 |
|
$request = $request->withAttribute(self::ATTRIBUTE_NAME, $session); |
|
|
|
|
73
|
|
|
|
74
|
18 |
|
return resolve( |
75
|
18 |
|
$next($request) |
76
|
|
|
)->then(function (ResponseInterface $response) use ($session) { |
77
|
|
|
return $this->updateCache($session)->then(function () use ($response) { |
78
|
18 |
|
return $response; |
79
|
18 |
|
}); |
80
|
|
|
})->then(function ($response) use ($session) { |
81
|
18 |
|
$cookie = $this->getCookie($session); |
82
|
18 |
|
$response = $cookie->addToResponse($response); |
83
|
|
|
|
84
|
18 |
|
return $response; |
85
|
18 |
|
}); |
86
|
19 |
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
19 |
|
private function fetchSessionFromRequest(ServerRequestInterface $request): PromiseInterface |
90
|
|
|
{ |
91
|
19 |
|
$id = ''; |
92
|
19 |
|
$cookies = RequestCookies::createFromRequest($request); |
93
|
|
|
|
94
|
|
|
try { |
95
|
19 |
|
if (!$cookies->has($this->cookieName)) { |
96
|
11 |
|
return resolve(new Session($id, [], $this->sessionId)); |
97
|
|
|
} |
98
|
8 |
|
$id = $cookies->get($this->cookieName)->getValue(); |
99
|
|
|
|
100
|
|
|
return $this->fetchSessionDataFromCache($id)->then(function (array $sessionData) use ($id) { |
101
|
6 |
|
return new Session($id, $sessionData, $this->sessionId); |
102
|
7 |
|
}); |
103
|
1 |
|
} catch (Throwable $et) { |
104
|
|
|
// Do nothing, only a not found will be thrown so generating our own id now |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return resolve(new Session($id, [], $this->sessionId)); |
108
|
|
|
} |
109
|
|
|
|
110
|
8 |
|
private function fetchSessionDataFromCache(string $id): PromiseInterface |
111
|
|
|
{ |
112
|
8 |
|
if ($id === '') { |
113
|
|
|
return resolve([]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->cache->get($id)->then(function ($result) { |
117
|
6 |
|
if ($result === null) { |
118
|
5 |
|
return resolve([]); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $result; |
122
|
7 |
|
}); |
123
|
|
|
} |
124
|
|
|
|
125
|
18 |
|
private function updateCache(Session $session): PromiseInterface |
126
|
|
|
{ |
127
|
18 |
|
foreach ($session->getOldIds() as $oldId) { |
128
|
2 |
|
$this->cache->delete($oldId); |
129
|
|
|
} |
130
|
|
|
|
131
|
18 |
|
if ($session->isActive()) { |
132
|
16 |
|
return resolve($this->cache->set($session->getId(), $session->getContents())); |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
return resolve(); |
136
|
|
|
} |
137
|
|
|
|
138
|
18 |
|
private function getCookie(Session $session): SetCookie |
139
|
|
|
{ |
140
|
18 |
|
$cookieParams = $this->cookieParams; |
141
|
|
|
|
142
|
18 |
|
if ($session->isActive()) { |
143
|
|
|
// Only set time when expires is set in the future |
144
|
16 |
|
if ($cookieParams[0] > 0) { |
145
|
11 |
|
$cookieParams[0] += \time(); |
146
|
|
|
} |
147
|
|
|
|
148
|
16 |
|
return new SetCookie($this->cookieName, $session->getId(), ...$cookieParams); |
|
|
|
|
149
|
|
|
} |
150
|
2 |
|
unset($cookieParams[0]); |
151
|
|
|
|
152
|
2 |
|
return SetCookie::thatDeletesCookie($this->cookieName, ...$cookieParams); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope