Completed
Push — master ( 0d138c...091bf0 )
by Lukas
07:48
created

SignApp   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 10.81 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 8
loc 74
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A configure() 8 8 1
C execute() 0 39 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OC\Core\Command\Integrity;
24
25
use OC\IntegrityCheck\Checker;
26
use OC\IntegrityCheck\Helpers\FileAccessHelper;
27
use OCP\IURLGenerator;
28
use phpseclib\Crypt\RSA;
29
use phpseclib\File\X509;
30
use Symfony\Component\Console\Command\Command;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
/**
36
 * Class SignApp
37
 *
38
 * @package OC\Core\Command\Integrity
39
 */
40
class SignApp extends Command {
41
	/** @var Checker */
42
	private $checker;
43
	/** @var FileAccessHelper */
44
	private $fileAccessHelper;
45
	/** @var IURLGenerator */
46
	private $urlGenerator;
47
48
	/**
49
	 * @param Checker $checker
50
	 * @param FileAccessHelper $fileAccessHelper
51
	 * @param IURLGenerator $urlGenerator
52
	 */
53
	public function __construct(Checker $checker,
54
								FileAccessHelper $fileAccessHelper,
55
								IURLGenerator $urlGenerator) {
56
		parent::__construct(null);
57
		$this->checker = $checker;
58
		$this->fileAccessHelper = $fileAccessHelper;
59
		$this->urlGenerator = $urlGenerator;
60
	}
61
62 View Code Duplication
	protected function configure() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
		$this
64
			->setName('integrity:sign-app')
65
			->setDescription('Signs an app using a private key.')
66
			->addOption('path', null, InputOption::VALUE_REQUIRED, 'Application to sign')
67
			->addOption('privateKey', null, InputOption::VALUE_REQUIRED, 'Path to private key to use for signing')
68
			->addOption('certificate', null, InputOption::VALUE_REQUIRED, 'Path to certificate to use for signing');
69
	}
70
71
	/**
72
	 * {@inheritdoc }
73
	 */
74
	protected function execute(InputInterface $input, OutputInterface $output) {
75
		$path = $input->getOption('path');
76
		$privateKeyPath = $input->getOption('privateKey');
77
		$keyBundlePath = $input->getOption('certificate');
78
		if(is_null($path) || is_null($privateKeyPath) || is_null($keyBundlePath)) {
79
			$documentationUrl = $this->urlGenerator->linkToDocs('developer-code-integrity');
80
			$output->writeln('This command requires the --path, --privateKey and --certificate.');
81
			$output->writeln('Example: ./occ integrity:sign-app --path="/Users/lukasreschke/Programming/myapp/" --privateKey="/Users/lukasreschke/private/myapp.key" --certificate="/Users/lukasreschke/public/mycert.crt"');
82
			$output->writeln('For more information please consult the documentation: '. $documentationUrl);
83
			return null;
84
		}
85
86
		$privateKey = $this->fileAccessHelper->file_get_contents($privateKeyPath);
87
		$keyBundle = $this->fileAccessHelper->file_get_contents($keyBundlePath);
88
89
		if($privateKey === false) {
90
			$output->writeln(sprintf('Private key "%s" does not exists.', $privateKeyPath));
91
			return null;
92
		}
93
94
		if($keyBundle === false) {
95
			$output->writeln(sprintf('Certificate "%s" does not exists.', $keyBundlePath));
96
			return null;
97
		}
98
99
		$rsa = new RSA();
100
		$rsa->loadKey($privateKey);
101
		$x509 = new X509();
102
		$x509->loadX509($keyBundle);
103
		$x509->setPrivateKey($rsa);
104
		try {
105
			$this->checker->writeAppSignature($path, $x509, $rsa);
106
			$output->writeln('Successfully signed "'.$path.'"');
107
		} catch (\Exception $e){
108
			$output->writeln('Error: ' . $e->getMessage());
109
			return 1;
110
		}
111
		return 0;
112
	}
113
}
114