1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Joas Schilling <[email protected]> |
4
|
|
|
* @author Lukas Reschke <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
7
|
|
|
* @license AGPL-3.0 |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OC\Session; |
24
|
|
|
|
25
|
|
|
use OCP\ISession; |
26
|
|
|
use OCP\Security\ICrypto; |
27
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class CryptoSessionData |
31
|
|
|
* |
32
|
|
|
* @package OC\Session |
33
|
|
|
*/ |
34
|
|
|
class CryptoSessionData implements \ArrayAccess, ISession { |
35
|
|
|
/** @var ISession */ |
36
|
|
|
protected $session; |
37
|
|
|
/** @var \OCP\Security\ICrypto */ |
38
|
|
|
protected $crypto; |
39
|
|
|
/** @var string */ |
40
|
|
|
protected $passphrase; |
41
|
|
|
/** @var array */ |
42
|
|
|
protected $sessionValues; |
43
|
|
|
/** @var bool */ |
44
|
|
|
protected $isModified = false; |
45
|
|
|
CONST encryptedSessionName = 'encrypted_session_data'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ISession $session |
49
|
|
|
* @param ICrypto $crypto |
50
|
|
|
* @param string $passphrase |
51
|
|
|
*/ |
52
|
|
|
public function __construct(ISession $session, |
53
|
|
|
ICrypto $crypto, |
54
|
|
|
$passphrase) { |
55
|
|
|
$this->crypto = $crypto; |
56
|
|
|
$this->session = $session; |
57
|
|
|
$this->passphrase = $passphrase; |
58
|
|
|
$this->initializeSession(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Close session if class gets destructed |
63
|
|
|
*/ |
64
|
|
|
public function __destruct() { |
65
|
|
|
$this->close(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function initializeSession() { |
69
|
|
|
$encryptedSessionData = $this->session->get(self::encryptedSessionName); |
70
|
|
|
try { |
71
|
|
|
$this->sessionValues = json_decode( |
|
|
|
|
72
|
|
|
$this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
73
|
|
|
true |
74
|
|
|
); |
75
|
|
|
} catch (\Exception $e) { |
76
|
|
|
$this->sessionValues = []; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set a value in the session |
82
|
|
|
* |
83
|
|
|
* @param string $key |
84
|
|
|
* @param mixed $value |
85
|
|
|
*/ |
86
|
|
|
public function set($key, $value) { |
87
|
|
|
$this->sessionValues[$key] = $value; |
88
|
|
|
$this->isModified = true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get a value from the session |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @return string|null Either the value or null |
96
|
|
|
*/ |
97
|
|
|
public function get($key) { |
98
|
|
|
if(isset($this->sessionValues[$key])) { |
99
|
|
|
return $this->sessionValues[$key]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check if a named key exists in the session |
107
|
|
|
* |
108
|
|
|
* @param string $key |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function exists($key) { |
112
|
|
|
return isset($this->sessionValues[$key]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Remove a $key/$value pair from the session |
117
|
|
|
* |
118
|
|
|
* @param string $key |
119
|
|
|
*/ |
120
|
|
|
public function remove($key) { |
121
|
|
|
$this->isModified = true; |
122
|
|
|
unset($this->sessionValues[$key]); |
123
|
|
|
$this->session->remove(self::encryptedSessionName); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Reset and recreate the session |
128
|
|
|
*/ |
129
|
|
|
public function clear() { |
130
|
|
|
$this->sessionValues = []; |
131
|
|
|
$this->isModified = true; |
132
|
|
|
$this->session->clear(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Wrapper around session_regenerate_id |
137
|
|
|
* |
138
|
|
|
* @param bool $deleteOldSession Whether to delete the old associated session file or not. |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function regenerateId($deleteOldSession = true) { |
142
|
|
|
$this->session->regenerateId($deleteOldSession); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Wrapper around session_id |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
* @throws SessionNotAvailableException |
150
|
|
|
* @since 9.1.0 |
151
|
|
|
*/ |
152
|
|
|
public function getId() { |
153
|
|
|
return $this->session->getId(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Close the session and release the lock, also writes all changed data in batch |
158
|
|
|
*/ |
159
|
|
|
public function close() { |
160
|
|
|
if($this->isModified) { |
161
|
|
|
$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
162
|
|
|
$this->session->set(self::encryptedSessionName, $encryptedValue); |
163
|
|
|
$this->isModified = false; |
164
|
|
|
} |
165
|
|
|
$this->session->close(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param mixed $offset |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public function offsetExists($offset) { |
173
|
|
|
return $this->exists($offset); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param mixed $offset |
178
|
|
|
* @return mixed |
179
|
|
|
*/ |
180
|
|
|
public function offsetGet($offset) { |
181
|
|
|
return $this->get($offset); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param mixed $offset |
186
|
|
|
* @param mixed $value |
187
|
|
|
*/ |
188
|
|
|
public function offsetSet($offset, $value) { |
189
|
|
|
$this->set($offset, $value); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param mixed $offset |
194
|
|
|
*/ |
195
|
|
|
public function offsetUnset($offset) { |
196
|
|
|
$this->remove($offset); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..