diff options
author | Henning Bopp <henning.bopp@gmail.com> | 2020-05-09 20:19:25 +0300 |
---|---|---|
committer | Henning Bopp <henning.bopp@gmail.com> | 2020-05-09 20:19:25 +0300 |
commit | b3531211c77215e9173bf0382ebe7667fca22044 (patch) | |
tree | 3027c7d27d010fbc4430086fa20f9466ea4df9a8 /lib | |
parent | 09c95d2f7a4ebd6c3ffde98877c532763791f831 (diff) |
Add ClickSend.com SMS Gateway
Signed-off-by: Henning Bopp <henning.bopp@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/Configure.php | 18 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/ClickSend.php | 78 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/ClickSendConfig.php | 72 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/ProviderFactory.php | 2 |
4 files changed, 169 insertions, 1 deletions
diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index 6d07fa3..dc7b95f 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -28,6 +28,7 @@ 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\ClickSendConfig; use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\ClockworkSMSConfig; use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\EcallSMSConfig; use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PlaySMSConfig; @@ -108,7 +109,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, clickatellcentral', 'websms'); + $providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms, puzzelsms, ecallsms, voipms, huawei_e3531, spryng, sms77io, ovh, clickatellcentral, clicksend): ', 'websms'); $provider = $helper->ask($input, $output, $providerQuestion); /** @var SMSConfig $config */ @@ -299,6 +300,21 @@ class Configure extends Command { $providerConfig->setPassword($password); break; + case 'clicksend': + $config->setProvider($provider); + /** @var ClickSendConfig $providerConfig */ + $providerConfig = $config->getProvider()->getConfig(); + + $usernameQuestion = new Question('Please enter your clicksend.com username: '); + $username = $helper->ask($input, $output, $usernameQuestion); + $apiKeyQuestion = new Question('Please enter your clicksend.com API Key (or, if subuser, the password): '); + $apiKey = $helper->ask($input, $output, $apiKeyQuestion); + + $providerConfig->setUser($username); + $providerConfig->setApiKey($apiKey); + + break; + default: $output->writeln("Invalid provider $provider"); break; diff --git a/lib/Service/Gateway/SMS/Provider/ClickSend.php b/lib/Service/Gateway/SMS/Provider/ClickSend.php new file mode 100644 index 0000000..b44000d --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/ClickSend.php @@ -0,0 +1,78 @@ +<?php + +declare(strict_types=1); + +/** + * @author Henning Bopp <henning.bopp@gmail.com> + * + * ClickSend - Two-factor Gateway for ClickSend + * + * 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 ClickSend implements IProvider { + public const PROVIDER_ID = 'clicksend'; + + /** @var IClient */ + private $client; + + /** @var ClickSendConfig */ + private $config; + + public function __construct(IClientService $clientService, + ClickSendConfig $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(); + $apiKey = $config->getApiKey(); + $username = $config->getUser(); + try { + $this->client->get('https://api-mapper.clicksend.com/http/v2/send.php', [ + 'query' => [ + 'method' => 'http', + 'username' => $username, + 'key' => $apiKey, + 'to' => $identifier, + 'message' => $message, + 'senderid' => 'nextcloud' + ], + ]); + } catch (Exception $ex) { + throw new SmsTransmissionException(); + } + } + + /** + * @return ClickSendConfig + */ + public function getConfig(): IProviderConfig { + return $this->config; + } +} diff --git a/lib/Service/Gateway/SMS/Provider/ClickSendConfig.php b/lib/Service/Gateway/SMS/Provider/ClickSendConfig.php new file mode 100644 index 0000000..31dd0a0 --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/ClickSendConfig.php @@ -0,0 +1,72 @@ +<?php + +declare(strict_types=1); + +/** + * @author Henning Bopp <henning.bopp@gmail.com> + * + * ClickSend - Config for Two-factor Gateway for ClickSend + * + * 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 ClickSendConfig 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 getUser(): string { + return $this->getOrFail('clicksend_user'); + } + + public function setUser(string $user) { + $this->config->setAppValue(Application::APP_NAME, 'clicksend_user', $user); + } + + public function getApiKey(): string { + return $this->getOrFail('clicksend_apikey'); + } + + public function setApiKey(string $password) { + $this->config->setAppValue(Application::APP_NAME, 'clicksend_apikey', $password); + } + + public function isComplete(): bool { + $set = $this->config->getAppKeys(Application::APP_NAME); + $expected = [ + 'clicksend_user', + 'clicksend_apikey', + ]; + 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 63bc239..ec49964 100644 --- a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php +++ b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php @@ -59,6 +59,8 @@ class ProviderFactory { return $this->container->query(SpryngSMS::class); case ClickatellCentral::PROVIDER_ID: return $this->container->query(ClickatellCentral::class); + case ClickSend::PROVIDER_ID: + return $this->container->query(ClickSend::class); default: throw new InvalidSmsProviderException("Provider <$id> does not exist"); } |