RecoveryController.php 6.1 KB

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