1
0

Session.php 3.9 KB

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