Completed
Pull Request — master (#253)
by
unknown
04:02
created

Configure::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 18
rs 9.7666
cc 4
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christoph Wurst <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\TwoFactorGateway\Command;
25
26
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
27
use OCA\TwoFactorGateway\Service\Gateway\Signal\Gateway as SignalGateway;
28
use OCA\TwoFactorGateway\Service\Gateway\Signal\GatewayConfig as SignalConfig;
29
use OCA\TwoFactorGateway\Service\Gateway\SMS\Gateway as SMSGateway;
30
use OCA\TwoFactorGateway\Service\Gateway\SMS\GatewayConfig as SMSConfig;
31
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\ClockworkSMSConfig;
32
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\EcallSMSConfig;
33
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PlaySMSConfig;
34
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\WebSmsConfig;
35
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PuzzelSMSConfig;
36
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\HuaweiE3531Config;
37
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\MesstoSmsConfig;
38
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\GatewayApiSmsConfig;
39
use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway;
40
use OCA\TwoFactorGateway\Service\Gateway\Telegram\GatewayConfig as TelegramConfig;
41
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
use Symfony\Component\Console\Question\Question;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Question\Question was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
47
class Configure extends Command {
48
49
	/** @var SignalGateway */
50
	private $signalGateway;
51
52
	/** @var SMSGateway */
53
	private $smsGateway;
54
55
	/** @var TelegramGateway */
56
	private $telegramGateway;
57
58
	public function __construct(SignalGateway $signalGateway,
59
								SMSGateway $smsGateway,
60
								TelegramGateway $telegramGateway) {
61
		parent::__construct('twofactorauth:gateway:configure');
62
		$this->signalGateway = $signalGateway;
63
		$this->smsGateway = $smsGateway;
64
		$this->telegramGateway = $telegramGateway;
65
66
		$this->addArgument(
67
			'gateway',
68
			InputArgument::REQUIRED,
69
			'The name of the gateway, e.g. sms, signal, telegram, etc.'
70
		);
71
	}
72
73
	protected function execute(InputInterface $input, OutputInterface $output) {
74
		$gatewayName = $input->getArgument('gateway');
75
76
		/** @var IGateway $gateway */
77
		$gateway = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $gateway is dead and can be removed.
Loading history...
78
		switch ($gatewayName) {
79
			case 'signal':
80
				$this->configureSignal($input, $output);
81
				break;
82
			case 'sms':
83
				$this->configureSms($input, $output);
84
				break;
85
			case 'telegram':
86
				$this->configureTelegram($input, $output);
87
				break;
88
			default:
89
				$output->writeln("<error>Invalid gateway $gatewayName</error>");
90
				return;
91
		}
92
	}
93
94
	private function configureSignal(InputInterface $input, OutputInterface $output) {
95
		$helper = $this->getHelper('question');
96
		$urlQuestion = new Question('Please enter the URL of the Signal gateway (leave blank to use default): ', 'http://localhost:5000');
97
		$url = $helper->ask($input, $output, $urlQuestion);
98
		$output->writeln("Using $url.");
99
100
		/** @var SignalConfig $config */
101
		$config = $this->signalGateway->getConfig();
102
103
		$config->setUrl($url);
104
	}
105
106
	private function configureSms(InputInterface $input, OutputInterface $output) {
107
		$helper = $this->getHelper('question');
108
		$providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms, puzzelsms, ecallsms, voipms, huawei_e3531, messtosms, gatewayapisms): ', 'websms');
109
		$provider = $helper->ask($input, $output, $providerQuestion);
110
111
		/** @var SMSConfig $config */
112
		$config = $this->smsGateway->getConfig();
113
		switch ($provider) {
114
			case 'websms':
115
				$config->setProvider($provider);
116
				/** @var WebSmsConfig $providerConfig */
117
				$providerConfig = $config->getProvider()->getConfig();
118
119
				$usernameQuestion = new Question('Please enter your websms.de username: ');
120
				$username = $helper->ask($input, $output, $usernameQuestion);
121
				$passwordQuestion = new Question('Please enter your websms.de password: ');
122
				$password = $helper->ask($input, $output, $passwordQuestion);
123
124
				$providerConfig->setUser($username);
125
				$providerConfig->setPassword($password);
126
127
				break;
128
			case 'playsms':
129
				$config->setProvider($provider);
130
				/** @var PlaySMSConfig $providerConfig */
131
				$providerConfig = $config->getProvider()->getConfig();
132
133
				$urlQuestion = new Question('Please enter your PlaySMS URL: ');
134
				$url = $helper->ask($input, $output, $urlQuestion);
135
				$usernameQuestion = new Question('Please enter your PlaySMS username: ');
136
				$username = $helper->ask($input, $output, $usernameQuestion);
137
				$passwordQuestion = new Question('Please enter your PlaySMS password: ');
138
				$password = $helper->ask($input, $output, $passwordQuestion);
139
140
				$providerConfig->setUrl($url);
141
				$providerConfig->setUser($username);
142
				$providerConfig->setPassword($password);
143
144
				break;
145
			case 'clockworksms':
146
				$config->setProvider($provider);
147
				/** @var ClockworkSMSConfig $providerConfig */
148
				$providerConfig = $config->getProvider()->getConfig();
149
150
				$apitokenQuestion = new Question('Please enter your clockworksms api token: ');
151
				$apitoken = $helper->ask($input, $output, $apitokenQuestion);
152
153
				$providerConfig->setApiToken($apitoken);
154
155
				break;
156
			case 'puzzelsms':
157
				$config->setProvider($provider);
158
159
				/** @var PuzzelSMSConfig $providerConfig */
160
				$providerConfig = $config->getProvider()->getConfig();
161
162
				$urlQuestion = new Question('Please enter your PuzzelSMS URL: ');
163
				$url = $helper->ask($input, $output, $urlQuestion);
164
165
				$usernameQuestion = new Question('Please enter your PuzzelSMS username: ');
166
				$username = $helper->ask($input, $output, $usernameQuestion);
167
168
				$passwordQuestion = new Question('Please enter your PuzzelSMS password: ');
169
				$password = $helper->ask($input, $output, $passwordQuestion);
170
171
				$serviceQuestion = new Question('Please enter your PuzzelSMS service ID: ');
172
				$serviceId = $helper->ask($input, $output, $serviceQuestion);
173
174
				$providerConfig->setUrl($url);
175
				$providerConfig->setUser($username);
176
				$providerConfig->setPassword($password);
177
				$providerConfig->setServiceId($serviceId);
178
				break;
179
			case 'ecallsms':
180
				$config->setProvider($provider);
181
				/** @var EcallSMSConfig $providerConfig */
182
				$providerConfig = $config->getProvider()->getConfig();
183
184
				$usernameQuestion = new Question('Please enter your eCall.ch username: ');
185
				$username = $helper->ask($input, $output, $usernameQuestion);
186
				$passwordQuestion = new Question('Please enter your eCall.ch password: ');
187
				$password = $helper->ask($input, $output, $passwordQuestion);
188
				$senderIdQuestion = new Question('Please enter your eCall.ch sender ID: ');
189
				$senderId = $helper->ask($input, $output, $senderIdQuestion);
190
191
				$providerConfig->setUser($username);
192
				$providerConfig->setPassword($password);
193
				$providerConfig->setSenderId($senderId);
194
				break;
195
196
			case 'voipms':
197
				$config->setProvider($provider);
198
199
				/** @var VoipMsConfig $providerConfig */
200
				$providerConfig = $config->getProvider()->getConfig();
201
202
				$usernameQuestion = new Question('Please enter your VoIP.ms API username: ');
203
				$username = $helper->ask($input, $output, $usernameQuestion);
204
205
				$passwordQuestion = new Question('Please enter your VoIP.ms API password: ');
206
				$password = $helper->ask($input, $output, $passwordQuestion);
207
208
				$didQuestion = new Question('Please enter your VoIP.ms DID: ');
209
				$did = $helper->ask($input, $output, $didQuestion);
210
211
				$providerConfig->setUser($username);
212
				$providerConfig->setPassword($password);
213
				$providerConfig->setDid($did);
214
				break;
215
216
			case 'huawei_e3531':
217
				$config->setProvider($provider);
218
				/** @var HuaweiE3531Config $providerConfig */
219
				$providerConfig = $config->getProvider()->getConfig();
220
221
				$urlQuestion = new Question('Please enter the base URL of the Huawei E3531 stick: ', 'http://192.168.8.1/api');
222
				$url = $helper->ask($input, $output, $urlQuestion);
223
224
				$providerConfig->setUrl($url);
225
				break;
226
227
			case 'messtosms':
228
				$config->setProvider($provider);
229
				/** @var MesstoSmsConfig $providerConfig */
230
				$providerConfig = $config->getProvider()->getConfig();
231
232
				$usernameQuestion = new Question('Please enter your messto.com username: ');
233
				$username = $helper->ask($input, $output, $usernameQuestion);
234
				$passwordQuestion = new Question('Please enter your messto.com password: ');
235
				$password = $helper->ask($input, $output, $passwordQuestion);
236
237
				$providerConfig->setUser($username);
238
				$providerConfig->setPassword($password);
239
240
				break;
241
242
			case 'gatewayapisms':
243
				$config->setProvider($provider);
244
				/** @var GatewayApiSmsConfig $providerConfig */
245
				$providerConfig = $config->getProvider()->getConfig();
246
247
				$authtokenQuestion = new Question('Please enter your GatewayAPI authentication token: ');
248
				$authtoken = $helper->ask($input, $output, $authtokenQuestion);
249
				$senderidQuestion = new Question('Please enter your GatewayAPI sender id (11 alphanumeric characters, or 15 digits): ');
250
				$senderid = $helper->ask($input, $output, $senderidQuestion);
251
252
				$providerConfig->setAuthToken($authtoken);
253
				$providerConfig->setSenderId($senderid);
254
255
				break;
256
257
			default:
258
				$output->writeln("Invalid provider $provider");
259
				break;
260
		}
261
262
	}
263
264
	private function configureTelegram(InputInterface $input, OutputInterface $output) {
265
		$helper = $this->getHelper('question');
266
		$tokenQuestion = new Question('Please enter your Telegram bot token: ');
267
		$token = $helper->ask($input, $output, $tokenQuestion);
268
		$output->writeln("Using $token.");
269
270
		/** @var TelegramConfig $config */
271
		$config = $this->telegramGateway->getConfig();
272
273
		$config->setBotToken($token);
274
	}
275
276
}
277