|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Session Handler |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (C) 2016 Jake Johns |
|
8
|
|
|
* |
|
9
|
|
|
* This software may be modified and distributed under the terms |
|
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
11
|
|
|
* |
|
12
|
|
|
* @category Trait |
|
13
|
|
|
* @package Vperyod\SessionHandler |
|
14
|
|
|
* @author Jake Johns <[email protected]> |
|
15
|
|
|
* @copyright 2016 Jake Johns |
|
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
17
|
|
|
* @link https://github.com/vperyod/vperyod.session-handler |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Vperyod\SessionHandler; |
|
21
|
|
|
|
|
22
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
23
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
24
|
|
|
|
|
25
|
|
|
use Aura\Session\Session; |
|
26
|
|
|
use Aura\Session\SegmentInterface as Segment; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Session request aware trait |
|
30
|
|
|
* |
|
31
|
|
|
* Trait for objects which need to know where the session attribute is stored in |
|
32
|
|
|
* the request and some helpers to interact with it. |
|
33
|
|
|
* |
|
34
|
|
|
* @category Trait |
|
35
|
|
|
* @package Vperyod\SessionHandler |
|
36
|
|
|
* @author Jake Johns <[email protected]> |
|
37
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
38
|
|
|
* @link https://github.com/vperyod/vperyod.session-handler |
|
39
|
|
|
*/ |
|
40
|
|
|
trait SessionRequestAwareTrait |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* Attribute on which to strore session object in request |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
* |
|
47
|
|
|
* @access protected |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $sessionAttribute = 'aura/session:session'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Name of posted CSRF token form field |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
* |
|
56
|
|
|
* @access protected |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $csrfName = '__csrf_token'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Name of request header storing CSRF token |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
* |
|
65
|
|
|
* @access protected |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $csrfHeader = 'X-Csrf-Token'; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Namespace for default message storage |
|
71
|
|
|
* |
|
72
|
|
|
* @var string |
|
73
|
|
|
* |
|
74
|
|
|
* @access protected |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $messageSegmentName = 'vperyod/session-handler:messages'; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Factory to create a messenger |
|
80
|
|
|
* |
|
81
|
|
|
* @var callable |
|
82
|
|
|
* |
|
83
|
|
|
* @access protected |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $messengerFactory; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set session attribute |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $attr name of attribute for session |
|
91
|
|
|
* |
|
92
|
|
|
* @return $this |
|
93
|
|
|
* |
|
94
|
|
|
* @access public |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function setSessionAttribute($attr) |
|
97
|
|
|
{ |
|
98
|
2 |
|
$this->sessionAttribute = $attr; |
|
99
|
2 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get name of attribute where session is stored |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
* |
|
107
|
|
|
* @access protected |
|
108
|
|
|
*/ |
|
109
|
11 |
|
protected function getSessionAttribute() |
|
110
|
|
|
{ |
|
111
|
11 |
|
return $this->sessionAttribute; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Set name of form field containing CSRF token |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $name name of CSRF form field |
|
118
|
|
|
* |
|
119
|
|
|
* @return $this |
|
120
|
|
|
* |
|
121
|
|
|
* @access public |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function setCsrfName($name) |
|
124
|
|
|
{ |
|
125
|
1 |
|
$this->csrfName = $name; |
|
126
|
1 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get CSRF form field name |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
* |
|
134
|
|
|
* @access protected |
|
135
|
|
|
*/ |
|
136
|
10 |
|
protected function getCsrfName() |
|
137
|
|
|
{ |
|
138
|
10 |
|
return $this->csrfName; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Set CSRF header name |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $header name of header for CSRF token |
|
145
|
|
|
* |
|
146
|
|
|
* @return $this |
|
147
|
|
|
* |
|
148
|
|
|
* @access public |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function setCsrfHeader($header) |
|
151
|
|
|
{ |
|
152
|
1 |
|
$this->csrfHeader = $header; |
|
153
|
1 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get name of header for CSRF token |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
* |
|
161
|
|
|
* @access protected |
|
162
|
|
|
*/ |
|
163
|
8 |
|
protected function getCsrfHeader() |
|
164
|
|
|
{ |
|
165
|
8 |
|
return $this->csrfHeader; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set namespace for default messenger storage |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $name name of session segment |
|
172
|
|
|
* |
|
173
|
|
|
* @return $this |
|
174
|
|
|
* |
|
175
|
|
|
* @access public |
|
176
|
|
|
*/ |
|
177
|
1 |
|
public function setMessageSegmentName($name) |
|
178
|
|
|
{ |
|
179
|
1 |
|
$this->messageSegmentName = $name; |
|
180
|
1 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get namespace for message segment |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
* |
|
188
|
|
|
* @access protected |
|
189
|
|
|
*/ |
|
190
|
4 |
|
protected function getMessageSegmentName() |
|
191
|
|
|
{ |
|
192
|
4 |
|
return $this->messageSegmentName; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Set factory for default messenger |
|
197
|
|
|
* |
|
198
|
|
|
* @param callable $factory factory to create a messenger |
|
199
|
|
|
* |
|
200
|
|
|
* @return $this |
|
201
|
|
|
* |
|
202
|
|
|
* @access public |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function setMessengerFactory(callable $factory) |
|
205
|
|
|
{ |
|
206
|
1 |
|
$this->messengerFactory = $factory; |
|
207
|
1 |
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get factory to create a messenger |
|
212
|
|
|
* |
|
213
|
|
|
* @return callable |
|
214
|
|
|
* |
|
215
|
|
|
* @access protected |
|
216
|
|
|
*/ |
|
217
|
2 |
|
protected function getMessengerFactory() |
|
218
|
|
|
{ |
|
219
|
2 |
|
if (null === $this->messengerFactory) { |
|
220
|
1 |
|
$this->messengerFactory = [$this, 'messengerFactory']; |
|
221
|
1 |
|
} |
|
222
|
2 |
|
return $this->messengerFactory; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Get session from request |
|
227
|
|
|
* |
|
228
|
|
|
* @param Request $request PSR7 Request |
|
229
|
|
|
* |
|
230
|
|
|
* @return Session |
|
231
|
|
|
* @throws Exception if session attribute is invalid |
|
232
|
|
|
* |
|
233
|
|
|
* @access protected |
|
234
|
|
|
*/ |
|
235
|
9 |
|
protected function getSession(Request $request) |
|
236
|
|
|
{ |
|
237
|
9 |
|
$session = $request->getAttribute($this->getSessionAttribute()); |
|
238
|
9 |
|
if (! $session instanceof Session) { |
|
239
|
1 |
|
throw new Exception( |
|
240
|
|
|
'Session not available in request at: ' |
|
241
|
1 |
|
. $this->sessionAttribute |
|
242
|
1 |
|
); |
|
243
|
|
|
} |
|
244
|
8 |
|
return $session; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get spec for CSRF form field |
|
249
|
|
|
* |
|
250
|
|
|
* @param Request $request PSR7 Request |
|
251
|
|
|
* |
|
252
|
|
|
* @return array |
|
253
|
|
|
* |
|
254
|
|
|
* @access protected |
|
255
|
|
|
*/ |
|
256
|
1 |
|
protected function getCsrfSpec(Request $request) |
|
257
|
|
|
{ |
|
258
|
1 |
|
$value = $this->getSession($request) |
|
259
|
1 |
|
->getCsrfToken() |
|
260
|
1 |
|
->getValue(); |
|
261
|
|
|
|
|
262
|
|
|
return [ |
|
263
|
1 |
|
'type' => 'hidden', |
|
264
|
1 |
|
'name' => $this->getCsrfName(), |
|
265
|
1 |
|
'value' => $value, |
|
266
|
1 |
|
]; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Get session segment for default messenger |
|
271
|
|
|
* |
|
272
|
|
|
* @param Request $request PSR7 Request |
|
273
|
|
|
* |
|
274
|
|
|
* @return Aura\Session\SegmentInterface |
|
275
|
|
|
* |
|
276
|
|
|
* @access protected |
|
277
|
|
|
*/ |
|
278
|
3 |
|
protected function getMessageSegment(Request $request) |
|
279
|
|
|
{ |
|
280
|
3 |
|
return $this->getSession($request) |
|
281
|
3 |
|
->getSegment($this->getMessageSegmentName()); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Create a new default messenger from the message segment |
|
286
|
|
|
* |
|
287
|
|
|
* @param Request $request PSR7 Request |
|
288
|
|
|
* |
|
289
|
|
|
* @return Messenger |
|
290
|
|
|
* |
|
291
|
|
|
* @access protected |
|
292
|
|
|
*/ |
|
293
|
2 |
|
protected function newMessenger(Request $request) |
|
294
|
|
|
{ |
|
295
|
2 |
|
$factory = $this->getMessengerFactory(); |
|
296
|
2 |
|
$segment = $this->getMessageSegment($request); |
|
297
|
2 |
|
return $factory($segment); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Create a messenger, default messenger factory |
|
302
|
|
|
* |
|
303
|
|
|
* @param Segment $segment session segment |
|
304
|
|
|
* |
|
305
|
|
|
* @return Messenger |
|
306
|
|
|
* |
|
307
|
|
|
* @access protected |
|
308
|
|
|
*/ |
|
309
|
1 |
|
protected function messengerFactory(Segment $segment) |
|
310
|
|
|
{ |
|
311
|
1 |
|
return new Messenger($segment); |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|