1
0

CryptoSessionData.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $requesttoken = $this->get('requesttoken');
  142. $this->sessionValues = [];
  143. if ($requesttoken !== null) {
  144. $this->set('requesttoken', $requesttoken);
  145. }
  146. $this->isModified = true;
  147. $this->session->clear();
  148. }
  149. public function reopen(): bool {
  150. $reopened = $this->session->reopen();
  151. if ($reopened) {
  152. $this->initializeSession();
  153. }
  154. return $reopened;
  155. }
  156. /**
  157. * Wrapper around session_regenerate_id
  158. *
  159. * @param bool $deleteOldSession Whether to delete the old associated session file or not.
  160. * @param bool $updateToken Wheater to update the associated auth token
  161. * @return void
  162. */
  163. public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {
  164. $this->session->regenerateId($deleteOldSession, $updateToken);
  165. }
  166. /**
  167. * Wrapper around session_id
  168. *
  169. * @return string
  170. * @throws SessionNotAvailableException
  171. * @since 9.1.0
  172. */
  173. public function getId(): string {
  174. return $this->session->getId();
  175. }
  176. /**
  177. * Close the session and release the lock, also writes all changed data in batch
  178. */
  179. public function close() {
  180. if ($this->isModified) {
  181. $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
  182. $this->session->set(self::encryptedSessionName, $encryptedValue);
  183. $this->isModified = false;
  184. }
  185. $this->session->close();
  186. }
  187. /**
  188. * @param mixed $offset
  189. * @return bool
  190. */
  191. public function offsetExists($offset): bool {
  192. return $this->exists($offset);
  193. }
  194. /**
  195. * @param mixed $offset
  196. * @return mixed
  197. */
  198. #[\ReturnTypeWillChange]
  199. public function offsetGet($offset) {
  200. return $this->get($offset);
  201. }
  202. /**
  203. * @param mixed $offset
  204. * @param mixed $value
  205. */
  206. public function offsetSet($offset, $value): void {
  207. $this->set($offset, $value);
  208. }
  209. /**
  210. * @param mixed $offset
  211. */
  212. public function offsetUnset($offset): void {
  213. $this->remove($offset);
  214. }
  215. }