1
0

CryptoSessionData.php 5.6 KB

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