diff options
author | Ruben de Wit <ruben.dewit@gmx.com> | 2019-05-18 17:35:09 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-12-10 16:04:08 +0300 |
commit | f6be6ad71a61bfe3d29ef9733443f9f2119867ec (patch) | |
tree | ceac7761d61acdf0ab0db563b6c92f439a53e993 /lib | |
parent | 167efc97d908de214fb7088c9d32d355e30c9bf1 (diff) |
* Added Spryng as SMS provider
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/Configure.php | 15 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/ProviderFactory.php | 2 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/SpryngSMS.php | 87 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/SpryngSMSConfig.php | 63 |
4 files changed, 166 insertions, 1 deletions
diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index 212a75f..b70ae89 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -34,6 +34,7 @@ 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\SMS\Provider\HuaweiE3531Config; +use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\SpryngSMSConfig; use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway; use OCA\TwoFactorGateway\Service\Gateway\Telegram\GatewayConfig as TelegramConfig; use Symfony\Component\Console\Command\Command; @@ -103,7 +104,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): ', 'websms'); + $providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms, puzzelsms, ecallsms, voipms, huawei_e3531, spryng): ', 'websms'); $provider = $helper->ask($input, $output, $providerQuestion); /** @var SMSConfig $config */ @@ -222,6 +223,18 @@ class Configure extends Command { $providerConfig->setUrl($url); break; + case 'spryng': + $config->setProvider($provider); + /** @var SpryngSMSConfig $providerConfig */ + $providerConfig = $config->getProvider()->getConfig(); + + $apitokenQuestion = new Question('Please enter your Spryng api token: '); + $apitoken = $helper->ask($input, $output, $apitokenQuestion); + + $providerConfig->setApiToken($apitoken); + + break; + default: $output->writeln("Invalid provider $provider"); break; diff --git a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php index db11057..0c590b0 100644 --- a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php +++ b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php @@ -51,6 +51,8 @@ class ProviderFactory { return $this->container->query(VoipMs::class); case HuaweiE3531::PROVIDER_ID: return $this->container->query(HuaweiE3531::class); + case SpryngSMS::PROVIDER_ID: + return $this->container->query(SpryngSMS::class); default: throw new InvalidSmsProviderException("Provider <$id> does not exist"); } diff --git a/lib/Service/Gateway/SMS/Provider/SpryngSMS.php b/lib/Service/Gateway/SMS/Provider/SpryngSMS.php new file mode 100644 index 0000000..2808197 --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/SpryngSMS.php @@ -0,0 +1,87 @@ +<?php + +declare(strict_types=1); + +/** + * @author Ruben de Wit <ruben@iamit.nl> + * + * 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 SpryngSMS implements IProvider { + + const PROVIDER_ID = 'spryng'; + + /** @var IClient */ + private $client; + + /** @var SpryngSMSConfig */ + private $config; + + public function __construct(IClientService $clientService, + SpryngSMSConfig $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(); + /** @var SpryngSMSConfig $providerConfig */ + try { + $response = $this->client->post( + 'https://rest.spryngsms.com/v1/messages?with%5B%5D=recipients', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $config->getApiToken(), + 'Content-Type' => 'application/json', + ], + 'json' => [ + 'body' => $message, + 'encoding' => 'plain', + 'originator' => 'Nextcloud', + 'recipients' => [$identifier], + 'route' => '1', + ], + ] + ); + } catch (Exception $ex) { + throw new SmsTransmissionException(); + } + } + + /** + * @return SpryngSMSConfig + */ + public function getConfig(): IProviderConfig { + return $this->config; + } + +} diff --git a/lib/Service/Gateway/SMS/Provider/SpryngSMSConfig.php b/lib/Service/Gateway/SMS/Provider/SpryngSMSConfig.php new file mode 100644 index 0000000..2f31a95 --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/SpryngSMSConfig.php @@ -0,0 +1,63 @@ +<?php + +declare(strict_types=1); + +/** + * @author Ruben de Wit <ruben@iamit.nl> + * + * 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 SpryngSMSConfig 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 ($val === null) { + throw new ConfigurationException(); + } + return $val; + } + + public function getApiToken(): string { + return $this->getOrFail('spryng_apitoken'); + } + + public function setApiToken(string $user) { + $this->config->setAppValue(Application::APP_NAME, 'spryng_apitoken', $user); + } + + public function isComplete(): bool { + $set = $this->config->getAppKeys(Application::APP_NAME); + $expected = [ + 'spryng_apitoken' + ]; + return count(array_intersect($set, $expected)) === count($expected); + } +} |