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