SettingsController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Encryption\Controller;
  25. use OCA\Encryption\Crypto\Crypt;
  26. use OCA\Encryption\KeyManager;
  27. use OCA\Encryption\Session;
  28. use OCA\Encryption\Util;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\IL10N;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\IUserManager;
  36. use OCP\IUserSession;
  37. class SettingsController extends Controller {
  38. /** @var IL10N */
  39. private $l;
  40. /** @var IUserManager */
  41. private $userManager;
  42. /** @var IUserSession */
  43. private $userSession;
  44. /** @var KeyManager */
  45. private $keyManager;
  46. /** @var Crypt */
  47. private $crypt;
  48. /** @var Session */
  49. private $session;
  50. /** @var ISession */
  51. private $ocSession;
  52. /** @var Util */
  53. private $util;
  54. /**
  55. * @param string $AppName
  56. * @param IRequest $request
  57. * @param IL10N $l10n
  58. * @param IUserManager $userManager
  59. * @param IUserSession $userSession
  60. * @param KeyManager $keyManager
  61. * @param Crypt $crypt
  62. * @param Session $session
  63. * @param ISession $ocSession
  64. * @param Util $util
  65. */
  66. public function __construct($AppName,
  67. IRequest $request,
  68. IL10N $l10n,
  69. IUserManager $userManager,
  70. IUserSession $userSession,
  71. KeyManager $keyManager,
  72. Crypt $crypt,
  73. Session $session,
  74. ISession $ocSession,
  75. Util $util
  76. ) {
  77. parent::__construct($AppName, $request);
  78. $this->l = $l10n;
  79. $this->userSession = $userSession;
  80. $this->userManager = $userManager;
  81. $this->keyManager = $keyManager;
  82. $this->crypt = $crypt;
  83. $this->session = $session;
  84. $this->ocSession = $ocSession;
  85. $this->util = $util;
  86. }
  87. /**
  88. * @NoAdminRequired
  89. * @UseSession
  90. *
  91. * @param string $oldPassword
  92. * @param string $newPassword
  93. * @return DataResponse
  94. */
  95. public function updatePrivateKeyPassword($oldPassword, $newPassword) {
  96. $result = false;
  97. $uid = $this->userSession->getUser()->getUID();
  98. $errorMessage = $this->l->t('Could not update the private key password.');
  99. //check if password is correct
  100. $passwordCorrect = $this->userManager->checkPassword($uid, $newPassword);
  101. if ($passwordCorrect === false) {
  102. // if check with uid fails we need to check the password with the login name
  103. // e.g. in the ldap case. For local user we need to check the password with
  104. // the uid because in this case the login name is case insensitive
  105. $loginName = $this->ocSession->get('loginname');
  106. $passwordCorrect = $this->userManager->checkPassword($loginName, $newPassword);
  107. }
  108. if ($passwordCorrect !== false) {
  109. $encryptedKey = $this->keyManager->getPrivateKey($uid);
  110. $decryptedKey = $this->crypt->decryptPrivateKey($encryptedKey, $oldPassword, $uid);
  111. if ($decryptedKey) {
  112. $encryptedKey = $this->crypt->encryptPrivateKey($decryptedKey, $newPassword, $uid);
  113. $header = $this->crypt->generateHeader();
  114. if ($encryptedKey) {
  115. $this->keyManager->setPrivateKey($uid, $header . $encryptedKey);
  116. $this->session->setPrivateKey($decryptedKey);
  117. $result = true;
  118. }
  119. } else {
  120. $errorMessage = $this->l->t('The old password was not correct, please try again.');
  121. }
  122. } else {
  123. $errorMessage = $this->l->t('The current log-in password was not correct, please try again.');
  124. }
  125. if ($result === true) {
  126. $this->session->setStatus(Session::INIT_SUCCESSFUL);
  127. return new DataResponse(
  128. ['message' => $this->l->t('Private key password successfully updated.')]
  129. );
  130. } else {
  131. return new DataResponse(
  132. ['message' => $errorMessage],
  133. Http::STATUS_BAD_REQUEST
  134. );
  135. }
  136. }
  137. /**
  138. * @UseSession
  139. *
  140. * @param bool $encryptHomeStorage
  141. * @return DataResponse
  142. */
  143. public function setEncryptHomeStorage($encryptHomeStorage) {
  144. $this->util->setEncryptHomeStorage($encryptHomeStorage);
  145. return new DataResponse();
  146. }
  147. }