Skip to content

Commit bb0eb83

Browse files
author
Simon Spannagel
committed
Allow removing gateways via CLI
Signed-off-by: Simon Spannagel <[email protected]>
1 parent 6bfb4d5 commit bb0eb83

16 files changed

+250
-69
lines changed

appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
<command>OCA\TwoFactorGateway\Command\Configure</command>
3232
<command>OCA\TwoFactorGateway\Command\Status</command>
3333
<command>OCA\TwoFactorGateway\Command\Test</command>
34+
<command>OCA\TwoFactorGateway\Command\Remove</command>
3435
</commands>
3536
</info>

lib/Command/Remove.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @author Christoph Wurst <[email protected]>
7+
* @author Simon Spannagel <[email protected]>
8+
*
9+
* Nextcloud - Two-factor Gateway
10+
*
11+
* This code is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License, version 3,
13+
* as published by the Free Software Foundation.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License, version 3,
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>
22+
*
23+
*/
24+
25+
namespace OCA\TwoFactorGateway\Command;
26+
27+
use OCA\TwoFactorGateway\Service\Gateway\Signal\Gateway as SignalGateway;
28+
use OCA\TwoFactorGateway\Service\Gateway\SMS\Gateway as SMSGateway;
29+
use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway;
30+
use Symfony\Component\Console\Command\Command;
31+
use Symfony\Component\Console\Input\InputArgument;
32+
use Symfony\Component\Console\Input\InputInterface;
33+
use Symfony\Component\Console\Output\OutputInterface;
34+
35+
class Remove extends Command {
36+
37+
/** @var SignalGateway */
38+
private $signalGateway;
39+
40+
/** @var SMSGateway */
41+
private $smsGateway;
42+
43+
/** @var TelegramGateway */
44+
private $telegramGateway;
45+
46+
public function __construct(SignalGateway $signalGateway,
47+
SMSGateway $smsGateway,
48+
TelegramGateway $telegramGateway) {
49+
parent::__construct('twofactorauth:gateway:remove');
50+
$this->signalGateway = $signalGateway;
51+
$this->smsGateway = $smsGateway;
52+
$this->telegramGateway = $telegramGateway;
53+
54+
$this->addArgument(
55+
'gateway',
56+
InputArgument::REQUIRED,
57+
'The name of the gateway, e.g. sms, signal, telegram, etc.'
58+
);
59+
}
60+
61+
protected function execute(InputInterface $input, OutputInterface $output) {
62+
$gatewayName = $input->getArgument('gateway');
63+
64+
/** @var IGateway $gateway */
65+
$gateway = null;
66+
switch ($gatewayName) {
67+
case 'signal':
68+
$gateway = $this->signalGateway;
69+
break;
70+
case 'sms':
71+
$gateway = $this->smsGateway;
72+
break;
73+
case 'telegram':
74+
$gateway = $this->telegramGateway;
75+
break;
76+
default:
77+
$output->writeln("<error>Invalid gateway $gatewayName</error>");
78+
return 1;
79+
}
80+
81+
$gateway->getConfig()->remove();
82+
$output->writeln("Removed configuration for gateway $gatewayName");
83+
return 0;
84+
}
85+
}

lib/Service/Gateway/SMS/GatewayConfig.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ public function isComplete(): bool {
6565
return false;
6666
}
6767
}
68+
69+
public function remove() {
70+
$this->getProvider()->getConfig()->remove();
71+
}
6872
}

lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131

