Skip to content

Commit fc7ed9f

Browse files
committed
export endpoint
1 parent 1a9548e commit fc7ed9f

File tree

4 files changed

+146
-45
lines changed

4 files changed

+146
-45
lines changed

appinfo/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
return [
44

55
'ocs' => [
6+
['name' => 'Local#setupExport', 'url' => '/setup/{encrypted}', 'verb' => 'GET'],
67
['name' => 'Local#getSettings', 'url' => '/settings', 'verb' => 'GET'],
78
['name' => 'Local#setSettings', 'url' => '/settings', 'verb' => 'PUT'],
89
['name' => 'Local#getRestoringPoints', 'url' => '/rp', 'verb' => 'GET'],

lib/Command/SetupExport.php

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333

3434
use Exception;
3535
use OC\Core\Command\Base;
36-
use OCA\Backup\Service\ConfigService;
37-
use OCA\Backup\Service\EncryptService;
38-
use OCA\Backup\Service\RemoteService;
39-
use OCA\Backup\Service\RemoteStreamService;
36+
use OCA\Backup\Service\ExportService;
4037
use Symfony\Component\Console\Input\InputInterface;
4138
use Symfony\Component\Console\Input\InputOption;
4239
use Symfony\Component\Console\Output\OutputInterface;
@@ -50,39 +47,19 @@
5047
class SetupExport extends Base {
5148

5249

53-
/** @var RemoteService */
54-
private $remoteService;
55-
56-
/** @var RemoteStreamService */
57-
private $remoteStreamService;
58-
59-
/** @var EncryptService */
60-
private $encryptService;
61-
62-
/** @var ConfigService */
63-
private $configService;
50+
/** @var ExportService */
51+
private $exportService;
6452

6553

6654
/**
6755
* SetupExport constructor.
6856
*
69-
* @param RemoteService $remoteService
70-
* @param RemoteStreamService $remoteStreamService
71-
* @param EncryptService $encryptService
72-
* @param ConfigService $configService
57+
* @param ExportService $exportService
7358
*/
74-
public function __construct(
75-
RemoteService $remoteService,
76-
RemoteStreamService $remoteStreamService,
77-
EncryptService $encryptService,
78-
ConfigService $configService
79-
) {
59+
public function __construct(ExportService $exportService) {
8060
parent::__construct();
8161

82-
$this->remoteService = $remoteService;
83-
$this->remoteStreamService = $remoteStreamService;
84-
$this->encryptService = $encryptService;
85-
$this->configService = $configService;
62+
$this->exportService = $exportService;
8663
}
8764

8865

@@ -104,21 +81,8 @@ protected function configure() {
10481
* @throws Exception
10582
*/
10683
protected function execute(InputInterface $input, OutputInterface $output): int {
107-
$this->remoteStreamService->getAppSignatory(true);
108-
109-
$setup = [
110-
'signatory' => $this->configService->getAppValue('key_pairs'),
111-
'remote' => $this->remoteService->getAll(true),
112-
'encryption' => $this->encryptService->getEncryptionKeys(true)
113-
];
114-
115-
$data = json_encode($setup);
116-
11784
$key = '';
118-
if ($input->getOption('key')) {
119-
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
120-
$data = $this->encryptService->encryptString($data, $key);
121-
}
85+
$data = $this->exportService->export($input->getOption('key'), $key);
12286

12387
$output->writeln($data);
12488

@@ -127,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
12791
$io->getErrorStyle()->warning(
12892
'Keep this KEY somewhere safe, it will be required to import' . "\n"
12993
. 'the setup of your Backup App on a fresh installation of Nextcloud: ' . "\n\n"
130-
. base64_encode($key)
94+
. $key
13195
);
13296
}
13397

lib/Controller/LocalController.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCA\Backup\Model\BackupEvent;
4242
use OCA\Backup\Service\ConfigService;
4343
use OCA\Backup\Service\CronService;
44+
use OCA\Backup\Service\ExportService;
4445
use OCA\Backup\Service\ExternalFolderService;
4546
use OCA\Backup\Service\FilesService;
4647
use OCA\Backup\Service\PointService;
@@ -79,6 +80,9 @@ class LocalController extends OcsController {
7980
/** @var ExternalFolderService */
8081
private $externalFolderService;
8182

83+
/** @var ExportService */
84+
private $exportService;
85+
8286
/** @var ConfigService */
8387
private $configService;
8488

@@ -94,6 +98,7 @@ class LocalController extends OcsController {
9498
* @param FilesService $filesService
9599
* @param CronService $cronService
96100
* @param ExternalFolderService $externalFolderService
101+
* @param ExportService $exportService
97102
* @param ConfigService $configService
98103
*/
99104
public function __construct(
@@ -105,6 +110,7 @@ public function __construct(
105110
FilesService $filesService,
106111
CronService $cronService,
107112
ExternalFolderService $externalFolderService,
113+
ExportService $exportService,
108114
ConfigService $configService
109115
) {
110116
parent::__construct($appName, $request);
@@ -115,6 +121,7 @@ public function __construct(
115121
$this->filesService = $filesService;
116122
$this->cronService = $cronService;
117123
$this->externalFolderService = $externalFolderService;
124+
$this->exportService = $exportService;
118125
$this->configService = $configService;
119126
}
120127

@@ -181,11 +188,34 @@ public function setSettings(array $settings): DataResponse {
181188
);
182189
}
183190
}
184-
191+
185192
return new DataResponse(array_merge($settings, $this->cronService->nextBackups()));
186193
}
187194

188195

196+
/**
197+
* @param string $encrypted
198+
*
199+
* @return DataResponse
200+
* @throws OCSException
201+
*/
202+
public function setupExport(string $encrypted): DataResponse {
203+
try {
204+
$key = '';
205+
$content = $this->exportService->export(($encrypted === 'encrypted'), $key);
206+
207+
return new DataResponse(
208+
[
209+
'key' => $key,
210+
'content' => $content
211+
]
212+
);
213+
} catch (Exception $e) {
214+
throw new OcsException($e->getMessage(), Http::STATUS_BAD_REQUEST);
215+
}
216+
}
217+
218+
189219
/**
190220
* @return DataResponse
191221
* @throws OCSException

lib/Service/ExportService.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
/**
7+
* Nextcloud - Backup now. Restore later.
8+
*
9+
* This file is licensed under the Affero General Public License version 3 or
10+
* later. See the COPYING file.
11+
*
12+
* @author Maxence Lange <maxence@artificial-owl.com>
13+
* @copyright 2021, Maxence Lange <maxence@artificial-owl.com>
14+
* @license GNU AGPL version 3 or any later version
15+
*
16+
* This program is free software: you can redistribute it and/or modify
17+
* it under the terms of the GNU Affero General Public License as
18+
* published by the Free Software Foundation, either version 3 of the
19+
* License, or (at your option) any later version.
20+
*
21+
* This program is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Affero General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Affero General Public License
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28+
*
29+
*/
30+
31+
32+
namespace OCA\Backup\Service;
33+
34+
use ArtificialOwl\MySmallPhpTools\Exceptions\SignatoryException;
35+
use SodiumException;
36+
37+
/**
38+
* Class ExportService
39+
*
40+
* @package OCA\Backup\Service
41+
*/
42+
class ExportService {
43+
44+
45+
/** @var RemoteService */
46+
private $remoteService;
47+
48+
/** @var RemoteStreamService */
49+
private $remoteStreamService;
50+
51+
/** @var EncryptService */
52+
private $encryptService;
53+
54+
/** @var ConfigService */
55+
private $configService;
56+
57+
58+
/**
59+
* ExportService constructor.
60+
*
61+
* @param RemoteService $remoteService
62+
* @param RemoteStreamService $remoteStreamService
63+
* @param EncryptService $encryptService
64+
* @param ConfigService $configService
65+
*/
66+
public function __construct(
67+
RemoteService $remoteService,
68+
RemoteStreamService $remoteStreamService,
69+
EncryptService $encryptService,
70+
ConfigService $configService
71+
) {
72+
$this->remoteService = $remoteService;
73+
$this->remoteStreamService = $remoteStreamService;
74+
$this->encryptService = $encryptService;
75+
$this->configService = $configService;
76+
}
77+
78+
79+
/**
80+
* @param bool $encrypted
81+
* @param string $key
82+
*
83+
* @return string
84+
* @throws SignatoryException
85+
* @throws SodiumException
86+
*/
87+
public function export(bool $encrypted, string &$key = ''): string {
88+
$this->remoteStreamService->getAppSignatory(true);
89+
90+
$setup = [
91+
'signatory' => $this->configService->getAppValue('key_pairs'),
92+
'remote' => $this->remoteService->getAll(true),
93+
'encryption' => $this->encryptService->getEncryptionKeys(true)
94+
];
95+
96+
$data = json_encode($setup);
97+
if ($encrypted) {
98+
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
99+
$data = $this->encryptService->encryptString($data, $key);
100+
$key = base64_encode($key);
101+
}
102+
103+
return $data;
104+
}
105+
106+
}

0 commit comments

Comments
 (0)