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
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Session request aware trait |
29
|
|
|
* |
30
|
|
|
* Trait for objects which need to know where the session attribute is stored in |
31
|
|
|
* the request. |
32
|
|
|
* |
33
|
|
|
* @category Trait |
34
|
|
|
* @package Vperyod\SessionHandler |
35
|
|
|
* @author Jake Johns <[email protected]> |
36
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
37
|
|
|
* @link https://github.com/vperyod/vperyod.session-handler |
38
|
|
|
*/ |
39
|
|
|
trait SessionRequestAwareTrait |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Session attribute |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
* |
46
|
|
|
* @access protected |
47
|
|
|
*/ |
48
|
|
|
protected $sessionAttribute = 'aura/session:session'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* CsrfName |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
* |
55
|
|
|
* @access protected |
56
|
|
|
*/ |
57
|
|
|
protected $csrfName = '__csrf_token'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* CsrfHeader |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
* |
64
|
|
|
* @access protected |
65
|
|
|
*/ |
66
|
|
|
protected $csrfHeader = 'X-Csrf-Token'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set session attribute |
70
|
|
|
* |
71
|
|
|
* @param string $attr name of attribute for session |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
*/ |
77
|
3 |
|
public function setSessionAttribute($attr) |
78
|
|
|
{ |
79
|
3 |
|
$this->sessionAttribute = $attr; |
80
|
3 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* SetCsrfName |
85
|
|
|
* |
86
|
|
|
* @param mixed $name DESCRIPTION |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
* |
90
|
|
|
* @access public |
91
|
|
|
*/ |
92
|
1 |
|
public function setCsrfName($name) |
93
|
|
|
{ |
94
|
1 |
|
$this->csrfName = $name; |
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* GetCsrfName |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
* |
103
|
|
|
* @access public |
104
|
|
|
*/ |
105
|
9 |
|
public function getCsrfName() |
106
|
|
|
{ |
107
|
9 |
|
return $this->csrfName; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* SetCsrfHeader |
112
|
|
|
* |
113
|
|
|
* @param mixed $header DESCRIPTION |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
* |
117
|
|
|
* @access public |
118
|
|
|
*/ |
119
|
1 |
|
public function setCsrfHeader($header) |
120
|
|
|
{ |
121
|
1 |
|
$this->csrfHeader = $header; |
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* GetCsrfHeader |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
* |
130
|
|
|
* @access public |
131
|
|
|
*/ |
132
|
8 |
|
public function getCsrfHeader() |
133
|
|
|
{ |
134
|
8 |
|
return $this->csrfHeader; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get session from request |
139
|
|
|
* |
140
|
|
|
* @param Request $request PSR7 Request |
141
|
|
|
* |
142
|
|
|
* @return Session |
143
|
|
|
* @throws InvalidArgumentException if session attribute is invalid |
144
|
|
|
* |
145
|
|
|
* @access protected |
146
|
|
|
*/ |
147
|
6 |
|
protected function getSession(Request $request) |
148
|
|
|
{ |
149
|
6 |
|
$session = $request->getAttribute($this->sessionAttribute); |
150
|
6 |
|
if (! $session instanceof Session) { |
151
|
1 |
|
throw new Exception( |
152
|
|
|
'Session not available in request at: ' |
153
|
1 |
|
. $this->sessionAttribute |
154
|
|
|
); |
155
|
|
|
} |
156
|
5 |
|
return $session; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* GetCsrfSpec |
161
|
|
|
* |
162
|
|
|
* @param Request $request DESCRIPTION |
163
|
|
|
* |
164
|
|
|
* @return mixed |
165
|
|
|
* |
166
|
|
|
* @access protected |
167
|
|
|
*/ |
168
|
1 |
|
protected function getCsrfSpec(Request $request) |
169
|
|
|
{ |
170
|
1 |
|
$value = $this->getSession($request) |
171
|
1 |
|
->getCsrfToken() |
172
|
1 |
|
->getValue(); |
173
|
|
|
|
174
|
|
|
return [ |
175
|
1 |
|
'type' => 'hidden', |
176
|
1 |
|
'name' => $this->getCsrfName(), |
177
|
1 |
|
'value' => $value, |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|