Skip to content

Commit

Permalink
Adapt filedrop to v2 routes
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Aug 31, 2023
1 parent 12c82d4 commit 6c118a5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions js/end_to_end_encryption-filedrop.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/end_to_end_encryption-filedrop.js.map

Large diffs are not rendered by default.

34 changes: 12 additions & 22 deletions lib/E2EEPublicShareTemplateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,16 @@
use Psr\Log\LoggerInterface;

class E2EEPublicShareTemplateProvider implements IPublicShareTemplateProvider {
private IUserManager $userManager;
private IUrlGenerator $urlGenerator;
private IL10N $l10n;
private Defaults $defaults;
private IInitialState $initialState;
private IKeyStorage $keyStorage;
private LoggerInterface $logger;

public function __construct(
IUserManager $userManager,
IUrlGenerator $urlGenerator,
IL10N $l10n,
Defaults $defaults,
IInitialState $initialState,
IKeyStorage $keyStorage,
LoggerInterface $logger
private IUserManager $userManager,
private IUrlGenerator $urlGenerator,
private IL10N $l10n,
private Defaults $defaults,
private IInitialState $initialState,
private IKeyStorage $keyStorage,
private LoggerInterface $logger,
private MetaDataStorage $metadataStorage,
) {
$this->userManager = $userManager;
$this->urlGenerator = $urlGenerator;
$this->l10n = $l10n;
$this->defaults = $defaults;
$this->initialState = $initialState;
$this->keyStorage = $keyStorage;
$this->logger = $logger;
}

public function shouldRespond(IShare $share): bool {
Expand All @@ -61,10 +47,14 @@ public function renderPage(IShare $share, string $token, string $path): Template
return new TemplateResponse(Application::APP_ID, 'error');
}

$metadata = json_decode($this->metadataStorage->getMetaData($owner->getUID(), $shareNode->getId()), true);

$this->initialState->provideInitialState('publicKey', $publicKey);
$this->initialState->provideInitialState('fileId', $shareNode->getId());
$this->initialState->provideInitialState('token', $token);
$this->initialState->provideInitialState('fileName', $shareNode->getName());
$this->initialState->provideInitialState('encryptionVersion', $metadata['version']);
$this->initialState->provideInitialState('counter', $this->metadataStorage->getCounter($shareNode->getId()));

// OpenGraph Support: http://ogp.me/
Util::addHeader('meta', ['property' => "og:title", 'content' => $this->l10n->t("Encrypted share")]);
Expand Down
9 changes: 6 additions & 3 deletions src/services/filedrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,17 @@ export async function getFileDropEntry(file, tag, publicKey) {
}

/**
* @param {1|2} encryptionVersion - The encrypted version for the folder
* @param {string} folderId
* @param {EncryptedFileMetadata[]} fileDrop
* @param {string} lockToken
* @param {string} shareToken
*/
export async function uploadFileDrop(folderId, fileDrop, lockToken, shareToken) {
export async function uploadFileDrop(encryptionVersion, folderId, fileDrop, lockToken, shareToken) {
const ocsUrl = generateOcsUrl(
'apps/end_to_end_encryption/api/v1/meta-data/{folderId}',
'apps/end_to_end_encryption/api/v{encryptionVersion}/meta-data/{folderId}',
{
encryptionVersion,
folderId,
}
)
Expand All @@ -103,10 +105,11 @@ export async function uploadFileDrop(folderId, fileDrop, lockToken, shareToken)
{
headers: {
'x-e2ee-supported': true,
'e2e-token': lockToken,
...(encryptionVersion === 2 ? { 'e2e-token': lockToken } : {}),
},
params: {
shareToken,
...(encryptionVersion === 1 ? { 'e2e-token': lockToken } : {}),
},
},
)
Expand Down
12 changes: 8 additions & 4 deletions src/services/lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import { generateOcsUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'

/**
* @param {1|2} encryptionVersion - The encrypted version for the folder
* @param {number} counter - The metadata counter received from the initial state
* @param {string} fileId - The file id to lock
* @param {?string} shareToken - The optional share token if this is a file drop.
* @return {Promise<string>} lockToken
*/
export async function lock(fileId, shareToken) {
export async function lock(encryptionVersion, counter, fileId, shareToken) {
const { data: { ocs: { meta, data } } } = await axios.post(
generateOcsUrl('apps/end_to_end_encryption/api/v1/lock/{fileId}', { fileId }),
generateOcsUrl('apps/end_to_end_encryption/api/v{encryptionVersion}/lock/{fileId}', { encryptionVersion, fileId }),
undefined,
{
headers: {
'x-e2ee-supported': true,
'x-nc-e2ee-counter': counter,
},
params: {
shareToken,
Expand All @@ -31,13 +34,14 @@ export async function lock(fileId, shareToken) {
}

/**
* @param {1|2} encryptionVersion - The encrypted version for the folder
* @param {string} fileId - The file id to lock
* @param {string} lockToken - The optional lock token if the folder was already locked.
* @param {?string} shareToken - The optional share token if this is a file drop.
*/
export async function unlock(fileId, lockToken, shareToken) {
export async function unlock(encryptionVersion, fileId, lockToken, shareToken) {
const { data: { ocs: { meta } } } = await axios.delete(
generateOcsUrl('apps/end_to_end_encryption/api/v1/lock/{fileId}', { fileId }),
generateOcsUrl('apps/end_to_end_encryption/api/v{encryptionVersion}/lock/{fileId}', { encryptionVersion, fileId }),
{
headers: {
'x-e2ee-supported': true,
Expand Down
11 changes: 8 additions & 3 deletions src/views/FileDrop.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export default {
publicKey: loadState('end_to_end_encryption', 'publicKey'),
/** @type {string} */
fileName: loadState('end_to_end_encryption', 'fileName'),
/** @type {"v1"|"v2"} */
encryptionVersion: Number.parseInt(loadState('end_to_end_encryption', 'encryptionVersion')),
/** @type {number} */
counter: Number.parseInt(loadState('end_to_end_encryption', 'counter')),
/** @type {{file: File, step: string, error: boolean}[]} */
uploadedFiles: [],
loading: false,
Expand Down Expand Up @@ -149,7 +153,7 @@ export default {
try {
logger.debug('Locking the folder', { lockToken: this.lockToken, shareToken: this.shareToken })
lockToken = await lock(this.folderId, this.shareToken)
lockToken = await lock(this.encryptionVersion, this.counter + 1, this.folderId, this.shareToken)
} catch (exception) {
logger.error('Could not lock the folder', { exception })
showError(t('end_to_end_encryption', 'Could not lock the folder'))
Expand Down Expand Up @@ -179,7 +183,7 @@ export default {
.filter(({ error }) => !error)
.reduce((fileDropEntries, { fileDrop }) => ({ ...fileDropEntries, ...fileDrop }), {})
await uploadFileDrop(this.folderId, fileDrops, lockToken, this.shareToken)
await uploadFileDrop(this.encryptionVersion, this.folderId, fileDrops, lockToken, this.shareToken)
} catch (exception) {
logger.error('Error while uploading metadata', { exception })
showError(t('end_to_end_encryption', 'Error while uploading metadata'))
Expand All @@ -190,7 +194,8 @@ export default {
progresses
.filter(({ error }) => !error)
.forEach(progress => { progress.step = UploadStep.UNLOCKING })
await unlock(this.folderId, lockToken, this.shareToken)
await unlock(this.encryptionVersion, this.folderId, lockToken, this.shareToken)
this.counter++
logger.debug('Unlocking the folder', { lockToken, shareToken: this.shareToken })
progresses
.filter(({ error }) => !error)
Expand Down

0 comments on commit 6c118a5

Please sign in to comment.