3232
class ClickatellCentralConfig implements IProviderConfig {
3333

34+
const expected = [
35+
'clickatell_central_api',
36+
'clickatell_central_user',
37+
'clickatell_central_password',
38+
];
39+
3440
/** @var IConfig */
3541
private $config;
3642

@@ -72,11 +78,12 @@ public function setPassword(string $password) {
7278

7379
public function isComplete(): bool {
7480
$set = $this->config->getAppKeys(Application::APP_NAME);
75-
$expected = [
76-
'clickatell_central_api',
77-
'clickatell_central_user',
78-
'clickatell_central_password',
79-
];
80-
return count(array_intersect($set, $expected)) === count($expected);
81+
return count(array_intersect($set, self::expected)) === count(self::expected);
82+
}
83+
84+
public function remove() {
85+
foreach(self::expected as $key) {
86+
$this->config->deleteAppValue(Application::APP_NAME, $key);
87+
}
8188
}
8289
}

lib/Service/Gateway/SMS/Provider/ClockworkSMSConfig.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
class ClockworkSMSConfig implements IProviderConfig {
3232

33+
const expected = [
34+
'clockworksms_apitoken'
35+
];
36+
3337
/** @var IConfig */
3438
private $config;
3539

@@ -55,9 +59,12 @@ public function setApiToken(string $user) {
5559

5660
public function isComplete(): bool {
5761
$set = $this->config->getAppKeys(Application::APP_NAME);
58-
$expected = [
59-
'clockworksms_apitoken'
60-
];
61-
return count(array_intersect($set, $expected)) === count($expected);
62+
return count(array_intersect($set, self::expected)) === count(self::expected);
63+
}
64+
65+
public function remove() {
66+
foreach(self::expected as $key) {
67+
$this->config->deleteAppValue(Application::APP_NAME, $key);
68+
}
6269
}
6370
}

lib/Service/Gateway/SMS/Provider/EcallSMSConfig.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
class EcallSMSConfig implements IProviderConfig {
3232

33+
const expected = [
34+
'ecallsms_username',
35+
'ecallsms_password',
36+
'ecallsms_senderid',
37+
];
38+
3339
/** @var IConfig */
3440
private $config;
3541

@@ -71,11 +77,12 @@ public function setSenderId(string $senderid) {
7177

7278
public function isComplete(): bool {
7379
$set = $this->config->getAppKeys(Application::APP_NAME);
74-
$expected = [
75-
'ecallsms_username',
76-
'ecallsms_password',
77-
'ecallsms_senderid',
78-
];
79-
return count(array_intersect($set, $expected)) === count($expected);
80+
return count(array_intersect($set, self::expected)) === count(self::expected);
81+
}
82+
83+
public function remove() {
84+
foreach(self::expected as $key) {
85+
$this->config->deleteAppValue(Application::APP_NAME, $key);
86+
}
8087
}
8188
}

lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
class HuaweiE3531Config implements IProviderConfig {
3232

33+
const expected = [
34+
'huawei_e3531_api',
35+
];
36+
3337
/** @var IConfig */
3438
private $config;
3539

@@ -55,9 +59,12 @@ public function setUrl(string $url) {
5559

5660
public function isComplete(): bool {
5761
$set = $this->config->getAppKeys(Application::APP_NAME);
58-
$expected = [
59-
'huawei_e3531_api',
60-
];
61-
return count(array_intersect($set, $expected)) === count($expected);
62+
return count(array_intersect($set, self::expected)) === count(self::expected);
63+
}
64+
65+
public function remove() {
66+
foreach(self::expected as $key) {
67+
$this->config->deleteAppValue(Application::APP_NAME, $key);
68+
}
6269
}
6370
}

lib/Service/Gateway/SMS/Provider/OvhConfig.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030

3131
class OvhConfig implements IProviderConfig {
3232

33+
const expected = [
34+
'ovh_application_key',
35+
'ovh_application_secret',
36+
'ovh_consumer_key',
37+
'ovh_endpoint',
38+
'ovh_account',
39+
'ovh_sender'
40+
];
41+
3342
/** @var IConfig */
3443
private $config;
3544

@@ -95,14 +104,12 @@ public function setSender($sender) {
95104

96105
public function isComplete(): bool {
97106
$set = $this->config->getAppKeys(Application::APP_NAME);
98-
$expected = [
99-
'ovh_application_key',
100-
'ovh_application_secret',
101-
'ovh_consumer_key',
102-
'ovh_endpoint',
103-
'ovh_account',
104-
'ovh_sender'
105-
];
106-
return count(array_intersect($set, $expected)) === count($expected);
107+
return count(array_intersect($set, self::expected)) === count(self::expected);
108+
}
109+
110+
public function remove() {
111+
foreach(self::expected as $key) {
112+
$this->config->deleteAppValue(Application::APP_NAME, $key);
113+
}
107114
}
108115
}

lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
class PlaySMSConfig implements IProviderConfig {
3232

33+
const expected = [
34+
'playsms_url',
35+
'playsms_user',
36+
'playsms_password',
37+
];
38+
3339
/** @var IConfig */
3440
private $config;
3541

@@ -71,11 +77,12 @@ public function setPassword(string $password) {
7177

7278
public function isComplete(): bool {
7379
$set = $this->config->getAppKeys(Application::APP_NAME);
74-
$expected = [
75-
'playsms_url',
76-
'playsms_user',
77-
'playsms_password',
78-
];
79-
return count(array_intersect($set, $expected)) === count($expected);
80+
return count(array_intersect($set, self::expected)) === count(self::expected);
81+
}
82+
83+
public function remove() {
84+
foreach(self::expected as $key) {
85+
$this->config->deleteAppValue(Application::APP_NAME, $key);
86+
}
8087
}
8188
}

lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030

3131
class PuzzelSMSConfig implements IProviderConfig {
3232

33+
const expected = [
34+
'puzzel_url',
35+
'puzzel_user',
36+
'puzzel_password',
37+
'puzzel_serviceid',
38+
];
39+
3340
/** @var IConfig */
3441
private $config;
3542

@@ -79,12 +86,12 @@ public function setServiceId(string $serviceid) {
7986

8087
public function isComplete(): bool {
8188
$set = $this->config->getAppKeys(Application::APP_NAME);
82-
$expected = [
83-
'puzzel_url',
84-
'puzzel_user',
85-
'puzzel_password',
86-
'puzzel_serviceid',
87-
];
88-
return count(array_intersect($set, $expected)) === count($expected);
89+
return count(array_intersect($set, self::expected)) === count(self::expected);
90+
}
91+
92+
public function remove() {
93+
foreach(self::expected as $key) {
94+
$this->config->deleteAppValue(Application::APP_NAME, $key);
95+
}
8996
}
9097
}

0 commit comments

Comments
 (0)