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