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