RecoveryController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Recovery;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\IConfig;
  13. use OCP\IL10N;
  14. use OCP\IRequest;
  15. class RecoveryController extends Controller {
  16. /**
  17. * @var IConfig
  18. */
  19. private $config;
  20. /**
  21. * @var IL10N
  22. */
  23. private $l;
  24. /**
  25. * @var Recovery
  26. */
  27. private $recovery;
  28. /**
  29. * @param string $AppName
  30. * @param IRequest $request
  31. * @param IConfig $config
  32. * @param IL10N $l10n
  33. * @param Recovery $recovery
  34. */
  35. public function __construct($AppName,
  36. IRequest $request,
  37. IConfig $config,
  38. IL10N $l10n,
  39. Recovery $recovery) {
  40. parent::__construct($AppName, $request);
  41. $this->config = $config;
  42. $this->l = $l10n;
  43. $this->recovery = $recovery;
  44. }
  45. /**
  46. * @param string $recoveryPassword
  47. * @param string $confirmPassword
  48. * @param string $adminEnableRecovery
  49. * @return DataResponse
  50. */
  51. public function adminRecovery($recoveryPassword, $confirmPassword, $adminEnableRecovery) {
  52. // Check if both passwords are the same
  53. if (empty($recoveryPassword)) {
  54. $errorMessage = $this->l->t('Missing recovery key password');
  55. return new DataResponse(['data' => ['message' => $errorMessage]],
  56. Http::STATUS_BAD_REQUEST);
  57. }
  58. if (empty($confirmPassword)) {
  59. $errorMessage = $this->l->t('Please repeat the recovery key password');
  60. return new DataResponse(['data' => ['message' => $errorMessage]],
  61. Http::STATUS_BAD_REQUEST);
  62. }
  63. if ($recoveryPassword !== $confirmPassword) {
  64. $errorMessage = $this->l->t('Repeated recovery key password does not match the provided recovery key password');
  65. return new DataResponse(['data' => ['message' => $errorMessage]],
  66. Http::STATUS_BAD_REQUEST);
  67. }
  68. if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') {
  69. if ($this->recovery->enableAdminRecovery($recoveryPassword)) {
  70. return new DataResponse(['data' => ['message' => $this->l->t('Recovery key successfully enabled')]]);
  71. }
  72. return new DataResponse(['data' => ['message' => $this->l->t('Could not enable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST);
  73. } elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') {
  74. if ($this->recovery->disableAdminRecovery($recoveryPassword)) {
  75. return new DataResponse(['data' => ['message' => $this->l->t('Recovery key successfully disabled')]]);
  76. }
  77. return new DataResponse(['data' => ['message' => $this->l->t('Could not disable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST);
  78. }
  79. // this response should never be sent but just in case.
  80. return new DataResponse(['data' => ['message' => $this->l->t('Missing parameters')]], Http::STATUS_BAD_REQUEST);
  81. }
  82. /**
  83. * @param string $newPassword
  84. * @param string $oldPassword
  85. * @param string $confirmPassword
  86. * @return DataResponse
  87. */
  88. public function changeRecoveryPassword($newPassword, $oldPassword, $confirmPassword) {
  89. //check if both passwords are the same
  90. if (empty($oldPassword)) {
  91. $errorMessage = $this->l->t('Please provide the old recovery password');
  92. return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
  93. }
  94. if (empty($newPassword)) {
  95. $errorMessage = $this->l->t('Please provide a new recovery password');
  96. return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
  97. }
  98. if (empty($confirmPassword)) {
  99. $errorMessage = $this->l->t('Please repeat the new recovery password');
  100. return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
  101. }
  102. if ($newPassword !== $confirmPassword) {
  103. $errorMessage = $this->l->t('Repeated recovery key password does not match the provided recovery key password');
  104. return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
  105. }
  106. $result = $this->recovery->changeRecoveryKeyPassword($newPassword,
  107. $oldPassword);
  108. if ($result) {
  109. return new DataResponse(
  110. [
  111. 'data' => [
  112. 'message' => $this->l->t('Password successfully changed.')]
  113. ]
  114. );
  115. }
  116. return new DataResponse(
  117. [
  118. 'data' => [
  119. 'message' => $this->l->t('Could not change the password. Maybe the old password was not correct.')
  120. ]
  121. ], Http::STATUS_BAD_REQUEST);
  122. }
  123. /**
  124. * @NoAdminRequired
  125. *
  126. * @param string $userEnableRecovery
  127. * @return DataResponse
  128. */
  129. public function userSetRecovery($userEnableRecovery) {
  130. if ($userEnableRecovery === '0' || $userEnableRecovery === '1') {
  131. $result = $this->recovery->setRecoveryForUser($userEnableRecovery);
  132. if ($result) {
  133. if ($userEnableRecovery === '0') {
  134. return new DataResponse(
  135. [
  136. 'data' => [
  137. 'message' => $this->l->t('Recovery Key disabled')]
  138. ]
  139. );
  140. }
  141. return new DataResponse(
  142. [
  143. 'data' => [
  144. 'message' => $this->l->t('Recovery Key enabled')]
  145. ]
  146. );
  147. }
  148. }
  149. return new DataResponse(
  150. [
  151. 'data' => [
  152. 'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')
  153. ]
  154. ], Http::STATUS_BAD_REQUEST);
  155. }
  156. }