SettingsController.php 3.4 KB

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