RecoveryController.php 6.1 KB

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