CryptoSessionData.php 4.4 KB

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