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.

Session.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Clark Tomlinson <fallen013@gmail.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Encryption;
  28. use OCA\Encryption\Exceptions\PrivateKeyMissingException;
  29. use OCP\ISession;
  30. class Session {
  31. /** @var ISession */
  32. protected $session;
  33. public const NOT_INITIALIZED = '0';
  34. public const INIT_EXECUTED = '1';
  35. public const INIT_SUCCESSFUL = '2';
  36. /**
  37. * @param ISession $session
  38. */
  39. public function __construct(ISession $session) {
  40. $this->session = $session;
  41. }
  42. /**
  43. * Sets status of encryption app
  44. *
  45. * @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
  46. */
  47. public function setStatus($status) {
  48. $this->session->set('encryptionInitialized', $status);
  49. }
  50. /**
  51. * Gets status if we already tried to initialize the encryption app
  52. *
  53. * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
  54. */
  55. public function getStatus() {
  56. $status = $this->session->get('encryptionInitialized');
  57. if (is_null($status)) {
  58. $status = self::NOT_INITIALIZED;
  59. }
  60. return $status;
  61. }
  62. /**
  63. * check if encryption was initialized successfully
  64. *
  65. * @return bool
  66. */
  67. public function isReady() {
  68. $status = $this->getStatus();
  69. return $status === self::INIT_SUCCESSFUL;
  70. }
  71. /**
  72. * Gets user or public share private key from session
  73. *
  74. * @return string $privateKey The user's plaintext private key
  75. * @throws Exceptions\PrivateKeyMissingException
  76. */
  77. public function getPrivateKey() {
  78. $key = $this->session->get('privateKey');
  79. if (is_null($key)) {
  80. throw new Exceptions\PrivateKeyMissingException('please try to log-out and log-in again', 0);
  81. }
  82. return $key;
  83. }
  84. /**
  85. * check if private key is set
  86. *
  87. * @return boolean
  88. */
  89. public function isPrivateKeySet() {
  90. $key = $this->session->get('privateKey');
  91. if (is_null($key)) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. /**
  97. * Sets user private key to session
  98. *
  99. * @param string $key users private key
  100. *
  101. * @note this should only be set on login
  102. */
  103. public function setPrivateKey($key) {
  104. $this->session->set('privateKey', $key);
  105. }
  106. /**
  107. * store data needed for the decrypt all operation in the session
  108. *
  109. * @param string $user
  110. * @param string $key
  111. */
  112. public function prepareDecryptAll($user, $key) {
  113. $this->session->set('decryptAll', true);
  114. $this->session->set('decryptAllKey', $key);
  115. $this->session->set('decryptAllUid', $user);
  116. }
  117. /**
  118. * check if we are in decrypt all mode
  119. *
  120. * @return bool
  121. */
  122. public function decryptAllModeActivated() {
  123. $decryptAll = $this->session->get('decryptAll');
  124. return ($decryptAll === true);
  125. }
  126. /**
  127. * get uid used for decrypt all operation
  128. *
  129. * @return string
  130. * @throws \Exception
  131. */
  132. public function getDecryptAllUid() {
  133. $uid = $this->session->get('decryptAllUid');
  134. if (is_null($uid) && $this->decryptAllModeActivated()) {
  135. throw new \Exception('No uid found while in decrypt all mode');
  136. } elseif (is_null($uid)) {
  137. throw new \Exception('Please activate decrypt all mode first');
  138. }
  139. return $uid;
  140. }
  141. /**
  142. * get private key for decrypt all operation
  143. *
  144. * @return string
  145. * @throws PrivateKeyMissingException
  146. */
  147. public function getDecryptAllKey() {
  148. $privateKey = $this->session->get('decryptAllKey');
  149. if (is_null($privateKey) && $this->decryptAllModeActivated()) {
  150. throw new PrivateKeyMissingException('No private key found while in decrypt all mode');
  151. } elseif (is_null($privateKey)) {
  152. throw new PrivateKeyMissingException('Please activate decrypt all mode first');
  153. }
  154. return $privateKey;
  155. }
  156. /**
  157. * remove keys from session
  158. */
  159. public function clear() {
  160. $this->session->remove('publicSharePrivateKey');
  161. $this->session->remove('privateKey');
  162. $this->session->remove('encryptionInitialized');
  163. $this->session->remove('decryptAll');
  164. $this->session->remove('decryptAllKey');
  165. $this->session->remove('decryptAllUid');
  166. }
  167. }