1
0

Session.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption;
  8. use OCA\Encryption\Exceptions\PrivateKeyMissingException;
  9. use OCP\ISession;
  10. class Session {
  11. public const NOT_INITIALIZED = '0';
  12. public const INIT_EXECUTED = '1';
  13. public const INIT_SUCCESSFUL = '2';
  14. /**
  15. * @param ISession $session
  16. */
  17. public function __construct(
  18. protected ISession $session,
  19. ) {
  20. }
  21. /**
  22. * Sets status of encryption app
  23. *
  24. * @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
  25. */
  26. public function setStatus($status) {
  27. $this->session->set('encryptionInitialized', $status);
  28. }
  29. /**
  30. * Gets status if we already tried to initialize the encryption app
  31. *
  32. * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
  33. */
  34. public function getStatus() {
  35. $status = $this->session->get('encryptionInitialized');
  36. if (is_null($status)) {
  37. $status = self::NOT_INITIALIZED;
  38. }
  39. return $status;
  40. }
  41. /**
  42. * check if encryption was initialized successfully
  43. *
  44. * @return bool
  45. */
  46. public function isReady() {
  47. $status = $this->getStatus();
  48. return $status === self::INIT_SUCCESSFUL;
  49. }
  50. /**
  51. * Gets user or public share private key from session
  52. *
  53. * @return string $privateKey The user's plaintext private key
  54. * @throws Exceptions\PrivateKeyMissingException
  55. */
  56. public function getPrivateKey() {
  57. $key = $this->session->get('privateKey');
  58. if (is_null($key)) {
  59. throw new PrivateKeyMissingException('please try to log-out and log-in again', 0);
  60. }
  61. return $key;
  62. }
  63. /**
  64. * check if private key is set
  65. *
  66. * @return boolean
  67. */
  68. public function isPrivateKeySet() {
  69. $key = $this->session->get('privateKey');
  70. if (is_null($key)) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. /**
  76. * Sets user private key to session
  77. *
  78. * @param string $key users private key
  79. *
  80. * @note this should only be set on login
  81. */
  82. public function setPrivateKey($key) {
  83. $this->session->set('privateKey', $key);
  84. }
  85. /**
  86. * store data needed for the decrypt all operation in the session
  87. *
  88. * @param string $user
  89. * @param string $key
  90. */
  91. public function prepareDecryptAll($user, $key) {
  92. $this->session->set('decryptAll', true);
  93. $this->session->set('decryptAllKey', $key);
  94. $this->session->set('decryptAllUid', $user);
  95. }
  96. /**
  97. * check if we are in decrypt all mode
  98. *
  99. * @return bool
  100. */
  101. public function decryptAllModeActivated() {
  102. $decryptAll = $this->session->get('decryptAll');
  103. return ($decryptAll === true);
  104. }
  105. /**
  106. * get uid used for decrypt all operation
  107. *
  108. * @return string
  109. * @throws \Exception
  110. */
  111. public function getDecryptAllUid() {
  112. $uid = $this->session->get('decryptAllUid');
  113. if (is_null($uid) && $this->decryptAllModeActivated()) {
  114. throw new \Exception('No uid found while in decrypt all mode');
  115. } elseif (is_null($uid)) {
  116. throw new \Exception('Please activate decrypt all mode first');
  117. }
  118. return $uid;
  119. }
  120. /**
  121. * get private key for decrypt all operation
  122. *
  123. * @return string
  124. * @throws PrivateKeyMissingException
  125. */
  126. public function getDecryptAllKey() {
  127. $privateKey = $this->session->get('decryptAllKey');
  128. if (is_null($privateKey) && $this->decryptAllModeActivated()) {
  129. throw new PrivateKeyMissingException('No private key found while in decrypt all mode');
  130. } elseif (is_null($privateKey)) {
  131. throw new PrivateKeyMissingException('Please activate decrypt all mode first');
  132. }
  133. return $privateKey;
  134. }
  135. /**
  136. * remove keys from session
  137. */
  138. public function clear() {
  139. $this->session->remove('publicSharePrivateKey');
  140. $this->session->remove('privateKey');
  141. $this->session->remove('encryptionInitialized');
  142. $this->session->remove('decryptAll');
  143. $this->session->remove('decryptAllKey');
  144. $this->session->remove('decryptAllUid');
  145. }
  146. }