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