You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB

  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\AnonRateLimit;
  12. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  13. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  14. use OCP\AppFramework\Http\Attribute\OpenAPI;
  15. use OCP\AppFramework\Http\Attribute\PublicPage;
  16. use OCP\AppFramework\Http\JSONResponse;
  17. use OCP\Authentication\Exceptions\InvalidTokenException;
  18. use OCP\IRequest;
  19. #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
  20. class WipeController extends Controller {
  21. public function __construct(
  22. string $appName,
  23. IRequest $request,
  24. private RemoteWipe $remoteWipe,
  25. ) {
  26. parent::__construct($appName, $request);
  27. }
  28. /**
  29. * Check if the device should be wiped
  30. *
  31. * @param string $token App password
  32. *
  33. * @return JSONResponse<Http::STATUS_OK, array{wipe: bool}, array{}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>
  34. *
  35. * 200: Device should be wiped
  36. * 404: Device should not be wiped
  37. */
  38. #[PublicPage]
  39. #[NoCSRFRequired]
  40. #[AnonRateLimit(limit: 10, period: 300)]
  41. #[FrontpageRoute(verb: 'POST', url: '/core/wipe/check')]
  42. public function checkWipe(string $token): JSONResponse {
  43. try {
  44. if ($this->remoteWipe->start($token)) {
  45. return new JSONResponse([
  46. 'wipe' => true
  47. ]);
  48. }
  49. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  50. } catch (InvalidTokenException $e) {
  51. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  52. }
  53. }
  54. /**
  55. * Finish the wipe
  56. *
  57. * @param string $token App password
  58. *
  59. * @return JSONResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, list<empty>, array{}>
  60. *
  61. * 200: Wipe finished successfully
  62. * 404: Device should not be wiped
  63. */
  64. #[PublicPage]
  65. #[NoCSRFRequired]
  66. #[AnonRateLimit(limit: 10, period: 300)]
  67. #[FrontpageRoute(verb: 'POST', url: '/core/wipe/success')]
  68. public function wipeDone(string $token): JSONResponse {
  69. try {
  70. if ($this->remoteWipe->finish($token)) {
  71. return new JSONResponse([]);
  72. }
  73. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  74. } catch (InvalidTokenException $e) {
  75. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  76. }
  77. }
  78. }