forked from nextcloud/richdocuments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenManager.php
More file actions
284 lines (245 loc) Β· 8.9 KB
/
Copy pathTokenManager.php
File metadata and controls
284 lines (245 loc) Β· 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Richdocuments;
use Exception;
use OCA\Richdocuments\Db\Direct;
use OCA\Richdocuments\Db\Wopi;
use OCA\Richdocuments\Db\WopiMapper;
use OCA\Richdocuments\WOPI\Parser;
use OCP\Constants;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\Node\BeforeNodeReadEvent;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Util;
use Psr\Log\LoggerInterface;
class TokenManager {
public function __construct(
private IRootFolder $rootFolder,
private IManager $shareManager,
private IURLGenerator $urlGenerator,
private Parser $wopiParser,
private ?string $userId,
private WopiMapper $wopiMapper,
private IL10N $trans,
private Helper $helper,
private PermissionManager $permissionManager,
private IEventDispatcher $eventDispatcher,
private LoggerInterface $logger,
) {
}
/**
* @throws Exception
*/
public function generateWopiToken(string $fileId, ?string $shareToken = null, ?string $editoruid = null, bool $direct = false): Wopi {
[$fileId, , $version] = Helper::parseFileId($fileId);
$owneruid = null;
$hideDownload = false;
// if the user is not logged-in do use the sharers storage
if ($shareToken !== null) {
/** @var File $file */
$share = $this->shareManager->getShareByToken($shareToken);
if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
throw new ShareNotFound();
}
$owneruid = $share->getShareOwner();
$updatable = (bool)($share->getPermissions() & \OCP\Constants::PERMISSION_UPDATE);
$updatable = $updatable && $this->permissionManager->userCanEdit($owneruid);
$hideDownload = $share->getHideDownload();
$userFolder = $this->rootFolder->getUserFolder($owneruid);
} else {
$editoruid = $this->userId ?? $editoruid;
$userFolder = $this->rootFolder->getUserFolder($editoruid);
$files = $userFolder->getById((int)$fileId);
$updatable = false;
foreach ($files as $file) {
if ($file->isUpdateable()) {
$updatable = true;
break;
}
}
$updatable = $updatable && $this->permissionManager->userCanEdit($editoruid);
// disable download if at least one shared access has it disabled
foreach ($files as $file) {
$share = $this->helper->getShareFromNode($file);
$attributes = $share?->getAttributes();
if ($attributes !== null
&& $attributes->getAttribute('permissions', 'download') === false
) {
$hideDownload = true;
break;
}
}
}
/** @var File $file */
$file = $userFolder->getFirstNodeById($fileId);
// Check node readability (for storage wrapper overwrites like terms of services)
if ($file === null || !$file->isReadable()) {
throw new NotPermittedException();
}
// If its a public share, use the owner from the share, otherwise check the file object
if (is_null($owneruid)) {
$owner = $file->getOwner();
if (is_null($owner)) {
// Editor UID instead of owner UID in case owner is null e.g. group folders
$owneruid = $editoruid;
} else {
$owneruid = $owner->getUID();
}
}
// Safeguard that users without required group permissions cannot create a token
if (!$this->permissionManager->isEnabledForUser($owneruid) && !$this->permissionManager->isEnabledForUser($editoruid)) {
throw new NotPermittedException();
}
// force read operation to trigger possible audit logging
$this->eventDispatcher->dispatchTyped(new BeforeNodeReadEvent($file));
$serverHost = $this->urlGenerator->getAbsoluteURL('/');
$guestName = $editoruid === null ? $this->prepareGuestName($this->helper->getGuestNameFromCookie()) : null;
return $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, $updatable, $serverHost, $guestName, $hideDownload, $direct, 0, $shareToken);
}
/**
* This method is receiving the results from the TOKEN_TYPE_FEDERATION generated on the opener server
* that is created in {@link newInitiatorToken}
*/
public function upgradeToRemoteToken(Wopi $wopi, Wopi $remoteWopi, string $shareToken, string $remoteServer, string $remoteServerToken): Wopi {
if ($remoteWopi->getTokenType() !== Wopi::TOKEN_TYPE_INITIATOR) {
return $wopi;
}
$remoteTokenType = $remoteWopi->getEditorUid() !== null ? Wopi::TOKEN_TYPE_REMOTE_USER : Wopi::TOKEN_TYPE_REMOTE_GUEST;
$wopi->setTokenType($remoteTokenType);
$wopi->setGuestDisplayname(
$remoteTokenType === Wopi::TOKEN_TYPE_REMOTE_USER ?
$remoteWopi->getEditorUid() . '@' . $remoteServer :
$remoteWopi->getGuestDisplayname()
);
$wopi->setShare($shareToken);
$wopi->setCanwrite($wopi->getCanwrite() && $remoteWopi->getCanwrite());
$wopi->setHideDownload($wopi->getHideDownload() || $remoteWopi->getHideDownload());
$wopi->setRemoteServer($remoteServer);
$wopi->setRemoteServerToken($remoteServerToken);
$this->wopiMapper->update($wopi);
return $wopi;
}
public function upgradeFromDirectInitiator(Direct $direct, Wopi $wopi) {
$wopi->setTokenType(Wopi::TOKEN_TYPE_REMOTE_GUEST);
$wopi->setEditorUid(null);
$wopi->setRemoteServer($direct->getInitiatorHost());
$wopi->setRemoteServerToken($direct->getInitiatorToken());
$this->wopiMapper->update($wopi);
return $wopi;
}
public function generateWopiTokenForTemplate(
File $templateFile,
int $targetFileId,
string $owneruid,
bool $isGuest,
bool $direct = false,
?int $sharePermissions = null,
?string $editoruid = null,
): Wopi {
if (empty($editoruid)) {
$editoruid = $isGuest ? null : $owneruid;
}
$rootFolder = $this->rootFolder->getUserFolder($owneruid);
$targetFile = $rootFolder->getFirstNodeById($targetFileId);
if (!$targetFile instanceof File) {
throw new NotFoundException();
}
// Check node readability (for storage wrapper overwrites like terms of services)
if (!$targetFile->isReadable()) {
throw new NotPermittedException();
}
$updatable = $targetFile->isUpdateable();
if (!is_null($sharePermissions)) {
$shareUpdatable = (bool)($sharePermissions & \OCP\Constants::PERMISSION_UPDATE);
$updatable = $updatable && $shareUpdatable;
}
$updatable = $updatable && $this->permissionManager->userCanEdit($owneruid);
$serverHost = $this->urlGenerator->getAbsoluteURL('/');
return $this->wopiMapper->generateFileToken(
$targetFile->getId(),
$owneruid,
$editoruid,
0,
$updatable,
$serverHost,
$isGuest ? '' : null,
false,
$direct,
$templateFile->getId()
);
}
public function newInitiatorToken($sourceServer, ?Node $node = null, $shareToken = null, bool $direct = false, $userId = null): Wopi {
if ($node !== null) {
$wopi = $this->generateWopiToken((string)$node->getId(), $shareToken, $userId, $direct);
$wopi->setServerHost($sourceServer);
$wopi->setTokenType(Wopi::TOKEN_TYPE_INITIATOR);
$this->wopiMapper->update($wopi);
return $wopi;
}
return $this->wopiMapper->generateInitiatorToken($this->userId, $sourceServer);
}
public function extendWithInitiatorUserToken(Wopi $wopi, string $initiatorUserHost, string $initiatorUserToken): Wopi {
$wopi->setRemoteServer($initiatorUserHost);
$wopi->setRemoteServerToken($initiatorUserToken);
$this->wopiMapper->update($wopi);
return $wopi;
}
public function prepareGuestName(?string $guestName = null) {
if (empty($guestName)) {
return $this->trans->t('Anonymous guest');
}
$guestName = $this->trans->t('%s (Guest)', Util::sanitizeHTML($guestName));
$cut = 56;
while (mb_strlen($guestName) >= 64) {
$guestName = $this->trans->t('%s (Guest)', Util::sanitizeHTML(
mb_substr($guestName, 0, $cut)
));
$cut -= 5;
}
return $guestName;
}
/**
* @param string $accessToken
* @param string $guestName
* @return void
* @throws Exceptions\ExpiredTokenException
* @throws Exceptions\UnknownTokenException
*/
public function updateGuestName(string $accessToken, string $guestName) {
$wopi = $this->wopiMapper->getWopiForToken($accessToken);
$wopi->setGuestDisplayname($this->prepareGuestName($guestName));
$this->wopiMapper->update($wopi);
}
public function setShareToken(Wopi $wopi, ?string $shareToken): Wopi {
$wopi->setShare($shareToken);
return $this->wopiMapper->update($wopi);
}
public function setGuestName(Wopi $wopi, ?string $guestName = null): Wopi {
if ($wopi->getTokenType() !== Wopi::TOKEN_TYPE_GUEST && $wopi->getTokenType() !== Wopi::TOKEN_TYPE_REMOTE_GUEST) {
return $wopi;
}
$wopi->setGuestDisplayname($this->prepareGuestName($guestName));
return $this->wopiMapper->update($wopi);
}
public function getUrlSrc(File $file): string {
return $this->wopiParser->getUrlSrcValue($file->getMimeType());
}
public function getUrlSrcForMimeType(string $type): ?string {
try {
return $this->wopiParser->getUrlSrcValue($type);
} catch (\Throwable $e) {
return null;
}
}
}