You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.1 KiB

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