CryptoSessionData.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. $this->sessionValues[$key] = $value;
  93. $this->isModified = true;
  94. }
  95. /**
  96. * Get a value from the session
  97. *
  98. * @param string $key
  99. * @return string|null Either the value or null
  100. */
  101. public function get(string $key) {
  102. if (isset($this->sessionValues[$key])) {
  103. return $this->sessionValues[$key];
  104. }
  105. return null;
  106. }
  107. /**
  108. * Check if a named key exists in the session
  109. *
  110. * @param string $key
  111. * @return bool
  112. */
  113. public function exists(string $key): bool {
  114. return isset($this->sessionValues[$key]);
  115. }
  116. /**
  117. * Remove a $key/$value pair from the session
  118. *
  119. * @param string $key
  120. */
  121. public function remove(string $key) {
  122. $this->isModified = true;
  123. unset($this->sessionValues[$key]);
  124. $this->session->remove(self::encryptedSessionName);
  125. }
  126. /**
  127. * Reset and recreate the session
  128. */
  129. public function clear() {
  130. $requesttoken = $this->get('requesttoken');
  131. $this->sessionValues = [];
  132. if ($requesttoken !== null) {
  133. $this->set('requesttoken', $requesttoken);
  134. }
  135. $this->isModified = true;
  136. $this->session->clear();
  137. }
  138. /**
  139. * Wrapper around session_regenerate_id
  140. *
  141. * @param bool $deleteOldSession Whether to delete the old associated session file or not.
  142. * @param bool $updateToken Wheater to update the associated auth token
  143. * @return void
  144. */
  145. public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {
  146. $this->session->regenerateId($deleteOldSession, $updateToken);
  147. }
  148. /**
  149. * Wrapper around session_id
  150. *
  151. * @return string
  152. * @throws SessionNotAvailableException
  153. * @since 9.1.0
  154. */
  155. public function getId(): string {
  156. return $this->session->getId();
  157. }
  158. /**
  159. * Close the session and release the lock, also writes all changed data in batch
  160. */
  161. public function close() {
  162. if ($this->isModified) {
  163. $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
  164. $this->session->set(self::encryptedSessionName, $encryptedValue);
  165. $this->isModified = false;
  166. }
  167. $this->session->close();
  168. }
  169. /**
  170. * @param mixed $offset
  171. * @return bool
  172. */
  173. public function offsetExists($offset): bool {
  174. return $this->exists($offset);
  175. }
  176. /**
  177. * @param mixed $offset
  178. * @return mixed
  179. */
  180. #[\ReturnTypeWillChange]
  181. public function offsetGet($offset) {
  182. return $this->get($offset);
  183. }
  184. /**
  185. * @param mixed $offset
  186. * @param mixed $value
  187. */
  188. public function offsetSet($offset, $value): void {
  189. $this->set($offset, $value);
  190. }
  191. /**
  192. * @param mixed $offset
  193. */
  194. public function offsetUnset($offset): void {
  195. $this->remove($offset);
  196. }
  197. }