1
0

WipeController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Controller;
  26. use OC\Authentication\Token\RemoteWipe;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\JSONResponse;
  30. use OCP\Authentication\Exceptions\InvalidTokenException;
  31. use OCP\IRequest;
  32. class WipeController extends Controller {
  33. public function __construct(
  34. string $appName,
  35. IRequest $request,
  36. private RemoteWipe $remoteWipe,
  37. ) {
  38. parent::__construct($appName, $request);
  39. }
  40. /**
  41. * @NoAdminRequired
  42. * @NoCSRFRequired
  43. * @PublicPage
  44. *
  45. * @AnonRateThrottle(limit=10, period=300)
  46. *
  47. * Check if the device should be wiped
  48. *
  49. * @param string $token App password
  50. *
  51. * @return JSONResponse<Http::STATUS_OK, array{wipe: bool}, array{}>|JSONResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  52. *
  53. * 200: Device should be wiped
  54. * 404: Device should not be wiped
  55. */
  56. public function checkWipe(string $token): JSONResponse {
  57. try {
  58. if ($this->remoteWipe->start($token)) {
  59. return new JSONResponse([
  60. 'wipe' => true
  61. ]);
  62. }
  63. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  64. } catch (InvalidTokenException $e) {
  65. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  66. }
  67. }
  68. /**
  69. * @NoAdminRequired
  70. * @NoCSRFRequired
  71. * @PublicPage
  72. *
  73. * @AnonRateThrottle(limit=10, period=300)
  74. *
  75. * Finish the wipe
  76. *
  77. * @param string $token App password
  78. *
  79. * @return JSONResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  80. *
  81. * 200: Wipe finished successfully
  82. * 404: Device should not be wiped
  83. */
  84. public function wipeDone(string $token): JSONResponse {
  85. try {
  86. if ($this->remoteWipe->finish($token)) {
  87. return new JSONResponse([]);
  88. }
  89. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  90. } catch (InvalidTokenException $e) {
  91. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  92. }
  93. }
  94. }