Session.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Clark Tomlinson <fallen013@gmail.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 OCA\Encryption;
  25. use OCA\Encryption\Exceptions\PrivateKeyMissingException;
  26. use \OCP\ISession;
  27. class Session {
  28. /** @var ISession */
  29. protected $session;
  30. const NOT_INITIALIZED = '0';
  31. const INIT_EXECUTED = '1';
  32. const INIT_SUCCESSFUL = '2';
  33. const RUN_MIGRATION = '3';
  34. /**
  35. * @param ISession $session
  36. */
  37. public function __construct(ISession $session) {
  38. $this->session = $session;
  39. }
  40. /**
  41. * Sets status of encryption app
  42. *
  43. * @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
  44. */
  45. public function setStatus($status) {
  46. $this->session->set('encryptionInitialized', $status);
  47. }
  48. /**
  49. * Gets status if we already tried to initialize the encryption app
  50. *
  51. * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
  52. */
  53. public function getStatus() {
  54. $status = $this->session->get('encryptionInitialized');
  55. if (is_null($status)) {
  56. $status = self::NOT_INITIALIZED;
  57. }
  58. return $status;
  59. }
  60. /**
  61. * Gets user or public share private key from session
  62. *
  63. * @return string $privateKey The user's plaintext private key
  64. * @throws Exceptions\PrivateKeyMissingException
  65. */
  66. public function getPrivateKey() {
  67. $key = $this->session->get('privateKey');
  68. if (is_null($key)) {
  69. throw new Exceptions\PrivateKeyMissingException('please try to log-out and log-in again', 0);
  70. }
  71. return $key;
  72. }
  73. /**
  74. * check if private key is set
  75. *
  76. * @return boolean
  77. */
  78. public function isPrivateKeySet() {
  79. $key = $this->session->get('privateKey');
  80. if (is_null($key)) {
  81. return false;
  82. }
  83. return true;
  84. }
  85. /**
  86. * Sets user private key to session
  87. *
  88. * @param string $key users private key
  89. *
  90. * @note this should only be set on login
  91. */
  92. public function setPrivateKey($key) {
  93. $this->session->set('privateKey', $key);
  94. }
  95. /**
  96. * store data needed for the decrypt all operation in the session
  97. *
  98. * @param string $user
  99. * @param string $key
  100. */
  101. public function prepareDecryptAll($user, $key) {
  102. $this->session->set('decryptAll', true);
  103. $this->session->set('decryptAllKey', $key);
  104. $this->session->set('decryptAllUid', $user);
  105. }
  106. /**
  107. * check if we are in decrypt all mode
  108. *
  109. * @return bool
  110. */
  111. public function decryptAllModeActivated() {
  112. $decryptAll = $this->session->get('decryptAll');
  113. return ($decryptAll === true);
  114. }
  115. /**
  116. * get uid used for decrypt all operation
  117. *
  118. * @return string
  119. * @throws \Exception
  120. */
  121. public function getDecryptAllUid() {
  122. $uid = $this->session->get('decryptAllUid');
  123. if (is_null($uid) && $this->decryptAllModeActivated()) {
  124. throw new \Exception('No uid found while in decrypt all mode');
  125. } elseif (is_null($uid)) {
  126. throw new \Exception('Please activate decrypt all mode first');
  127. }
  128. return $uid;
  129. }
  130. /**
  131. * get private key for decrypt all operation
  132. *
  133. * @return string
  134. * @throws PrivateKeyMissingException
  135. */
  136. public function getDecryptAllKey() {
  137. $privateKey = $this->session->get('decryptAllKey');
  138. if (is_null($privateKey) && $this->decryptAllModeActivated()) {
  139. throw new PrivateKeyMissingException('No private key found while in decrypt all mode');
  140. } elseif (is_null($privateKey)) {
  141. throw new PrivateKeyMissingException('Please activate decrypt all mode first');
  142. }
  143. return $privateKey;
  144. }
  145. /**
  146. * remove keys from session
  147. */
  148. public function clear() {
  149. $this->session->remove('publicSharePrivateKey');
  150. $this->session->remove('privateKey');
  151. $this->session->remove('encryptionInitialized');
  152. $this->session->remove('decryptAll');
  153. $this->session->remove('decryptAllKey');
  154. $this->session->remove('decryptAllUid');
  155. }
  156. }