Completed
Push — master ( b34111...bbfc02 )
by Morris
18:28
created

RSAPrivateKey::manipulateStorageConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2018, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\Files_External\Lib\Auth\PublicKey;
24
25
use \OCP\IL10N;
26
use \OCA\Files_External\Lib\DefinitionParameter;
27
use \OCA\Files_External\Lib\Auth\AuthMechanism;
28
use \OCA\Files_External\Lib\StorageConfig;
29
use \OCP\IConfig;
30
use OCP\IUser;
31
use \phpseclib\Crypt\RSA as RSACrypt;
32
33
/**
34
 * RSA public key authentication
35
 */
36
class RSAPrivateKey extends AuthMechanism {
37
38
	/** @var IConfig */
39
	private $config;
40
41
	public function __construct(IL10N $l, IConfig $config) {
42
		$this->config = $config;
43
44
		$this
45
			->setIdentifier('publickey::rsa_private')
46
			->setScheme(self::SCHEME_PUBLICKEY)
47
			->setText($l->t('RSA private key'))
48
			->addParameters([
49
				new DefinitionParameter('user', $l->t('Username')),
50
				(new DefinitionParameter('password', $l->t('Password')))
51
					->setFlag(DefinitionParameter::FLAG_OPTIONAL)
52
					->setType(DefinitionParameter::VALUE_PASSWORD),
53
				new DefinitionParameter('private_key', $l->t('Private key')),
54
			]);
55
	}
56
57 View Code Duplication
	public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
58
		$auth = new RSACrypt();
59
		$auth->setPassword($this->config->getSystemValue('secret', ''));
60
		if (!$auth->loadKey($storage->getBackendOption('private_key'))) {
61
			throw new \RuntimeException('unable to load private key');
62
		}
63
		$storage->setBackendOption('public_key_auth', $auth);
64
	}
65
}
66