1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* sessionware (https://github.com/juliangut/sessionware). |
5
|
|
|
* PSR7 session management middleware. |
6
|
|
|
* |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @link https://github.com/juliangut/sessionware |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Jgut\Middleware\Sessionware; |
15
|
|
|
|
16
|
|
|
use Jgut\Middleware\Sessionware\Manager\Manager; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Session handling middleware. |
22
|
|
|
*/ |
23
|
|
|
class Sessionware |
24
|
|
|
{ |
25
|
|
|
const SESSION_KEY = '__SESSIONWARE_SESSION__'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Manager |
29
|
|
|
*/ |
30
|
|
|
protected $sessionManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Session |
34
|
|
|
*/ |
35
|
|
|
protected $session; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Middleware constructor. |
39
|
|
|
* |
40
|
|
|
* @param Manager $sessionManager |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Manager $sessionManager) |
43
|
|
|
{ |
44
|
|
|
$this->sessionManager = $sessionManager; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get session from request. |
49
|
|
|
* |
50
|
|
|
* @param ServerRequestInterface $request |
51
|
|
|
* |
52
|
|
|
* @return Session |
53
|
|
|
*/ |
54
|
|
|
public static function getSession(ServerRequestInterface $request) : Session |
55
|
|
|
{ |
56
|
|
|
return $request->getAttribute(static::SESSION_KEY); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Execute middleware. |
61
|
|
|
* |
62
|
|
|
* @param ServerRequestInterface $request |
63
|
|
|
* @param ResponseInterface $response |
64
|
|
|
* @param callable $next |
65
|
|
|
* |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
* |
68
|
|
|
* @return ResponseInterface |
69
|
|
|
*/ |
70
|
|
|
public function __invoke( |
71
|
|
|
ServerRequestInterface $request, |
72
|
|
|
ResponseInterface $response, |
73
|
|
|
callable $next |
74
|
|
|
) :ResponseInterface { |
75
|
|
|
$requestCookies = $request->getCookieParams(); |
76
|
|
|
$sessionName = $this->sessionManager->getConfiguration()->getName(); |
77
|
|
|
if (array_key_exists($sessionName, $requestCookies) && !empty($requestCookies[$sessionName])) { |
78
|
|
|
$this->sessionManager->setSessionId($requestCookies[$sessionName]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->session = new Session($this->sessionManager); |
82
|
|
|
|
83
|
|
|
$response = $next($request->withAttribute(static::SESSION_KEY, $this->session), $response); |
84
|
|
|
|
85
|
|
|
$response = $this->respondWithSessionCookie($response); |
86
|
|
|
|
87
|
|
|
$this->session->close(); |
88
|
|
|
|
89
|
|
|
return $response; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add session cookie Set-Cookie header to response. |
94
|
|
|
* |
95
|
|
|
* @param ResponseInterface $response |
96
|
|
|
* |
97
|
|
|
* @return ResponseInterface |
98
|
|
|
*/ |
99
|
|
|
protected function respondWithSessionCookie(ResponseInterface $response) : ResponseInterface |
100
|
|
|
{ |
101
|
|
|
if (!$this->session->isActive()) { |
102
|
|
|
return $response; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$configuration = $this->getConfiguration(); |
106
|
|
|
|
107
|
|
|
$timeoutKey = $configuration->getTimeoutKey(); |
108
|
|
|
$expireTime = $this->session->has($timeoutKey) |
109
|
|
|
? $this->session->get($timeoutKey) |
110
|
|
|
: time() - $configuration->getLifetime(); |
111
|
|
|
|
112
|
|
|
return $response->withAddedHeader( |
113
|
|
|
'Set-Cookie', |
114
|
|
|
sprintf( |
115
|
|
|
'%s=%s; %s', |
116
|
|
|
urlencode($configuration->getName()), |
117
|
|
|
urlencode($this->session->getId()), |
118
|
|
|
$this->getSessionCookieParameters($expireTime) |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get session cookie parameters. |
125
|
|
|
* |
126
|
|
|
* @param int $expireTime |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function getSessionCookieParameters(int $expireTime) : string |
131
|
|
|
{ |
132
|
|
|
$configuration = $this->getConfiguration(); |
133
|
|
|
|
134
|
|
|
$cookieParams = [ |
135
|
|
|
sprintf( |
136
|
|
|
'expires=%s; max-age=%s', |
137
|
|
|
gmdate('D, d M Y H:i:s T', $expireTime), |
138
|
|
|
$configuration->getLifetime() |
139
|
|
|
), |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
if (!empty($configuration->getCookiePath())) { |
143
|
|
|
$cookieParams[] = 'path=' . $configuration->getCookiePath(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (!empty($configuration->getCookieDomain())) { |
147
|
|
|
$cookieParams[] = 'domain=' . $configuration->getCookieDomain(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($configuration->isCookieSecure()) { |
151
|
|
|
$cookieParams[] = 'secure'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($configuration->isCookieHttpOnly()) { |
155
|
|
|
$cookieParams[] = 'httponly'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return implode('; ', $cookieParams); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get session configuration. |
163
|
|
|
* |
164
|
|
|
* @return Configuration |
165
|
|
|
*/ |
166
|
|
|
protected function getConfiguration() : Configuration |
167
|
|
|
{ |
168
|
|
|
return $this->sessionManager->getConfiguration(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|