RecoveryController.php 5.1 KB

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