diff options
author | Kim Syversen <kim.syversen@gmail.com> | 2018-11-12 18:39:17 +0300 |
---|---|---|
committer | Kim Syversen <kim.syversen@gmail.com> | 2018-11-12 18:39:17 +0300 |
commit | 745f09108e220eebb3b2acf55eb8e227fff62c8f (patch) | |
tree | f1cd3ab25d0aae4e277f40d4be3a84776d861c46 /lib | |
parent | bd741d05c5df83c04a4af792846e9ab4a3c5074c (diff) |
Added support for PuzzelSMS
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/Configure.php | 29 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/ProviderFactory.php | 4 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/PuzzelSMS.php | 82 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php | 90 |
4 files changed, 202 insertions, 3 deletions
diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index 07f16f1..a8790c6 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -30,6 +30,7 @@ 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\SMS\Provider\PuzzelSMSConfig; use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway; use OCA\TwoFactorGateway\Service\Gateway\Telegram\GatewayConfig as TelegramConfig; use Symfony\Component\Console\Command\Command; @@ -99,7 +100,7 @@ class Configure extends Command { private function configureSms(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); - $providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms): ', 'websms'); + $providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms, puzzelsms): ', 'websms'); $provider = $helper->ask($input, $output, $providerQuestion); /** @var SMSConfig $config */ @@ -147,6 +148,30 @@ class Configure extends Command { $providerConfig->setApiToken($apitoken); break; + case 'puzzelsms': + $config->setProvider($provider); + + /** @var PuzzelSMSConfig $providerConfig */ + $providerConfig = $config->getProvider()->getConfig(); + + $urlQuestion = new Question('Please enter your PuzzelSMS URL: '); + $url = $helper->ask($input, $output, $urlQuestion); + + $usernameQuestion = new Question('Please enter your PuzzelSMS username: '); + $username = $helper->ask($input, $output, $usernameQuestion); + + $passwordQuestion = new Question('Please enter your PuzzelSMS password: '); + $password = $helper->ask($input, $output, $passwordQuestion); + + $serviceQuestion = new Question('Please enter your PuzzelSMS service ID: '); + $serviceId = $helper->ask($input, $output, $serviceQuestion); + + $providerConfig->setUrl($url); + $providerConfig->setUser($username); + $providerConfig->setPassword($password); + $providerConfig->setServiceId($serviceId); + break; + default: $output->writeln("Invalid provider $provider"); break; @@ -166,4 +191,4 @@ class Configure extends Command { $config->setBotToken($token); } -} +}
\ No newline at end of file diff --git a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php index 49d2608..06369ce 100644 --- a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php +++ b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php @@ -37,6 +37,8 @@ class ProviderFactory { public function getProvider(string $id): IProvider { switch ($id) { + case PuzzelSMS::PROVIDER_ID: + return $this->container->query(PuzzelSMS::class); case PlaySMS::PROVIDER_ID: return $this->container->query(PlaySMS::class); case WebSms::PROVIDER_ID: @@ -48,4 +50,4 @@ class ProviderFactory { } } -} +}
\ No newline at end of file diff --git a/lib/Service/Gateway/SMS/Provider/PuzzelSMS.php b/lib/Service/Gateway/SMS/Provider/PuzzelSMS.php new file mode 100644 index 0000000..4c192e1 --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/PuzzelSMS.php @@ -0,0 +1,82 @@ +<?php + +declare(strict_types=1); + +/** + * @author Kim Syversen <kim.syversen@gmail.com> + * + * 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\Service\Gateway\SMS\Provider; + +use Exception; +use OCA\TwoFactorGateway\Exception\SmsTransmissionException; +use OCP\Http\Client\IClient; +use OCP\Http\Client\IClientService; +use OCP\IConfig; + +class PuzzelSMS implements IProvider { + + const PROVIDER_ID = 'puzzelsms'; + + /** @var IClient */ + private $client; + + /** @var PuzzelConfig */ + private $config; + + public function __construct(IClientService $clientService, + PuzzelSMSConfig $config) { + $this->client = $clientService->newClient(); + $this->config = $config; + } + + /** + * @param string $identifier + * @param string $message + * + * @throws SmsTransmissionException + */ + public function send(string $identifier, string $message) { + $config = $this->getConfig(); + + try { + $this->client->get( + $config->getUrl(), + [ + 'query' => [ + 'username' => $config->getUser(), + 'password' => $config->getPassword(), + "message[0].recipient" => "+".$identifier, + "message[0].content" => $message, + 'serviceId'=> $config->getServiceId(), + ], + ] + ); + } catch (Exception $ex) { + throw new SmsTransmissionException(); + } + } + + /** + * @return PuzzelConfig + */ + public function getConfig(): IProviderConfig { + return $this->config; + } + +}
\ No newline at end of file diff --git a/lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php b/lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php new file mode 100644 index 0000000..26471ef --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php @@ -0,0 +1,90 @@ +<?php + +declare(strict_types=1); + +/** + * @author Kim Syversen <kim.syversen@gmail.com> + * + * 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\Service\Gateway\SMS\Provider; + +use function array_intersect; +use OCA\TwoFactorGateway\AppInfo\Application; +use OCA\TwoFactorGateway\Exception\ConfigurationException; +use OCP\IConfig; + +class PuzzelSMSConfig implements IProviderConfig { + + /** @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('puzzel_url'); + } + + public function setUrl(string $url) { + $this->config->setAppValue(Application::APP_NAME, 'puzzel_url', $url); + } + + public function getUser(): string { + return $this->getOrFail('puzzel_user'); + } + + public function setUser(string $user) { + $this->config->setAppValue(Application::APP_NAME, 'puzzel_user', $user); + } + + public function getPassword(): string { + return $this->getOrFail('puzzel_password'); + } + + public function setPassword(string $password) { + $this->config->setAppValue(Application::APP_NAME, 'puzzel_password', $password); + } + + public function getServiceId() { + return $this->getOrFail('puzzel_serviceid'); + } + + public function setServiceId(string $serviceid) { + $this->config->setAppValue(Application::APP_NAME, 'puzzel_serviceid', $serviceid); + } + + public function isComplete(): bool { + $set = $this->config->getAppKeys(Application::APP_NAME); + $expected = [ + 'puzzel_url', + 'puzzel_user', + 'puzzel_password', + 'puzzel_serviceid', + ]; + return count(array_intersect($set, $expected)) === count($expected); + } +}
\ No newline at end of file |