1
0

WipeController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Authentication\Token\RemoteWipe;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  12. use OCP\AppFramework\Http\JSONResponse;
  13. use OCP\Authentication\Exceptions\InvalidTokenException;
  14. use OCP\IRequest;
  15. class WipeController extends Controller {
  16. public function __construct(
  17. string $appName,
  18. IRequest $request,
  19. private RemoteWipe $remoteWipe,
  20. ) {
  21. parent::__construct($appName, $request);
  22. }
  23. /**
  24. * @NoAdminRequired
  25. * @NoCSRFRequired
  26. * @PublicPage
  27. *
  28. * @AnonRateThrottle(limit=10, period=300)
  29. *
  30. * Check if the device should be wiped
  31. *
  32. * @param string $token App password
  33. *
  34. * @return JSONResponse<Http::STATUS_OK, array{wipe: bool}, array{}>|JSONResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  35. *
  36. * 200: Device should be wiped
  37. * 404: Device should not be wiped
  38. */
  39. #[FrontpageRoute(verb: 'POST', url: '/core/wipe/check')]
  40. public function checkWipe(string $token): JSONResponse {
  41. try {
  42. if ($this->remoteWipe->start($token)) {
  43. return new JSONResponse([
  44. 'wipe' => true
  45. ]);
  46. }
  47. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  48. } catch (InvalidTokenException $e) {
  49. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  50. }
  51. }
  52. /**
  53. * @NoAdminRequired
  54. * @NoCSRFRequired
  55. * @PublicPage
  56. *
  57. * @AnonRateThrottle(limit=10, period=300)
  58. *
  59. * Finish the wipe
  60. *
  61. * @param string $token App password
  62. *
  63. * @return JSONResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  64. *
  65. * 200: Wipe finished successfully
  66. * 404: Device should not be wiped
  67. */
  68. #[FrontpageRoute(verb: 'POST', url: '/core/wipe/success')]
  69. public function wipeDone(string $token): JSONResponse {
  70. try {
  71. if ($this->remoteWipe->finish($token)) {
  72. return new JSONResponse([]);
  73. }
  74. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  75. } catch (InvalidTokenException $e) {
  76. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  77. }
  78. }
  79. }