changeRecoveryPassword.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Copyright (c) 2013, Bjoern Schiessle <schiessle@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or later.
  5. * See the COPYING-README file.
  6. *
  7. * @brief Script to change recovery key password
  8. *
  9. */
  10. use OCA\Encryption;
  11. \OCP\JSON::checkAdminUser();
  12. \OCP\JSON::checkAppEnabled('files_encryption');
  13. \OCP\JSON::callCheck();
  14. $l = OC_L10N::get('core');
  15. $return = false;
  16. $oldPassword = $_POST['oldPassword'];
  17. $newPassword = $_POST['newPassword'];
  18. $view = new \OC\Files\View('/');
  19. $util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \OCP\User::getUser());
  20. $proxyStatus = \OC_FileProxy::$enabled;
  21. \OC_FileProxy::$enabled = false;
  22. $keyId = $util->getRecoveryKeyId();
  23. $keyPath = '/owncloud_private_key/' . $keyId . '.private.key';
  24. $encryptedRecoveryKey = $view->file_get_contents($keyPath);
  25. $decryptedRecoveryKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedRecoveryKey, $oldPassword);
  26. if ($decryptedRecoveryKey) {
  27. $encryptedRecoveryKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($decryptedRecoveryKey, $newPassword);
  28. $view->file_put_contents($keyPath, $encryptedRecoveryKey);
  29. $return = true;
  30. }
  31. \OC_FileProxy::$enabled = $proxyStatus;
  32. // success or failure
  33. if ($return) {
  34. \OCP\JSON::success(array('data' => array('message' => $l->t('Password successfully changed.'))));
  35. } else {
  36. \OCP\JSON::error(array('data' => array('message' => $l->t('Could not change the password. Maybe the old password was not correct.'))));
  37. }