@@ -39,199 +39,199 @@ |
||
39 | 39 | * @package OC\Session |
40 | 40 | */ |
41 | 41 | class CryptoSessionData implements \ArrayAccess, ISession { |
42 | - /** @var ISession */ |
|
43 | - protected $session; |
|
44 | - /** @var \OCP\Security\ICrypto */ |
|
45 | - protected $crypto; |
|
46 | - /** @var string */ |
|
47 | - protected $passphrase; |
|
48 | - /** @var array */ |
|
49 | - protected $sessionValues; |
|
50 | - /** @var bool */ |
|
51 | - protected $isModified = false; |
|
52 | - public const encryptedSessionName = 'encrypted_session_data'; |
|
53 | - |
|
54 | - /** |
|
55 | - * @param ISession $session |
|
56 | - * @param ICrypto $crypto |
|
57 | - * @param string $passphrase |
|
58 | - */ |
|
59 | - public function __construct(ISession $session, |
|
60 | - ICrypto $crypto, |
|
61 | - string $passphrase) { |
|
62 | - $this->crypto = $crypto; |
|
63 | - $this->session = $session; |
|
64 | - $this->passphrase = $passphrase; |
|
65 | - $this->initializeSession(); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Close session if class gets destructed |
|
70 | - */ |
|
71 | - public function __destruct() { |
|
72 | - try { |
|
73 | - $this->close(); |
|
74 | - } catch (SessionNotAvailableException $e) { |
|
75 | - // This exception can occur if session is already closed |
|
76 | - // So it is safe to ignore it and let the garbage collector to proceed |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - protected function initializeSession() { |
|
81 | - $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: ''; |
|
82 | - try { |
|
83 | - $this->sessionValues = json_decode( |
|
84 | - $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
85 | - true |
|
86 | - ); |
|
87 | - } catch (\Exception $e) { |
|
88 | - $this->sessionValues = []; |
|
89 | - $this->regenerateId(true, false); |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Set a value in the session |
|
95 | - * |
|
96 | - * @param string $key |
|
97 | - * @param mixed $value |
|
98 | - */ |
|
99 | - public function set(string $key, $value) { |
|
100 | - if ($this->get($key) === $value) { |
|
101 | - // Do not write the session if the value hasn't changed to avoid reopening |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - $reopened = $this->reopen(); |
|
106 | - $this->sessionValues[$key] = $value; |
|
107 | - $this->isModified = true; |
|
108 | - if ($reopened) { |
|
109 | - $this->close(); |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get a value from the session |
|
115 | - * |
|
116 | - * @param string $key |
|
117 | - * @return string|null Either the value or null |
|
118 | - */ |
|
119 | - public function get(string $key) { |
|
120 | - if (isset($this->sessionValues[$key])) { |
|
121 | - return $this->sessionValues[$key]; |
|
122 | - } |
|
123 | - |
|
124 | - return null; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Check if a named key exists in the session |
|
129 | - * |
|
130 | - * @param string $key |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function exists(string $key): bool { |
|
134 | - return isset($this->sessionValues[$key]); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Remove a $key/$value pair from the session |
|
139 | - * |
|
140 | - * @param string $key |
|
141 | - */ |
|
142 | - public function remove(string $key) { |
|
143 | - $reopened = $this->reopen(); |
|
144 | - $this->isModified = true; |
|
145 | - unset($this->sessionValues[$key]); |
|
146 | - if ($reopened) { |
|
147 | - $this->close(); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Reset and recreate the session |
|
153 | - */ |
|
154 | - public function clear() { |
|
155 | - $requesttoken = $this->get('requesttoken'); |
|
156 | - $this->sessionValues = []; |
|
157 | - if ($requesttoken !== null) { |
|
158 | - $this->set('requesttoken', $requesttoken); |
|
159 | - } |
|
160 | - $this->isModified = true; |
|
161 | - $this->session->clear(); |
|
162 | - } |
|
163 | - |
|
164 | - public function reopen(): bool { |
|
165 | - $reopened = $this->session->reopen(); |
|
166 | - if ($reopened) { |
|
167 | - $this->initializeSession(); |
|
168 | - } |
|
169 | - return $reopened; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Wrapper around session_regenerate_id |
|
174 | - * |
|
175 | - * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
176 | - * @param bool $updateToken Wheater to update the associated auth token |
|
177 | - * @return void |
|
178 | - */ |
|
179 | - public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) { |
|
180 | - $this->session->regenerateId($deleteOldSession, $updateToken); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Wrapper around session_id |
|
185 | - * |
|
186 | - * @return string |
|
187 | - * @throws SessionNotAvailableException |
|
188 | - * @since 9.1.0 |
|
189 | - */ |
|
190 | - public function getId(): string { |
|
191 | - return $this->session->getId(); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Close the session and release the lock, also writes all changed data in batch |
|
196 | - */ |
|
197 | - public function close() { |
|
198 | - if ($this->isModified) { |
|
199 | - $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
200 | - $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
201 | - $this->isModified = false; |
|
202 | - } |
|
203 | - $this->session->close(); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @param mixed $offset |
|
208 | - * @return bool |
|
209 | - */ |
|
210 | - public function offsetExists($offset): bool { |
|
211 | - return $this->exists($offset); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @param mixed $offset |
|
216 | - * @return mixed |
|
217 | - */ |
|
218 | - #[\ReturnTypeWillChange] |
|
219 | - public function offsetGet($offset) { |
|
220 | - return $this->get($offset); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * @param mixed $offset |
|
225 | - * @param mixed $value |
|
226 | - */ |
|
227 | - public function offsetSet($offset, $value): void { |
|
228 | - $this->set($offset, $value); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @param mixed $offset |
|
233 | - */ |
|
234 | - public function offsetUnset($offset): void { |
|
235 | - $this->remove($offset); |
|
236 | - } |
|
42 | + /** @var ISession */ |
|
43 | + protected $session; |
|
44 | + /** @var \OCP\Security\ICrypto */ |
|
45 | + protected $crypto; |
|
46 | + /** @var string */ |
|
47 | + protected $passphrase; |
|
48 | + /** @var array */ |
|
49 | + protected $sessionValues; |
|
50 | + /** @var bool */ |
|
51 | + protected $isModified = false; |
|
52 | + public const encryptedSessionName = 'encrypted_session_data'; |
|
53 | + |
|
54 | + /** |
|
55 | + * @param ISession $session |
|
56 | + * @param ICrypto $crypto |
|
57 | + * @param string $passphrase |
|
58 | + */ |
|
59 | + public function __construct(ISession $session, |
|
60 | + ICrypto $crypto, |
|
61 | + string $passphrase) { |
|
62 | + $this->crypto = $crypto; |
|
63 | + $this->session = $session; |
|
64 | + $this->passphrase = $passphrase; |
|
65 | + $this->initializeSession(); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Close session if class gets destructed |
|
70 | + */ |
|
71 | + public function __destruct() { |
|
72 | + try { |
|
73 | + $this->close(); |
|
74 | + } catch (SessionNotAvailableException $e) { |
|
75 | + // This exception can occur if session is already closed |
|
76 | + // So it is safe to ignore it and let the garbage collector to proceed |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + protected function initializeSession() { |
|
81 | + $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: ''; |
|
82 | + try { |
|
83 | + $this->sessionValues = json_decode( |
|
84 | + $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
85 | + true |
|
86 | + ); |
|
87 | + } catch (\Exception $e) { |
|
88 | + $this->sessionValues = []; |
|
89 | + $this->regenerateId(true, false); |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Set a value in the session |
|
95 | + * |
|
96 | + * @param string $key |
|
97 | + * @param mixed $value |
|
98 | + */ |
|
99 | + public function set(string $key, $value) { |
|
100 | + if ($this->get($key) === $value) { |
|
101 | + // Do not write the session if the value hasn't changed to avoid reopening |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + $reopened = $this->reopen(); |
|
106 | + $this->sessionValues[$key] = $value; |
|
107 | + $this->isModified = true; |
|
108 | + if ($reopened) { |
|
109 | + $this->close(); |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get a value from the session |
|
115 | + * |
|
116 | + * @param string $key |
|
117 | + * @return string|null Either the value or null |
|
118 | + */ |
|
119 | + public function get(string $key) { |
|
120 | + if (isset($this->sessionValues[$key])) { |
|
121 | + return $this->sessionValues[$key]; |
|
122 | + } |
|
123 | + |
|
124 | + return null; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Check if a named key exists in the session |
|
129 | + * |
|
130 | + * @param string $key |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function exists(string $key): bool { |
|
134 | + return isset($this->sessionValues[$key]); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Remove a $key/$value pair from the session |
|
139 | + * |
|
140 | + * @param string $key |
|
141 | + */ |
|
142 | + public function remove(string $key) { |
|
143 | + $reopened = $this->reopen(); |
|
144 | + $this->isModified = true; |
|
145 | + unset($this->sessionValues[$key]); |
|
146 | + if ($reopened) { |
|
147 | + $this->close(); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Reset and recreate the session |
|
153 | + */ |
|
154 | + public function clear() { |
|
155 | + $requesttoken = $this->get('requesttoken'); |
|
156 | + $this->sessionValues = []; |
|
157 | + if ($requesttoken !== null) { |
|
158 | + $this->set('requesttoken', $requesttoken); |
|
159 | + } |
|
160 | + $this->isModified = true; |
|
161 | + $this->session->clear(); |
|
162 | + } |
|
163 | + |
|
164 | + public function reopen(): bool { |
|
165 | + $reopened = $this->session->reopen(); |
|
166 | + if ($reopened) { |
|
167 | + $this->initializeSession(); |
|
168 | + } |
|
169 | + return $reopened; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Wrapper around session_regenerate_id |
|
174 | + * |
|
175 | + * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
176 | + * @param bool $updateToken Wheater to update the associated auth token |
|
177 | + * @return void |
|
178 | + */ |
|
179 | + public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) { |
|
180 | + $this->session->regenerateId($deleteOldSession, $updateToken); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Wrapper around session_id |
|
185 | + * |
|
186 | + * @return string |
|
187 | + * @throws SessionNotAvailableException |
|
188 | + * @since 9.1.0 |
|
189 | + */ |
|
190 | + public function getId(): string { |
|
191 | + return $this->session->getId(); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Close the session and release the lock, also writes all changed data in batch |
|
196 | + */ |
|
197 | + public function close() { |
|
198 | + if ($this->isModified) { |
|
199 | + $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
200 | + $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
201 | + $this->isModified = false; |
|
202 | + } |
|
203 | + $this->session->close(); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @param mixed $offset |
|
208 | + * @return bool |
|
209 | + */ |
|
210 | + public function offsetExists($offset): bool { |
|
211 | + return $this->exists($offset); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @param mixed $offset |
|
216 | + * @return mixed |
|
217 | + */ |
|
218 | + #[\ReturnTypeWillChange] |
|
219 | + public function offsetGet($offset) { |
|
220 | + return $this->get($offset); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * @param mixed $offset |
|
225 | + * @param mixed $value |
|
226 | + */ |
|
227 | + public function offsetSet($offset, $value): void { |
|
228 | + $this->set($offset, $value); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @param mixed $offset |
|
233 | + */ |
|
234 | + public function offsetUnset($offset): void { |
|
235 | + $this->remove($offset); |
|
236 | + } |
|
237 | 237 | } |