diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2018-08-21 22:46:09 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2018-08-21 22:46:09 +0300 |
commit | bd8e9bc6821092d6b6049d43b53f0e1f5512dcca (patch) | |
tree | 0ddd0e0cd3cb1e7cc037ead1e0f7b4f6f524005d /lib | |
parent | de724a0e8085b405684a67b04c22fc07b27743af (diff) |
Add command to configure the gateways
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/Configure.php | 148 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/GatewayConfig.php | 5 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php | 12 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/WebSmsConfig.php | 8 | ||||
-rw-r--r-- | lib/Service/Gateway/Signal/GatewayConfig.php | 36 |
5 files changed, 206 insertions, 3 deletions
diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php new file mode 100644 index 0000000..db1005b --- /dev/null +++ b/lib/Command/Configure.php @@ -0,0 +1,148 @@ +<?php + +declare(strict_types=1); + +/** + * @author Pascal Clémot <pascal.clemot@free.fr> + * + * Nextcloud - Two-factor Gateway + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\TwoFactorGateway\Command; + +use OCA\TwoFactorGateway\Service\Gateway\IGateway; +use OCA\TwoFactorGateway\Service\Gateway\Signal\Gateway as SignalGateway; +use OCA\TwoFactorGateway\Service\Gateway\Signal\GatewayConfig as SignalConfig; +use OCA\TwoFactorGateway\Service\Gateway\SMS\Gateway as SMSGateway; +use OCA\TwoFactorGateway\Service\Gateway\SMS\GatewayConfig as SMSConfig; +use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PlaySMSConfig; +use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\WebSmsConfig; +use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\Question; + +class Configure extends Command { + + /** @var SignalGateway */ + private $signalGateway; + + /** @var SMSGateway */ + private $smsGateway; + + /** @var TelegramGateway */ + private $telegramGateway; + + public function __construct(SignalGateway $signalGateway, + SMSGateway $smsGateway, + TelegramGateway $telegramGateway) { + parent::__construct('twofactorauth:gateway:configure'); + $this->signalGateway = $signalGateway; + $this->smsGateway = $smsGateway; + $this->telegramGateway = $telegramGateway; + + $this->addArgument( + 'gateway', + InputArgument::REQUIRED, + 'The identifier (e.g. phone number) of the recipient' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $gatewayName = $input->getArgument('gateway'); + + /** @var IGateway $gateway */ + $gateway = null; + switch ($gatewayName) { + case 'signal': + $this->configureSignal($input, $output); + break; + case 'sms': + $this->configureSms($input, $output); + break; + case 'telegram': + $this->configureTelegram($input, $output); + break; + default: + $output->writeln("<error>Invalid gateway $gatewayName</error>"); + return; + } + } + + private function configureSignal(InputInterface $input, OutputInterface $output) { + $helper = $this->getHelper('question'); + $urlQuestion = new Question('Please enter the URL of the Signal gateway (leave blank to use default): ', 'http://localhost:5000'); + $url = $helper->ask($input, $output, $urlQuestion); + $output->writeln("Using $url."); + + /** @var SignalConfig $config */ + $config = $this->signalGateway->getConfig(); + + $config->setUrl($url); + } + + private function configureSms(InputInterface $input, OutputInterface $output) { + $helper = $this->getHelper('question'); + $providerQuestion = new Question('Please choose a SMS provider (websms, playsms): ', 'websms'); + $provider = $helper->ask($input, $output, $providerQuestion); + + /** @var SMSConfig $config */ + $config = $this->smsGateway->getConfig(); + switch ($provider) { + case 'websms': + $config->setProvider($provider); + /** @var WebSmsConfig $providerConfig */ + $providerConfig = $config->getProvider()->getConfig(); + + $usernameQuestion = new Question('Please enter your websms.de username: '); + $username = $helper->ask($input, $output, $usernameQuestion); + $passwordQuestion = new Question('Please enter your websms.de password: '); + $password = $helper->ask($input, $output, $passwordQuestion); + + $providerConfig->setUser($username); + $providerConfig->setPassword($password); + + break; + case 'playsms': + $config->setProvider($provider); + /** @var PlaySMSConfig $providerConfig */ + $providerConfig = $config->getProvider()->getConfig(); + + $urlQuestion = new Question('Please enter your PlaySMS URL: '); + $url = $helper->ask($input, $output, $urlQuestion); + $usernameQuestion = new Question('Please enter your PlaySMS username: '); + $username = $helper->ask($input, $output, $usernameQuestion); + $passwordQuestion = new Question('Please enter your PlaySMS password: '); + $password = $helper->ask($input, $output, $passwordQuestion); + + $providerConfig->setUrl($url); + $providerConfig->setUser($username); + $providerConfig->setPassword($password); + + break; + default: + $output->writeln("Invalid provider $provider"); + break; + } + + } + + private function configureTelegram(InputInterface $input, OutputInterface $output) { + } + +}
\ No newline at end of file diff --git a/lib/Service/Gateway/SMS/GatewayConfig.php b/lib/Service/Gateway/SMS/GatewayConfig.php index ffaff71..9e19f82 100644 --- a/lib/Service/Gateway/SMS/GatewayConfig.php +++ b/lib/Service/Gateway/SMS/GatewayConfig.php @@ -53,6 +53,10 @@ class GatewayConfig implements IGatewayConfig { return $this->providerFactory->getProvider($providerName); } + public function setProvider(string $provider) { + $this->config->setAppValue(Application::APP_NAME, 'sms_provider_name', $provider); + } + public function isComplete(): bool { try { $provider = $this->getProvider(); @@ -62,4 +66,5 @@ class GatewayConfig implements IGatewayConfig { } } + }
\ No newline at end of file diff --git a/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php b/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php index 76e32e8..4ebb2d2 100644 --- a/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php +++ b/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php @@ -49,14 +49,26 @@ class PlaySMSConfig implements IProviderConfig { return $this->getOrFail('playsms_url'); } + public function setUrl(string $url) { + $this->config->setAppValue(Application::APP_NAME, 'playsms_url', $url); + } + public function getUser(): string { return $this->getOrFail('playsms_user'); } + public function setUser(string $user) { + $this->config->setAppValue(Application::APP_NAME, 'playsms_user', $user); + } + public function getPassword(): string { return $this->getOrFail('playsms_password'); } + public function setPassword(string $password) { + $this->config->setAppValue(Application::APP_NAME, 'playsms_password', $password); + } + public function isComplete(): bool { $set = $this->config->getAppKeys(Application::APP_NAME); $expected = [ diff --git a/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php b/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php index 93eafd0..6f7646f 100644 --- a/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php +++ b/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php @@ -49,10 +49,18 @@ class WebSmsConfig implements IProviderConfig { return $this->getOrFail('websms_de_user'); } + public function setUser(string $user) { + $this->config->setAppValue(Application::APP_NAME, 'websms_de_user', $user); + } + public function getPassword(): string { return $this->getOrFail('websms_de_password'); } + public function setPassword(string $password) { + $this->config->setAppValue(Application::APP_NAME, 'websms_de_password', $password); + } + public function isComplete(): bool { $set = $this->config->getAppKeys(Application::APP_NAME); $expected = [ diff --git a/lib/Service/Gateway/Signal/GatewayConfig.php b/lib/Service/Gateway/Signal/GatewayConfig.php index 27d76d8..e067876 100644 --- a/lib/Service/Gateway/Signal/GatewayConfig.php +++ b/lib/Service/Gateway/Signal/GatewayConfig.php @@ -23,13 +23,43 @@ declare(strict_types=1); namespace OCA\TwoFactorGateway\Service\Gateway\Signal; +use OCA\TwoFactorGateway\AppInfo\Application; +use OCA\TwoFactorGateway\Exception\ConfigurationException; use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig; +use OCP\IConfig; class GatewayConfig implements IGatewayConfig { + /** @var IConfig */ + private $config; + + public function __construct(IConfig $config) { + $this->config = $config; + } + + private function getOrFail(string $key): string { + $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + if (is_null($val)) { + throw new ConfigurationException(); + } + return $val; + } + + public function getUrl(): string { + return $this->getOrFail('signal_url'); + } + + public function setUrl(string $url) { + $this->config->setAppValue(Application::APP_NAME, 'signal_url', $url); + } + public function isComplete(): bool { - // TODO: https://github.com/nextcloud/twofactor_gateway/issues/84 - return true; + $set = $this->config->getAppKeys(Application::APP_NAME); + $expected = [ + 'signal_url', + ]; + return count(array_intersect($set, $expected)) === count($expected); } -}
\ No newline at end of file + +} |