Session.php 4.6 KB

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