Skip to content

Commit

Permalink
Temp: Auth handling stuff
Browse files Browse the repository at this point in the history
Signed-off-by: codewithvk <vivek.javiya@collabora.com>
  • Loading branch information
codewithvk committed Jan 7, 2025
1 parent 7e840f9 commit 7fac5ca
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 18 deletions.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
['name' => 'settings#checkSettings', 'url' => 'settings/check', 'verb' => 'GET'],
['name' => 'settings#demoServers', 'url' => 'settings/demo', 'verb' => 'GET'],
['name' => 'settings#getFontNames', 'url' => 'settings/fonts', 'verb' => 'GET'],
// We want to create new routes like this to store files...
['name' => 'settings#getJsonFontList', 'url' => 'settings/fonts.json', 'verb' => 'GET'],
['name' => 'settings#getFontFile', 'url' => 'settings/fonts/{name}', 'verb' => 'GET'],
['name' => 'settings#getFontFileOverview', 'url' => 'settings/fonts/{name}/overview', 'verb' => 'GET'],
Expand Down
27 changes: 13 additions & 14 deletions lib/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,24 +384,23 @@ public function editOnlineTarget(int $fileId, ?string $target = null): RedirectR
#[PublicPage]
public function token(int $fileId, ?string $shareToken = null, ?string $path = null, ?string $guestName = null): DataResponse {
try {
// if ($fileId === -1 && $path !== null && str_starts_with($path, 'admin-settings/')) {
// $parts = explode('/', $path);
// $adminUserId = $parts[1] ?? $this->userId; // fallback if needed
if ($fileId === -1 && $path !== null && str_starts_with($path, 'admin-settings/')) {
$parts = explode('/', $path);
$adminUserId = $parts[1] ?? $this->userId; // fallback if needed

// $docKey = $fileId . '_' . $this->config->getSystemValue('instanceid');
$docKey = $fileId . '_' . $this->config->getSystemValue('instanceid');

// $wopi = $this->tokenManager->generateWopiToken($fileId, null, $adminUserId);
$wopi = $this->tokenManager->generateWopiToken($fileId, null, $adminUserId);

// $coolBaseUrl = $this->appConfig->getCollaboraUrlPublic();
// $adminSettingsWopiSrc = $coolBaseUrl . '/browser/admin-settings.html?'
// . 'WOPISrc=' . urlencode($this->urlGenerator->getAbsoluteURL('/index.php/apps/richdocuments/wopi/admin-settings'));
$coolBaseUrl = $this->appConfig->getCollaboraUrlPublic();
$adminSettingsWopiSrc = $coolBaseUrl . '/browser/admin-settings.html?';

// return new DataResponse([
// 'urlSrc' => $adminSettingsWopiSrc,
// 'token' => $wopi->getToken(),
// 'token_ttl' => $wopi->getExpiry(),
// ]);
// }
return new DataResponse([
'urlSrc' => $adminSettingsWopiSrc,
'token' => $wopi->getToken(),
'token_ttl' => $wopi->getExpiry(),
]);
}

// Normal file handling (unchanged)
$share = $shareToken ? $this->shareManager->getShareByToken($shareToken) : null;
Expand Down
13 changes: 12 additions & 1 deletion lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,20 @@ public function __construct(
#[FrontpageRoute(verb: 'GET', url: 'wopi/files/{fileId}')]
public function checkFileInfo(string $fileId, string $access_token): JSONResponse {
try {
$wopi = $this->wopiMapper->getWopiForToken($access_token);

// TODO: condition for $wopi not found?
if ($fileId == "-1" && $wopi->getTokenType() == WOPI::TOKEN_TYPE_SETTING_AUTH) {
$response = [
"usersettings" => 'DONE',
];

return new JSONResponse($response);
}

[$fileId, , $version] = Helper::parseFileId($fileId);

$wopi = $this->wopiMapper->getWopiForToken($access_token);
$file = $this->getFileForWopiToken($wopi);
if (!($file instanceof File)) {
throw new NotFoundException('No valid file found for ' . $fileId);
Expand Down
5 changes: 5 additions & 0 deletions lib/Db/Wopi.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class Wopi extends Entity implements \JsonSerializable {
*/
public const TOKEN_TYPE_INITIATOR = 4;

/*
* Temporary token that is used for authentication while communication between cool iframe and user/admin settings
*/
public const TOKEN_TYPE_SETTING_AUTH = 5;

/** @var string */
protected $ownerUid;

Expand Down
28 changes: 28 additions & 0 deletions lib/Db/WopiMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ public function generateFileToken($fileId, $owner, $editor, $version, $updatable
return $wopi;
}

public function generateUserSettingsToken($fileId, $owner, $editor, $version, $updatable, $serverHost, ?string $guestDisplayname = null, $hideDownload = false, $direct = false, $templateId = 0, $share = null) {
$token = $this->random->generate(32, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);

$wopi = Wopi::fromParams([
'fileid' => $fileId,
'ownerUid' => $owner,
'editorUid' => $editor,
'version' => $version,
'canwrite' => $updatable,
'serverHost' => $serverHost,
'token' => $token,
'expiry' => $this->calculateNewTokenExpiry(),
'guestDisplayname' => $guestDisplayname,
'hideDownload' => $hideDownload,
'direct' => $direct,
'templateId' => $templateId,
'remoteServer' => '',
'remoteServerToken' => '',
'share' => $share,
'tokenType' => Wopi::TOKEN_TYPE_SETTING_AUTH
]);

/** @var Wopi $wopi */
$wopi = $this->insert($wopi);

return $wopi;
}

public function generateInitiatorToken($uid, $remoteServer) {
$token = $this->random->generate(32, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);

Expand Down
11 changes: 10 additions & 1 deletion lib/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ public function __construct(
* @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;
$rootFolder = $this->rootFolder;

if ($fileId == "-1")
{
$editoruid = $this->userId;
$serverHost = $this->urlGenerator->getAbsoluteURL('/');
return $this->wopiMapper->generateUserSettingsToken($fileId, $owneruid, $editoruid, 0, true, $serverHost, "", $hideDownload, $direct, 0, $shareToken);
}

[$fileId, , $version] = Helper::parseFileId($fileId);

// // // Parse docKey to extract fileId
// // // Usually docKey is something like "<fileId>_<instanceid>[_version]"
// $parts = explode('_', $docKey);
Expand Down
5 changes: 4 additions & 1 deletion src/components/AdminSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
<!-- New Collabora Admin Settings Section -->
<div id="admin-cool-frame-section" class="section">
<h2>{{ t('richdocuments', 'Collabora Admin Settings') }}</h2>
<CoolFrame :endpoint="'/cool/admin-settings'"
<CoolFrame v-if="tokenGenerated"
:endpoint="'/cool/admin-settings'"
:public-wopi-url="settings.public_wopi_url"
:access-token="accessToken"
:access-token-t-t-l="accessTokenTTL" />
Expand Down Expand Up @@ -517,6 +518,7 @@ export default {
accessToken: '',
accessTokenTTL: '',
userId: '',
tokenGenerated: false,
}
},
computed: {
Expand Down Expand Up @@ -630,6 +632,7 @@ export default {
if (data.token) {
this.accessToken = data.token
this.accessTokenTTL = data.token_ttl
this.tokenGenerated = true
console.debug('Admin settings WOPI token generated:', this.accessToken, this.accessTokenTTL)
} else if (data.federatedUrl) {
console.error('Federated URL returned, not expected for admin settings.')
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const getNextcloudUrl = () => {

export const getCoolServerUrl = (collaboraBaseUrl) => {
// todo fix wopi Url
const wopiurl = getCallbackBaseUrl() + '/index.php/apps/richdocuments/wopi/admin-settings'
const wopiurl = getCallbackBaseUrl() + '/index.php/apps/richdocuments/wopi/files/-1'

const AdminSettingsUrl = collaboraBaseUrl + '/browser/dist/admin-settings.html?'

Expand Down

0 comments on commit 7fac5ca

Please sign in to comment.