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