RecoveryController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Clark Tomlinson <fallen013@gmail.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Encryption\Controller;
  28. use OCA\Encryption\Recovery;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\IConfig;
  33. use OCP\IL10N;
  34. use OCP\IRequest;
  35. class RecoveryController extends Controller {
  36. /**
  37. * @var IConfig
  38. */
  39. private $config;
  40. /**
  41. * @var IL10N
  42. */
  43. private $l;
  44. /**
  45. * @var Recovery
  46. */
  47. private $recovery;
  48. /**
  49. * @param string $AppName
  50. * @param IRequest $request
  51. * @param IConfig $config
  52. * @param IL10N $l10n
  53. * @param Recovery $recovery
  54. */
  55. public function __construct($AppName,
  56. IRequest $request,
  57. IConfig $config,
  58. IL10N $l10n,
  59. Recovery $recovery) {
  60. parent::__construct($AppName, $request);
  61. $this->config = $config;
  62. $this->l = $l10n;
  63. $this->recovery = $recovery;
  64. }
  65. /**
  66. * @param string $recoveryPassword
  67. * @param string $confirmPassword
  68. * @param string $adminEnableRecovery
  69. * @return DataResponse
  70. */
  71. public function adminRecovery($recoveryPassword, $confirmPassword, $adminEnableRecovery) {
  72. // Check if both passwords are the same
  73. if (empty($recoveryPassword)) {
  74. $errorMessage = $this->l->t('Missing recovery key password');
  75. return new DataResponse(['data' => ['message' => $errorMessage]],
  76. Http::STATUS_BAD_REQUEST);
  77. }
  78. if (empty($confirmPassword)) {
  79. $errorMessage = $this->l->t('Please repeat the recovery key password');
  80. return new DataResponse(['data' => ['message' => $errorMessage]],
  81. Http::STATUS_BAD_REQUEST);
  82. }
  83. if ($recoveryPassword !== $confirmPassword) {
  84. $errorMessage = $this->l->t('Repeated recovery key password does not match the provided recovery key password');
  85. return new DataResponse(['data' => ['message' => $errorMessage]],
  86. Http::STATUS_BAD_REQUEST);
  87. }
  88. if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') {
  89. if ($this->recovery->enableAdminRecovery($recoveryPassword)) {
  90. return new DataResponse(['data' => ['message' => $this->l->t('Recovery key successfully enabled')]]);
  91. }
  92. return new DataResponse(['data' => ['message' => $this->l->t('Could not enable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST);
  93. } elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') {
  94. if ($this->recovery->disableAdminRecovery($recoveryPassword)) {
  95. return new DataResponse(['data' => ['message' => $this->l->t('Recovery key successfully disabled')]]);
  96. }
  97. return new DataResponse(['data' => ['message' => $this->l->t('Could not disable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST);
  98. }
  99. // this response should never be sent but just in case.
  100. return new DataResponse(['data' => ['message' => $this->l->t('Missing parameters')]], Http::STATUS_BAD_REQUEST);
  101. }
  102. /**
  103. * @param string $newPassword
  104. * @param string $oldPassword
  105. * @param string $confirmPassword
  106. * @return DataResponse
  107. */
  108. public function changeRecoveryPassword($newPassword, $oldPassword, $confirmPassword) {
  109. //check if both passwords are the same
  110. if (empty($oldPassword)) {
  111. $errorMessage = $this->l->t('Please provide the old recovery password');
  112. return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
  113. }
  114. if (empty($newPassword)) {
  115. $errorMessage = $this->l->t('Please provide a new recovery password');
  116. return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
  117. }
  118. if (empty($confirmPassword)) {
  119. $errorMessage = $this->l->t('Please repeat the new recovery password');
  120. return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
  121. }
  122. if ($newPassword !== $confirmPassword) {
  123. $errorMessage = $this->l->t('Repeated recovery key password does not match the provided recovery key password');
  124. return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
  125. }
  126. $result = $this->recovery->changeRecoveryKeyPassword($newPassword,
  127. $oldPassword);
  128. if ($result) {
  129. return new DataResponse(
  130. [
  131. 'data' => [
  132. 'message' => $this->l->t('Password successfully changed.')]
  133. ]
  134. );
  135. }
  136. return new DataResponse(
  137. [
  138. 'data' => [
  139. 'message' => $this->l->t('Could not change the password. Maybe the old password was not correct.')
  140. ]
  141. ], Http::STATUS_BAD_REQUEST);
  142. }
  143. /**
  144. * @NoAdminRequired
  145. *
  146. * @param string $userEnableRecovery
  147. * @return DataResponse
  148. */
  149. public function userSetRecovery($userEnableRecovery) {
  150. if ($userEnableRecovery === '0' || $userEnableRecovery === '1') {
  151. $result = $this->recovery->setRecoveryForUser($userEnableRecovery);
  152. if ($result) {
  153. if ($userEnableRecovery === '0') {
  154. return new DataResponse(
  155. [
  156. 'data' => [
  157. 'message' => $this->l->t('Recovery Key disabled')]
  158. ]
  159. );
  160. }
  161. return new DataResponse(
  162. [
  163. 'data' => [
  164. 'message' => $this->l->t('Recovery Key enabled')]
  165. ]
  166. );
  167. }
  168. }
  169. return new DataResponse(
  170. [
  171. 'data' => [
  172. 'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')
  173. ]
  174. ], Http::STATUS_BAD_REQUEST);
  175. }
  176. }