cryptosessiondata.php 4.1 KB

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