Skip to content

Commit

Permalink
Store signature on metadata save
Browse files Browse the repository at this point in the history
And send it back in a header on request.

Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Jul 3, 2023
1 parent e1a97e5 commit c6d6c5f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/Controller/MetaDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ public function getMetaData(int $id, ?string $shareToken = null): DataResponse {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Cannot read metadata'));
}
return new DataResponse(['meta-data' => $metaData]);
return new DataResponse(
['meta-data' => $metaData],
Http::STATUS_OK,
['X-NC-E2EE-SIGNATURE' => $this->metaDataStorage->getSignature($id)],
);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/IMetaDataStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public function updateMetaDataIntoIntermediateFile(string $userId, int $id, stri
*/
public function saveIntermediateFile(string $userId, int $id): void;

/**
* Get the stored signature
*
* @throws NotPermittedException
* @throws NotFoundException
*/
public function getSignature(int $id): string;

/**
* Delete the previously set intermediate file
*
Expand Down
27 changes: 27 additions & 0 deletions lib/MetaDataStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class MetaDataStorage implements IMetaDataStorage {
private string $metaDataRoot = '/meta-data';
private string $metaDataFileName = 'meta.data';
private string $intermediateMetaDataFileName = 'intermediate.meta.data';
private string $metaDataSignatureFileName = 'meta.data.signature';

public function __construct(IAppData $appData,
IRootFolder $rootFolder) {
Expand Down Expand Up @@ -195,9 +196,35 @@ public function saveIntermediateFile(string $userId, int $id): void {
$intermediateMetaDataFile->delete();
}

// Save signature alongside the metadata.
// It will be send in a header on metadata request for the client to check.
$this->saveSignature($id, json_decode($intermediateMetaDataFile->getContent(), true)['signature']);

$this->cleanupLegacyFile($userId, $id);
}

private function saveSignature(int $id, string $signature): void {
$folderName = $this->getFolderNameForFileId($id);
$dir = $this->appData->getFolder($folderName);

try {
$signatureFile = $dir->getFile($this->metaDataSignatureFileName);
} catch (NotFoundException $ex) {
$signatureFile = $dir->newFile($this->metaDataSignatureFileName);
}

$signatureFile->putContent($signature);
}

/**
* @inheritDoc
*/
public function getSignature(int $id): string {
$folderName = $this->getFolderNameForFileId($id);
$dir = $this->appData->getFolder($folderName);
return $dir->getFile($this->metaDataSignatureFileName)->getContent()();
}

/**
* @inheritDoc
*/
Expand Down

0 comments on commit c6d6c5f

Please sign in to comment.