RecoveryController.php 6.0 KB

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