summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2016-02-11 13:55:22 +0100
committerRobin Appelman <icewind@owncloud.com>2016-02-11 14:00:08 +0100
commit80eb1aada52c4b9d8e6a1ab0ce165c1977794071 (patch)
tree3fbef9ee1193babf34e4dc155ea4cd5d1189e4b0
parent06af9b817edb1fdf230f71c2fc77bc9c12031bc6 (diff)
downloadnextcloud-server-80eb1aada52c4b9d8e6a1ab0ce165c1977794071.tar.gz
nextcloud-server-80eb1aada52c4b9d8e6a1ab0ce165c1977794071.zip
encrypt passwords for files_external
-rw-r--r--apps/files_external/service/dbconfigservice.php36
-rw-r--r--apps/files_external/tests/service/dbconfigservicetest.php2
-rw-r--r--apps/files_external/tests/service/storagesservicetest.php2
3 files changed, 35 insertions, 5 deletions
diff --git a/apps/files_external/service/dbconfigservice.php b/apps/files_external/service/dbconfigservice.php
index d52bf51e4aa..07f9942e05c 100644
--- a/apps/files_external/service/dbconfigservice.php
+++ b/apps/files_external/service/dbconfigservice.php
@@ -23,6 +23,7 @@ namespace OCA\Files_External\Service;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
+use OCP\Security\ICrypto;
/**
* Stores the mount config in the database
@@ -41,12 +42,19 @@ class DBConfigService {
private $connection;
/**
+ * @var ICrypto
+ */
+ private $crypto;
+
+ /**
* DBConfigService constructor.
*
* @param IDBConnection $connection
+ * @param ICrypto $crypto
*/
- public function __construct(IDBConnection $connection) {
+ public function __construct(IDBConnection $connection, ICrypto $crypto) {
$this->connection = $connection;
+ $this->crypto = $crypto;
}
/**
@@ -246,6 +254,9 @@ class DBConfigService {
* @param string $value
*/
public function setConfig($mountId, $key, $value) {
+ if ($key === 'password') {
+ $value = $this->encryptValue($value);
+ }
$count = $this->connection->insertIfNotExist('*PREFIX*external_config', [
'mount_id' => $mountId,
'key' => $key,
@@ -267,6 +278,7 @@ class DBConfigService {
* @param string $value
*/
public function setOption($mountId, $key, $value) {
+
$count = $this->connection->insertIfNotExist('*PREFIX*external_options', [
'mount_id' => $mountId,
'key' => $key,
@@ -398,13 +410,31 @@ class DBConfigService {
* @return array ['key1' => $value1, ...]
*/
private function createKeyValueMap(array $keyValuePairs) {
+ $decryptedPairts = array_map(function ($pair) {
+ if ($pair['key'] === 'password') {
+ $pair['value'] = $this->decryptValue($pair['value']);
+ }
+ return $pair;
+ }, $keyValuePairs);
$keys = array_map(function ($pair) {
return $pair['key'];
- }, $keyValuePairs);
+ }, $decryptedPairts);
$values = array_map(function ($pair) {
return $pair['value'];
- }, $keyValuePairs);
+ }, $decryptedPairts);
return array_combine($keys, $values);
}
+
+ private function encryptValue($value) {
+ return $this->crypto->encrypt($value);
+ }
+
+ private function decryptValue($value) {
+ try {
+ return $this->crypto->decrypt($value);
+ } catch (\Exception $e) {
+ return $value;
+ }
+ }
}
diff --git a/apps/files_external/tests/service/dbconfigservicetest.php b/apps/files_external/tests/service/dbconfigservicetest.php
index 41b5df73613..30c67ac8c93 100644
--- a/apps/files_external/tests/service/dbconfigservicetest.php
+++ b/apps/files_external/tests/service/dbconfigservicetest.php
@@ -45,7 +45,7 @@ class DBConfigServiceTest extends TestCase {
public function setUp() {
parent::setUp();
$this->connection = \OC::$server->getDatabaseConnection();
- $this->dbConfig = new DBConfigService($this->connection);
+ $this->dbConfig = new DBConfigService($this->connection, \OC::$server->getCrypto());
}
public function tearDown() {
diff --git a/apps/files_external/tests/service/storagesservicetest.php b/apps/files_external/tests/service/storagesservicetest.php
index 68671b599bd..3fbe3b755e1 100644
--- a/apps/files_external/tests/service/storagesservicetest.php
+++ b/apps/files_external/tests/service/storagesservicetest.php
@@ -83,7 +83,7 @@ abstract class StoragesServiceTest extends \Test\TestCase {
public function setUp() {
parent::setUp();
- $this->dbConfig = new CleaningDBConfig(\OC::$server->getDatabaseConnection());
+ $this->dbConfig = new CleaningDBConfig(\OC::$server->getDatabaseConnection(), \OC::$server->getCrypto());
self::$hookCalls = array();
$config = \OC::$server->getConfig();
$this->dataDir = $config->getSystemValue(