55 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66 * SPDX-License-Identifier: AGPL-3.0-only
77 */
8+
89namespace OCA \Files_External ;
910
10- use OC \Files \Storage \Common ;
11- use OCA \Files_External \Config \IConfigHandler ;
12- use OCA \Files_External \Config \UserContext ;
1311use OCA \Files_External \Lib \Backend \Backend ;
1412use OCA \Files_External \Service \BackendService ;
15- use OCA \Files_External \Service \GlobalStoragesService ;
16- use OCA \Files_External \Service \UserGlobalStoragesService ;
17- use OCA \Files_External \Service \UserStoragesService ;
18- use OCP \Files \StorageNotAvailableException ;
19- use OCP \IConfig ;
20- use OCP \Security \ISecureRandom ;
13+ use OCA \Files_External \Service \EncryptionService ;
2114use OCP \Server ;
22- use phpseclib \Crypt \AES ;
2315use Psr \Container \ContainerExceptionInterface ;
24- use Psr \Log \LoggerInterface ;
2516
2617/**
2718 * Class to configure mount.json globally and for users
2819 */
2920class MountConfig {
30- // TODO: make this class non-static and give it a proper namespace
31-
3221 public const MOUNT_TYPE_GLOBAL = 'global ' ;
3322 public const MOUNT_TYPE_GROUP = 'group ' ;
3423 public const MOUNT_TYPE_USER = 'user ' ;
3524 public const MOUNT_TYPE_PERSONAL = 'personal ' ;
3625
37- // whether to skip backend test (for unit tests, as this static class is not mockable)
38- public static $ skipTest = false ;
39-
40- public function __construct (
41- private UserGlobalStoragesService $ userGlobalStorageService ,
42- private UserStoragesService $ userStorageService ,
43- private GlobalStoragesService $ globalStorageService ,
44- ) {
45- }
46-
4726 /**
4827 * @param mixed $input
4928 * @param string|null $userId
5029 * @return mixed
5130 * @throws ContainerExceptionInterface
5231 * @since 16.0.0
32+ * @deprecated 34.0.0 use BackendService instead
5333 */
5434 public static function substitutePlaceholdersInConfig ($ input , ?string $ userId = null ) {
55- /** @var BackendService $backendService */
56- $ backendService = Server::get (BackendService::class);
57- /** @var IConfigHandler[] $handlers */
58- $ handlers = $ backendService ->getConfigHandlers ();
59- foreach ($ handlers as $ handler ) {
60- if ($ handler instanceof UserContext && $ userId !== null ) {
61- $ handler ->setUserId ($ userId );
62- }
63- $ input = $ handler ->handle ($ input );
64- }
65- return $ input ;
35+ return Server::get (BackendService::class)->applyConfigHandlers ($ input , $ userId );
6636 }
6737
6838 /**
@@ -73,120 +43,41 @@ public static function substitutePlaceholdersInConfig($input, ?string $userId =
7343 * @param boolean $isPersonal
7444 * @return int see self::STATUS_*
7545 * @throws \Exception
46+ * @deprecated 34.0.0 use BackendService instead
7647 */
7748 public static function getBackendStatus ($ class , $ options ) {
78- if (self ::$ skipTest ) {
79- return StorageNotAvailableException::STATUS_SUCCESS ;
80- }
81- foreach ($ options as $ key => &$ option ) {
82- if ($ key === 'password ' ) {
83- // no replacements in passwords
84- continue ;
85- }
86- $ option = self ::substitutePlaceholdersInConfig ($ option );
87- }
88- if (class_exists ($ class )) {
89- try {
90- /** @var Common $storage */
91- $ storage = new $ class ($ options );
92-
93- try {
94- $ result = $ storage ->test ();
95- $ storage ->setAvailability ($ result );
96- if ($ result ) {
97- return StorageNotAvailableException::STATUS_SUCCESS ;
98- }
99- } catch (\Exception $ e ) {
100- $ storage ->setAvailability (false );
101- throw $ e ;
102- }
103- } catch (\Exception $ exception ) {
104- Server::get (LoggerInterface::class)->error ($ exception ->getMessage (), ['exception ' => $ exception , 'app ' => 'files_external ' ]);
105- throw $ exception ;
106- }
107- }
108- return StorageNotAvailableException::STATUS_ERROR ;
49+ return Server::get (BackendService::class)->getBackendStatus ($ class , $ options );
10950 }
11051
11152 /**
11253 * Encrypt passwords in the given config options
11354 *
11455 * @param array $options mount options
11556 * @return array updated options
57+ * @deprecated 34.0.0 use EncryptionService instead
11658 */
11759 public static function encryptPasswords ($ options ) {
118- if (isset ($ options ['password ' ])) {
119- $ options ['password_encrypted ' ] = self ::encryptPassword ($ options ['password ' ]);
120- // do not unset the password, we want to keep the keys order
121- // on load... because that's how the UI currently works
122- $ options ['password ' ] = '' ;
123- }
124- return $ options ;
60+ return Server::get (EncryptionService::class)->encryptPasswords ($ options );
12561 }
12662
12763 /**
12864 * Decrypt passwords in the given config options
12965 *
13066 * @param array $options mount options
13167 * @return array updated options
68+ * @deprecated 34.0.0 use EncryptionService instead
13269 */
13370 public static function decryptPasswords ($ options ) {
134- // note: legacy options might still have the unencrypted password in the "password" field
135- if (isset ($ options ['password_encrypted ' ])) {
136- $ options ['password ' ] = self ::decryptPassword ($ options ['password_encrypted ' ]);
137- unset($ options ['password_encrypted ' ]);
138- }
139- return $ options ;
140- }
141-
142- /**
143- * Encrypt a single password
144- *
145- * @param string $password plain text password
146- * @return string encrypted password
147- */
148- private static function encryptPassword ($ password ) {
149- $ cipher = self ::getCipher ();
150- $ iv = Server::get (ISecureRandom::class)->generate (16 );
151- $ cipher ->setIV ($ iv );
152- return base64_encode ($ iv . $ cipher ->encrypt ($ password ));
153- }
154-
155- /**
156- * Decrypts a single password
157- *
158- * @param string $encryptedPassword encrypted password
159- * @return string plain text password
160- */
161- private static function decryptPassword ($ encryptedPassword ) {
162- $ cipher = self ::getCipher ();
163- $ binaryPassword = base64_decode ($ encryptedPassword );
164- $ iv = substr ($ binaryPassword , 0 , 16 );
165- $ cipher ->setIV ($ iv );
166- $ binaryPassword = substr ($ binaryPassword , 16 );
167- return $ cipher ->decrypt ($ binaryPassword );
168- }
169-
170- /**
171- * Returns the encryption cipher
172- *
173- * @return AES
174- */
175- private static function getCipher () {
176- $ cipher = new AES (AES ::MODE_CBC );
177- $ cipher ->setKey (Server::get (IConfig::class)->getSystemValue ('passwordsalt ' , null ));
178- return $ cipher ;
71+ return Server::get (EncryptionService::class)->decryptPasswords ($ options );
17972 }
18073
18174 /**
18275 * Computes a hash based on the given configuration.
18376 * This is mostly used to find out whether configurations
18477 * are the same.
185- *
186- * @param array $config
187- * @return string
78+ * @throws \JsonException
18879 */
189- public static function makeConfigHash ($ config ) {
80+ public static function makeConfigHash (array $ config ): string {
19081 $ data = json_encode (
19182 [
19283 'c ' => $ config ['backend ' ],
@@ -195,7 +86,8 @@ public static function makeConfigHash($config) {
19586 'o ' => $ config ['options ' ],
19687 'p ' => $ config ['priority ' ] ?? -1 ,
19788 'mo ' => $ config ['mountOptions ' ] ?? [],
198- ]
89+ ],
90+ JSON_THROW_ON_ERROR
19991 );
20092 return hash ('md5 ' , $ data );
20193 }
0 commit comments