OpenLocalEditorController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files\Controller;
  25. use OCA\Files\Db\OpenLocalEditor;
  26. use OCA\Files\Db\OpenLocalEditorMapper;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCSController;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\DB\Exception;
  33. use OCP\IRequest;
  34. use OCP\Security\ISecureRandom;
  35. use Psr\Log\LoggerInterface;
  36. class OpenLocalEditorController extends OCSController {
  37. public const TOKEN_LENGTH = 128;
  38. public const TOKEN_DURATION = 600; // 10 Minutes
  39. public const TOKEN_RETRIES = 50;
  40. protected ITimeFactory $timeFactory;
  41. protected OpenLocalEditorMapper $mapper;
  42. protected ISecureRandom $secureRandom;
  43. protected LoggerInterface $logger;
  44. protected ?string $userId;
  45. public function __construct(
  46. string $appName,
  47. IRequest $request,
  48. ITimeFactory $timeFactory,
  49. OpenLocalEditorMapper $mapper,
  50. ISecureRandom $secureRandom,
  51. LoggerInterface $logger,
  52. ?string $userId
  53. ) {
  54. parent::__construct($appName, $request);
  55. $this->timeFactory = $timeFactory;
  56. $this->mapper = $mapper;
  57. $this->secureRandom = $secureRandom;
  58. $this->logger = $logger;
  59. $this->userId = $userId;
  60. }
  61. /**
  62. * @NoAdminRequired
  63. * @UserRateThrottle(limit=10, period=120)
  64. */
  65. public function create(string $path): DataResponse {
  66. $pathHash = sha1($path);
  67. $entity = new OpenLocalEditor();
  68. $entity->setUserId($this->userId);
  69. $entity->setPathHash($pathHash);
  70. $entity->setExpirationTime($this->timeFactory->getTime() + self::TOKEN_DURATION); // Expire in 10 minutes
  71. for ($i = 1; $i <= self::TOKEN_RETRIES; $i++) {
  72. $token = $this->secureRandom->generate(self::TOKEN_LENGTH, ISecureRandom::CHAR_ALPHANUMERIC);
  73. $entity->setToken($token);
  74. try {
  75. $this->mapper->insert($entity);
  76. return new DataResponse([
  77. 'userId' => $this->userId,
  78. 'pathHash' => $pathHash,
  79. 'expirationTime' => $entity->getExpirationTime(),
  80. 'token' => $entity->getToken(),
  81. ]);
  82. } catch (Exception $e) {
  83. if ($e->getCode() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
  84. // Only retry on unique constraint violation
  85. throw $e;
  86. }
  87. }
  88. }
  89. $this->logger->error('Giving up after ' . self::TOKEN_RETRIES . ' retries to generate a unique local editor token for path hash: ' . $pathHash);
  90. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  91. }
  92. /**
  93. * @NoAdminRequired
  94. * @BruteForceProtection(action=openLocalEditor)
  95. */
  96. public function validate(string $path, string $token): DataResponse {
  97. $pathHash = sha1($path);
  98. try {
  99. $entity = $this->mapper->verifyToken($this->userId, $pathHash, $token);
  100. } catch (DoesNotExistException $e) {
  101. $response = new DataResponse([], Http::STATUS_NOT_FOUND);
  102. $response->throttle(['userId' => $this->userId, 'pathHash' => $pathHash]);
  103. return $response;
  104. }
  105. $this->mapper->delete($entity);
  106. if ($entity->getExpirationTime() <= $this->timeFactory->getTime()) {
  107. $response = new DataResponse([], Http::STATUS_NOT_FOUND);
  108. $response->throttle(['userId' => $this->userId, 'pathHash' => $pathHash]);
  109. return $response;
  110. }
  111. return new DataResponse([
  112. 'userId' => $this->userId,
  113. 'pathHash' => $pathHash,
  114. 'expirationTime' => $entity->getExpirationTime(),
  115. 'token' => $entity->getToken(),
  116. ]);
  117. }
  118. }