SettingsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-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\Controller;
  8. use OCA\Encryption\Crypto\Crypt;
  9. use OCA\Encryption\KeyManager;
  10. use OCA\Encryption\Session;
  11. use OCA\Encryption\Util;
  12. use OCP\AppFramework\Controller;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  15. use OCP\AppFramework\Http\Attribute\UseSession;
  16. use OCP\AppFramework\Http\DataResponse;
  17. use OCP\IL10N;
  18. use OCP\IRequest;
  19. use OCP\ISession;
  20. use OCP\IUserManager;
  21. use OCP\IUserSession;
  22. class SettingsController extends Controller {
  23. /** @var IL10N */
  24. private $l;
  25. /** @var IUserManager */
  26. private $userManager;
  27. /** @var IUserSession */
  28. private $userSession;
  29. /** @var KeyManager */
  30. private $keyManager;
  31. /** @var Crypt */
  32. private $crypt;
  33. /** @var Session */
  34. private $session;
  35. /** @var ISession */
  36. private $ocSession;
  37. /** @var Util */
  38. private $util;
  39. /**
  40. * @param string $AppName
  41. * @param IRequest $request
  42. * @param IL10N $l10n
  43. * @param IUserManager $userManager
  44. * @param IUserSession $userSession
  45. * @param KeyManager $keyManager
  46. * @param Crypt $crypt
  47. * @param Session $session
  48. * @param ISession $ocSession
  49. * @param Util $util
  50. */
  51. public function __construct($AppName,
  52. IRequest $request,
  53. IL10N $l10n,
  54. IUserManager $userManager,
  55. IUserSession $userSession,
  56. KeyManager $keyManager,
  57. Crypt $crypt,
  58. Session $session,
  59. ISession $ocSession,
  60. Util $util,
  61. ) {
  62. parent::__construct($AppName, $request);
  63. $this->l = $l10n;
  64. $this->userSession = $userSession;
  65. $this->userManager = $userManager;
  66. $this->keyManager = $keyManager;
  67. $this->crypt = $crypt;
  68. $this->session = $session;
  69. $this->ocSession = $ocSession;
  70. $this->util = $util;
  71. }
  72. /**
  73. * @param string $oldPassword
  74. * @param string $newPassword
  75. * @return DataResponse
  76. */
  77. #[NoAdminRequired]
  78. #[UseSession]
  79. public function updatePrivateKeyPassword($oldPassword, $newPassword) {
  80. $result = false;
  81. $uid = $this->userSession->getUser()->getUID();
  82. $errorMessage = $this->l->t('Could not update the private key password.');
  83. //check if password is correct
  84. $passwordCorrect = $this->userManager->checkPassword($uid, $newPassword);
  85. if ($passwordCorrect === false) {
  86. // if check with uid fails we need to check the password with the login name
  87. // e.g. in the ldap case. For local user we need to check the password with
  88. // the uid because in this case the login name is case insensitive
  89. $loginName = $this->ocSession->get('loginname');
  90. $passwordCorrect = $this->userManager->checkPassword($loginName, $newPassword);
  91. }
  92. if ($passwordCorrect !== false) {
  93. $encryptedKey = $this->keyManager->getPrivateKey($uid);
  94. $decryptedKey = $this->crypt->decryptPrivateKey($encryptedKey, $oldPassword, $uid);
  95. if ($decryptedKey) {
  96. $encryptedKey = $this->crypt->encryptPrivateKey($decryptedKey, $newPassword, $uid);
  97. $header = $this->crypt->generateHeader();
  98. if ($encryptedKey) {
  99. $this->keyManager->setPrivateKey($uid, $header . $encryptedKey);
  100. $this->session->setPrivateKey($decryptedKey);
  101. $result = true;
  102. }
  103. } else {
  104. $errorMessage = $this->l->t('The old password was not correct, please try again.');
  105. }
  106. } else {
  107. $errorMessage = $this->l->t('The current log-in password was not correct, please try again.');
  108. }
  109. if ($result === true) {
  110. $this->session->setStatus(Session::INIT_SUCCESSFUL);
  111. return new DataResponse(
  112. ['message' => $this->l->t('Private key password successfully updated.')]
  113. );
  114. } else {
  115. return new DataResponse(
  116. ['message' => $errorMessage],
  117. Http::STATUS_BAD_REQUEST
  118. );
  119. }
  120. }
  121. /**
  122. * @param bool $encryptHomeStorage
  123. * @return DataResponse
  124. */
  125. #[UseSession]
  126. public function setEncryptHomeStorage($encryptHomeStorage) {
  127. $this->util->setEncryptHomeStorage($encryptHomeStorage);
  128. return new DataResponse();
  129. }
  130. }