Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/twofactor_gateway.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristian Schrötter <cs@fnx.li>2020-03-26 16:37:31 +0300
committerChristian Schrötter <cs@fnx.li>2020-03-26 16:37:34 +0300
commit6226572f3e8c47b40cc3a900c11f7beb028b7054 (patch)
tree94a9ec68720c2b3c028809b6b37c398b032ba1d8 /lib
parent7a3c27337beb81e7b63073d339aefcbd6a1397a2 (diff)
Implement central.clickatell.com API
Signed-off-by: Christian Schrötter <cs@fnx.li>
Diffstat (limited to 'lib')
-rw-r--r--lib/Command/Configure.php19
-rw-r--r--lib/Service/Gateway/SMS/Provider/ClickatellCentral.php75
-rw-r--r--lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php83
-rw-r--r--lib/Service/Gateway/SMS/Provider/ProviderFactory.php2
4 files changed, 178 insertions, 1 deletions
diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php
index e93c8e2..fbb8654 100644
--- a/lib/Command/Configure.php
+++ b/lib/Command/Configure.php
@@ -37,6 +37,7 @@ use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\WebSmsConfig;
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PuzzelSMSConfig;
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\HuaweiE3531Config;
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\SpryngSMSConfig;
+use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\ClickatellCentralConfig;
use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway;
use OCA\TwoFactorGateway\Service\Gateway\Telegram\GatewayConfig as TelegramConfig;
use Symfony\Component\Console\Command\Command;
@@ -107,7 +108,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, puzzelsms, ecallsms, voipms, huawei_e3531, spryng, sms77io, ovh', 'websms');
+ $providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms, puzzelsms, ecallsms, voipms, huawei_e3531, spryng, sms77io, ovh, clickatellcentral', 'websms');
$provider = $helper->ask($input, $output, $providerQuestion);
/** @var SMSConfig $config */
@@ -281,6 +282,22 @@ class Configure extends Command {
$providerConfig->setSender($sender);
break;
+ case 'clickatellcentral':
+ $config->setProvider($provider);
+ /** @var ClickatellCentralConfig $providerConfig */
+ $providerConfig = $config->getProvider()->getConfig();
+
+ $apiQuestion = new Question('Please enter your central.clickatell.com API-ID: ');
+ $api = $helper->ask($input, $output, $apiQuestion);
+ $usernameQuestion = new Question('Please enter your central.clickatell.com username: ');
+ $username = $helper->ask($input, $output, $usernameQuestion);
+ $passwordQuestion = new Question('Please enter your central.clickatell.com password: ');
+ $password = $helper->ask($input, $output, $passwordQuestion);
+
+ $providerConfig->setApi($api);
+ $providerConfig->setUser($username);
+ $providerConfig->setPassword($password);
+ break;
default:
$output->writeln("Invalid provider $provider");
diff --git a/lib/Service/Gateway/SMS/Provider/ClickatellCentral.php b/lib/Service/Gateway/SMS/Provider/ClickatellCentral.php
new file mode 100644
index 0000000..e826988
--- /dev/null
+++ b/lib/Service/Gateway/SMS/Provider/ClickatellCentral.php
@@ -0,0 +1,75 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Christian Schrötter <cs@fnx.li>
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * 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;
+
+class ClickatellCentral implements IProvider {
+
+ const PROVIDER_ID = 'clickatellcentral';
+
+ /** @var IClient */
+ private $client;
+
+ /** @var ClickatellCentralConfig */
+ private $config;
+
+ public function __construct(IClientService $clientService,
+ ClickatellCentralConfig $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(vsprintf('https://api.clickatell.com/http/sendmsg?user=%s&password=%s&api_id=%u&to=%s&text=%s', [
+ urlencode($config->getUser()),
+ urlencode($config->getPassword()),
+ $config->getApi(),
+ urlencode($identifier),
+ urlencode($message),
+ ]));
+ } catch (Exception $ex) {
+ throw new SmsTransmissionException();
+ }
+ }
+
+ /**
+ * @return ClickatellCentralConfig
+ */
+ public function getConfig(): IProviderConfig {
+ return $this->config;
+ }
+}
diff --git a/lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php b/lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php
new file mode 100644
index 0000000..e788b8e
--- /dev/null
+++ b/lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php
@@ -0,0 +1,83 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Christian Schrötter <cs@fnx.li>
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ * @author André Fondse <andre@hetnetwerk.org>
+ *
+ * Nextcloud - Two-factor Gateway for Telegram
+ *
+ * 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 OCA\TwoFactorGateway\AppInfo\Application;
+use OCA\TwoFactorGateway\Exception\ConfigurationException;
+use OCP\IConfig;
+
+class ClickatellCentralConfig 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 getApi(): string {
+ return $this->getOrFail('clickatell_central_api');
+ }
+
+ public function setApi(string $api) {
+ $this->config->setAppValue(Application::APP_NAME, 'clickatell_central_api', $api);
+ }
+
+ public function getUser(): string {
+ return $this->getOrFail('clickatell_central_user');
+ }
+
+ public function setUser(string $user) {
+ $this->config->setAppValue(Application::APP_NAME, 'clickatell_central_user', $user);
+ }
+
+ public function getPassword(): string {
+ return $this->getOrFail('clickatell_central_password');
+ }
+
+ public function setPassword(string $password) {
+ $this->config->setAppValue(Application::APP_NAME, 'clickatell_central_password', $password);
+ }
+
+ public function isComplete(): bool {
+ $set = $this->config->getAppKeys(Application::APP_NAME);
+ $expected = [
+ 'clickatell_central_api',
+ 'clickatell_central_user',
+ 'clickatell_central_password',
+ ];
+ return count(array_intersect($set, $expected)) === count($expected);
+ }
+
+}
diff --git a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php
index d3f3614..c56213c 100644
--- a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php
+++ b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php
@@ -57,6 +57,8 @@ class ProviderFactory {
return $this->container->query(Ovh::class);
case SpryngSMS::PROVIDER_ID:
return $this->container->query(SpryngSMS::class);
+ case ClickatellCentral::PROVIDER_ID:
+ return $this->container->query(ClickatellCentral::class);
default:
throw new InvalidSmsProviderException("Provider <$id> does not exist");
}