From dab96f04407649fca90d0eee62d08f2595175e84 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Sat, 30 May 2020 01:19:21 +0000 Subject: [PATCH 01/14] 1st draft impl of Plivo SMS provider --- lib/Service/Gateway/SMS/Provider/Plivo.php | 75 +++++++++++++++++++ .../Gateway/SMS/Provider/PlivoConfig.php | 25 +++++++ .../Gateway/SMS/Provider/ProviderFactory.php | 2 + 3 files changed, 102 insertions(+) create mode 100644 lib/Service/Gateway/SMS/Provider/Plivo.php create mode 100644 lib/Service/Gateway/SMS/Provider/PlivoConfig.php diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php new file mode 100644 index 00000000..641c6dfb --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -0,0 +1,75 @@ + + * + * Plivo - Config for Two-factor Gateway for Plivo + * + * 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 + * + */ + +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 Plivo implements IProvider { + public const PROVIDER_ID = 'plivo'; + + /** @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(); + $apiId = $config->getApiId(); + try { + $this->client->get("https://api.plivo.com/v1/Account/$apiId/Message/", [ + 'body' => [ + 'to' => 'get from phone number of user', + 'src' = 'tbd', + 'txt' = '2fa code', + ] + ]); + } catch (Exception $ex) { + throw new SmsTransmissionException(); + } + } + + /** + * @return ClickSendConfig + */ + public function getConfig(): IProviderConfig { + return $this->config; + } +} diff --git a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php new file mode 100644 index 00000000..391d7fbd --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php @@ -0,0 +1,25 @@ + + * + * Plivo - Config for Two-factor Gateway for Plivo + * + * 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 + * + */ + +namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider; + diff --git a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php index ec499640..6fb5db00 100644 --- a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php +++ b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php @@ -61,6 +61,8 @@ public function getProvider(string $id): IProvider { return $this->container->query(ClickatellCentral::class); case ClickSend::PROVIDER_ID: return $this->container->query(ClickSend::class); + case Plivo::PROVIDER_ID: + return $this->container->query(Plivo::class); default: throw new InvalidSmsProviderException("Provider <$id> does not exist"); } From 6545e7eb9f6b9d2b8f20991c1a6e01779c01f1da Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Sat, 30 May 2020 05:36:38 +0000 Subject: [PATCH 02/14] finish Plivo implementation --- lib/Command/Configure.php | 26 ++++++++- lib/Service/Gateway/SMS/Provider/Plivo.php | 28 ++++++---- .../Gateway/SMS/Provider/PlivoConfig.php | 54 +++++++++++++++++++ 3 files changed, 97 insertions(+), 11 deletions(-) diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index dc7b95f4..5242ea0d 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -35,6 +35,7 @@ use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\Sms77IoConfig; use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\OvhConfig; use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\WebSmsConfig; +use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PlivoConfig; use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PuzzelSMSConfig; use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\HuaweiE3531Config; use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\SpryngSMSConfig; @@ -109,7 +110,7 @@ private function configureSignal(InputInterface $input, OutputInterface $output) 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, clicksend): ', 'websms'); + $providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms, plivo, puzzelsms, ecallsms, voipms, huawei_e3531, spryng, sms77io, ovh, clickatellcentral, clicksend): ', 'websms'); $provider = $helper->ask($input, $output, $providerQuestion); /** @var SMSConfig $config */ @@ -313,6 +314,29 @@ private function configureSms(InputInterface $input, OutputInterface $output) { $providerConfig->setUser($username); $providerConfig->setApiKey($apiKey); + break; + case 'plivo': + $config->setProvider($provider); + /** @var PlivoConfig $providerConfig */ + $providerConfig = $config->getProvider()->getConfig(); + + $authIdQuestion = new Question('Please enter your plivo authentication id (Auth ID): '); + $authId = $helper->ask($input, $output, $usernameQuestion); + + $authTokenQuestion = new Question('Please enter your plivo authentication token (Auth Token): '); + $authToken = $helper->ask($input, $output, $apiKeyQuestion); + + $srcNumberQuestion = new Question('Please enter your plivo phone number (in E.164 format): '); + $srcNumber = $helper->ask($input, $output, $usernameQuestion); + + $callbackUrlQuestion = new Question('Please enter your plivo callback url: '); + $callbackUrl = $helper->ask($input, $output, $apiKeyQuestion); + + $providerConfig->setValue($providerConfig::AUTH_ID_KEY,$authId); + $providerConfig->setValue($providerConfig::AUTH_TOKEN_KEY, $authToken); + $providerConfig->setValue($providerConfig::CALLBACK_URL, $srcNumber); + $providerConfig->setValue($providerConfig::SRC_NUMBER_KEY, $callbackUrl); + break; default: diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index 641c6dfb..2e629ff0 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -34,11 +34,11 @@ class Plivo implements IProvider { /** @var IClient */ private $client; - /** @var ClickSendConfig */ + /** @var PlivoConfig */ private $config; public function __construct(IClientService $clientService, - ClickSendConfig $config) { + PlivoConfig $config) { $this->client = $clientService->newClient(); $this->config = $config; } @@ -51,14 +51,22 @@ public function __construct(IClientService $clientService, */ public function send(string $identifier, string $message) { $config = $this->getConfig(); - $apiKey = $config->getApiKey(); - $apiId = $config->getApiId(); + $authToken = $config->getAuthToken(); + $authID = $config->getAuthID(); + $srcNumber = $config->getSrcNumber(); + $callbackUrl = $config->getCallbackUrl(); + try { - $this->client->get("https://api.plivo.com/v1/Account/$apiId/Message/", [ - 'body' => [ - 'to' => 'get from phone number of user', - 'src' = 'tbd', - 'txt' = '2fa code', + $this->client->get("https://api.plivo.com/v1/Account/$authID/Message/", [ + 'body' => json_encode([ + 'to' => $identifier, + 'src' = $srcNumber, + 'txt' = $message, + 'url' = $callbackUrl + ],JSON_FORCE_OBJECT), + 'headers' => [ + 'Content-Type' => "application/json", + 'Authorization' => "Basic " . base64_encode($authID:$authToken); ] ]); } catch (Exception $ex) { @@ -67,7 +75,7 @@ public function send(string $identifier, string $message) { } /** - * @return ClickSendConfig + * @return PlivoConfig */ public function getConfig(): IProviderConfig { return $this->config; diff --git a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php index 391d7fbd..8a214185 100644 --- a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php +++ b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php @@ -23,3 +23,57 @@ 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 const AUTH_ID_KEY = 'plivo_auth_id'; + public const AUTH_TOKEN_KEY = 'plivo_auth_token'; + public const CALLBACK_URL = 'plivo_callback_url'; + public const SRC_NUMBER_KEY = 'plivo_src_number'; + + private const EXPECTED_KEYS = [ + self::AUTH_ID_KEY, + self::AUTH_TOKEN_KEY, + self::CALLBACK_URL, + self::SRC_NUMBER_KEY + ]; + + public function __construct(IConfig $config) { + $this->config = $config; + } + + private function getInternalValue(string $key): string { + $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + if (is_null($val)) { + throw new ConfigurationException(); + } + return $val; + } + + public function getValue(string $key): string { + return $this->getInternalValue($key); + } + + public function setValue(string $key, string $value) { + $this->config->setAppValue(Application::APP_NAME, $key, $value); + } + + public function isComplete(): bool { + $set = $this->config->getAppKeys(Application::APP_NAME); + return count(array_intersect($set,self::EXPECTED_KEYS)) === count($expected); + } + + public function remove() { + foreach(self::EXPECTED_KEYS as $key) { + $this->config->deleteAppValue(Application::APP_NAME, $key); + } + } +} From 2d44d8525d97809b20837f7771abb56ab774d90f Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Sat, 30 May 2020 05:45:39 +0000 Subject: [PATCH 03/14] fix compilation issues --- lib/Command/Configure.php | 8 ++++---- lib/Service/Gateway/SMS/Provider/Plivo.php | 10 +++++----- lib/Service/Gateway/SMS/Provider/PlivoConfig.php | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index 5242ea0d..4e386ee1 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -321,16 +321,16 @@ private function configureSms(InputInterface $input, OutputInterface $output) { $providerConfig = $config->getProvider()->getConfig(); $authIdQuestion = new Question('Please enter your plivo authentication id (Auth ID): '); - $authId = $helper->ask($input, $output, $usernameQuestion); + $authId = $helper->ask($input, $output, $authIdQuestion); $authTokenQuestion = new Question('Please enter your plivo authentication token (Auth Token): '); - $authToken = $helper->ask($input, $output, $apiKeyQuestion); + $authToken = $helper->ask($input, $output, $authTokenQuestion); $srcNumberQuestion = new Question('Please enter your plivo phone number (in E.164 format): '); - $srcNumber = $helper->ask($input, $output, $usernameQuestion); + $srcNumber = $helper->ask($input, $output, $srcNumberQuestion); $callbackUrlQuestion = new Question('Please enter your plivo callback url: '); - $callbackUrl = $helper->ask($input, $output, $apiKeyQuestion); + $callbackUrl = $helper->ask($input, $output, $callbackUrlQuestion); $providerConfig->setValue($providerConfig::AUTH_ID_KEY,$authId); $providerConfig->setValue($providerConfig::AUTH_TOKEN_KEY, $authToken); diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index 2e629ff0..46d78671 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -43,7 +43,7 @@ public function __construct(IClientService $clientService, $this->config = $config; } - ** + /** * @param string $identifier * @param string $message * @@ -60,13 +60,13 @@ public function send(string $identifier, string $message) { $this->client->get("https://api.plivo.com/v1/Account/$authID/Message/", [ 'body' => json_encode([ 'to' => $identifier, - 'src' = $srcNumber, - 'txt' = $message, - 'url' = $callbackUrl + 'src' => $srcNumber, + 'txt' => $message, + 'url' => $callbackUrl ],JSON_FORCE_OBJECT), 'headers' => [ 'Content-Type' => "application/json", - 'Authorization' => "Basic " . base64_encode($authID:$authToken); + 'Authorization' => "Basic " . base64_encode($authID.':'.$authToken) ] ]); } catch (Exception $ex) { diff --git a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php index 8a214185..db458cbe 100644 --- a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php +++ b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php @@ -29,7 +29,7 @@ use OCA\TwoFactorGateway\Exception\ConfigurationException; use OCP\IConfig; -class ClickSendConfig implements IProviderConfig { +class PlivoConfig implements IProviderConfig { /** @var IConfig */ private $config; From 8cd0c96319da870d1904150ec521907db9efaaa4 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Sat, 30 May 2020 05:46:45 +0000 Subject: [PATCH 04/14] add plivo to documentation --- doc/Admin Documentation.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/Admin Documentation.md b/doc/Admin Documentation.md index 7be6b955..2af0f9b1 100644 --- a/doc/Admin Documentation.md +++ b/doc/Admin Documentation.md @@ -191,4 +191,15 @@ Interactive admin configuration: occ twofactorauth:gateway:configure sms ``` +### Plivo +URL: https://www.plivo.com +Stability: Experimental + +Use the HTTPS service provided by clicksend.com for sending SMS. + +Interactive admin configuration: +```bash +occ twofactorauth:gateway:configure sms +``` + [User Documentation]: https://nextcloud-twofactor-gateway.readthedocs.io/en/latest/User%20Documentation/ From d22c44de03fa157a6bcdccd6ce255d92ee2e75a3 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Sun, 31 May 2020 21:23:48 +0000 Subject: [PATCH 05/14] fix indentation. sort providers alphabetically. --- doc/Admin Documentation.md | 2 +- lib/Command/Configure.php | 12 +++++---- lib/Service/Gateway/SMS/Provider/Plivo.php | 10 +++---- .../Gateway/SMS/Provider/PlivoConfig.php | 27 +++++++++---------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/doc/Admin Documentation.md b/doc/Admin Documentation.md index 2af0f9b1..a5c567c0 100644 --- a/doc/Admin Documentation.md +++ b/doc/Admin Documentation.md @@ -195,7 +195,7 @@ occ twofactorauth:gateway:configure sms URL: https://www.plivo.com Stability: Experimental -Use the HTTPS service provided by clicksend.com for sending SMS. +Use the HTTPS service provided by plivo.com for sending SMS. Interactive admin configuration: ```bash diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index 4e386ee1..5b8f7c3b 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -109,8 +109,10 @@ private function configureSignal(InputInterface $input, OutputInterface $output) private function configureSms(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); - - $providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms, plivo, puzzelsms, ecallsms, voipms, huawei_e3531, spryng, sms77io, ovh, clickatellcentral, clicksend): ', 'websms'); + $providerArray = ['websms', 'playsms', 'clockworksms', 'puzzelsms', 'ecallsms', 'voipms', 'huawei_e3531', 'spryng', 'sms77io', 'ovh', 'clickatellcentral', 'clicksend', 'plivo']; + sort($providerArray); + $strOfProviders = implode(',',$providerArray); + $providerQuestion = new Question('Please choose a SMS provider ('.$strOfProviders.'): ', 'websms'); $provider = $helper->ask($input, $output, $providerQuestion); /** @var SMSConfig $config */ @@ -326,7 +328,7 @@ private function configureSms(InputInterface $input, OutputInterface $output) { $authTokenQuestion = new Question('Please enter your plivo authentication token (Auth Token): '); $authToken = $helper->ask($input, $output, $authTokenQuestion); - $srcNumberQuestion = new Question('Please enter your plivo phone number (in E.164 format): '); + $srcNumberQuestion = new Question("Please enter your plivo phone number (in E.164 format '+12345678901'): "); $srcNumber = $helper->ask($input, $output, $srcNumberQuestion); $callbackUrlQuestion = new Question('Please enter your plivo callback url: '); @@ -334,8 +336,8 @@ private function configureSms(InputInterface $input, OutputInterface $output) { $providerConfig->setValue($providerConfig::AUTH_ID_KEY,$authId); $providerConfig->setValue($providerConfig::AUTH_TOKEN_KEY, $authToken); - $providerConfig->setValue($providerConfig::CALLBACK_URL, $srcNumber); - $providerConfig->setValue($providerConfig::SRC_NUMBER_KEY, $callbackUrl); + $providerConfig->setValue($providerConfig::CALLBACK_URL, $callbackUrl); + $providerConfig->setValue($providerConfig::SRC_NUMBER_KEY, $srcNumber); break; diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index 46d78671..ba924a77 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -59,11 +59,11 @@ public function send(string $identifier, string $message) { try { $this->client->get("https://api.plivo.com/v1/Account/$authID/Message/", [ 'body' => json_encode([ - 'to' => $identifier, - 'src' => $srcNumber, - 'txt' => $message, - 'url' => $callbackUrl - ],JSON_FORCE_OBJECT), + 'to' => $identifier, + 'src' => $srcNumber, + 'txt' => $message, + 'url' => $callbackUrl + ],JSON_FORCE_OBJECT), 'headers' => [ 'Content-Type' => "application/json", 'Authorization' => "Basic " . base64_encode($authID.':'.$authToken) diff --git a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php index db458cbe..0d151c4f 100644 --- a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php +++ b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php @@ -23,7 +23,6 @@ namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider; - use function array_intersect; use OCA\TwoFactorGateway\AppInfo\Application; use OCA\TwoFactorGateway\Exception\ConfigurationException; @@ -40,11 +39,11 @@ class PlivoConfig implements IProviderConfig { public const SRC_NUMBER_KEY = 'plivo_src_number'; private const EXPECTED_KEYS = [ - self::AUTH_ID_KEY, - self::AUTH_TOKEN_KEY, - self::CALLBACK_URL, - self::SRC_NUMBER_KEY - ]; + self::AUTH_ID_KEY, + self::AUTH_TOKEN_KEY, + self::CALLBACK_URL, + self::SRC_NUMBER_KEY + ]; public function __construct(IConfig $config) { $this->config = $config; @@ -59,20 +58,20 @@ private function getInternalValue(string $key): string { } public function getValue(string $key): string { - return $this->getInternalValue($key); - } + return $this->getInternalValue($key); + } - public function setValue(string $key, string $value) { - $this->config->setAppValue(Application::APP_NAME, $key, $value); - } - - public function isComplete(): bool { + public function setValue(string $key, string $value) { + $this->config->setAppValue(Application::APP_NAME, $key, $value); + } + + public function isComplete(): bool { $set = $this->config->getAppKeys(Application::APP_NAME); return count(array_intersect($set,self::EXPECTED_KEYS)) === count($expected); } public function remove() { - foreach(self::EXPECTED_KEYS as $key) { + foreach (self::EXPECTED_KEYS as $key) { $this->config->deleteAppValue(Application::APP_NAME, $key); } } From 0e9919184425c57d6935c95fa5803d8d89286b43 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Mon, 1 Jun 2020 06:12:44 +0000 Subject: [PATCH 06/14] improve use of variable inside string --- lib/Command/Configure.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index 5b8f7c3b..65fc5c26 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -110,9 +110,9 @@ private function configureSignal(InputInterface $input, OutputInterface $output) private function configureSms(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); $providerArray = ['websms', 'playsms', 'clockworksms', 'puzzelsms', 'ecallsms', 'voipms', 'huawei_e3531', 'spryng', 'sms77io', 'ovh', 'clickatellcentral', 'clicksend', 'plivo']; - sort($providerArray); - $strOfProviders = implode(',',$providerArray); - $providerQuestion = new Question('Please choose a SMS provider ('.$strOfProviders.'): ', 'websms'); + sort($providerArray,SORT_STRING); + $strOfProviders = implode(',', $providerArray); + $providerQuestion = new Question("Please choose a SMS provider ($strOfProviders): ", 'websms'); $provider = $helper->ask($input, $output, $providerQuestion); /** @var SMSConfig $config */ From 83cb8b4a180580b178b08e6a055cdc83c1794c67 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Wed, 3 Jun 2020 00:57:49 +0000 Subject: [PATCH 07/14] fix bug. referenced const array incorrectly --- lib/Service/Gateway/SMS/Provider/PlivoConfig.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php index 0d151c4f..f8287ca1 100644 --- a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php +++ b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php @@ -67,7 +67,7 @@ public function setValue(string $key, string $value) { public function isComplete(): bool { $set = $this->config->getAppKeys(Application::APP_NAME); - return count(array_intersect($set,self::EXPECTED_KEYS)) === count($expected); + return count(array_intersect($set,self::EXPECTED_KEYS)) === count(self::EXPECTED_KEYS); } public function remove() { From 16c9729d2d4a451958d8f51a180807856272b1a8 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Wed, 3 Jun 2020 01:38:19 +0000 Subject: [PATCH 08/14] fix bug. calling wrong functions --- lib/Service/Gateway/SMS/Provider/Plivo.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index ba924a77..292f09fd 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -51,10 +51,10 @@ public function __construct(IClientService $clientService, */ public function send(string $identifier, string $message) { $config = $this->getConfig(); - $authToken = $config->getAuthToken(); - $authID = $config->getAuthID(); - $srcNumber = $config->getSrcNumber(); - $callbackUrl = $config->getCallbackUrl(); + $authToken = $config->getValue($config::AUTH_TOKEN_KEY); + $authID = $config->getValue($config::AUTH_ID_KEY); + $srcNumber = $config->getValue($config::SRC_NUMBER_KEY); + $callbackUrl = $config->getValue($config::CALLBACK_URL); try { $this->client->get("https://api.plivo.com/v1/Account/$authID/Message/", [ From 209cb8a5e72f1a1689d2c774341839eda25d488d Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Wed, 3 Jun 2020 02:41:22 +0000 Subject: [PATCH 09/14] add logger. post doesn't work, but get does on IClient --- lib/Service/Gateway/SMS/Provider/Plivo.php | 34 +++++++++++++--------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index 292f09fd..b0fa8320 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -27,6 +27,7 @@ use OCA\TwoFactorGateway\Exception\SmsTransmissionException; use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; +use \OCP\ILogger; class Plivo implements IProvider { public const PROVIDER_ID = 'plivo'; @@ -36,11 +37,14 @@ class Plivo implements IProvider { /** @var PlivoConfig */ private $config; + + private $logger; public function __construct(IClientService $clientService, - PlivoConfig $config) { + PlivoConfig $config, ILogger $logger) { $this->client = $clientService->newClient(); $this->config = $config; + $this->logger = $logger; } /** @@ -56,19 +60,23 @@ public function send(string $identifier, string $message) { $srcNumber = $config->getValue($config::SRC_NUMBER_KEY); $callbackUrl = $config->getValue($config::CALLBACK_URL); + $apiParams = [ + 'body' => json_encode([ + 'dst' => $identifier, + 'src' => $srcNumber, + 'text' => $message, + 'url' => $callbackUrl + ],JSON_FORCE_OBJECT), + 'headers' => [ + 'Content-Type' => "application/json", + 'Authorization' => "Basic " . base64_encode($authID.':'.$authToken) + ], + 'debug' => true + ]; + try { - $this->client->get("https://api.plivo.com/v1/Account/$authID/Message/", [ - 'body' => json_encode([ - 'to' => $identifier, - 'src' => $srcNumber, - 'txt' => $message, - 'url' => $callbackUrl - ],JSON_FORCE_OBJECT), - 'headers' => [ - 'Content-Type' => "application/json", - 'Authorization' => "Basic " . base64_encode($authID.':'.$authToken) - ] - ]); + $this->logger->error("api call: https://api.plivo.com/v1/Account/$authID/Message/" .print_r($apiParams,true)); + $this->client->post("https://api.plivo.com/v1/Account/$authID/Message/", $apiParams); } catch (Exception $ex) { throw new SmsTransmissionException(); } From f1263750349b0ef0ecdc2b4dabc66b0204dae94e Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Wed, 3 Jun 2020 03:14:03 +0000 Subject: [PATCH 10/14] fix bug. don't pass 'url' param to plivo unless you have a valid url --- lib/Service/Gateway/SMS/Provider/Plivo.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index b0fa8320..4cde2bd1 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -64,8 +64,7 @@ public function send(string $identifier, string $message) { 'body' => json_encode([ 'dst' => $identifier, 'src' => $srcNumber, - 'text' => $message, - 'url' => $callbackUrl + 'text' => $message ],JSON_FORCE_OBJECT), 'headers' => [ 'Content-Type' => "application/json", @@ -75,9 +74,10 @@ public function send(string $identifier, string $message) { ]; try { - $this->logger->error("api call: https://api.plivo.com/v1/Account/$authID/Message/" .print_r($apiParams,true)); $this->client->post("https://api.plivo.com/v1/Account/$authID/Message/", $apiParams); } catch (Exception $ex) { + $this->logger->error("api call: https://api.plivo.com/v1/Account/$authID/Message/" .print_r($apiParams,true)); + $this->logger->error($ex->getMessage()); throw new SmsTransmissionException(); } } From a614378c9f1eca32c12b5cef169edceefa56bd12 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Wed, 3 Jun 2020 09:53:56 +0000 Subject: [PATCH 11/14] remove debug flag. successfully receive 2fa code over sms from plivo --- lib/Service/Gateway/SMS/Provider/Plivo.php | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index 4cde2bd1..42eae49d 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -61,22 +61,21 @@ public function send(string $identifier, string $message) { $callbackUrl = $config->getValue($config::CALLBACK_URL); $apiParams = [ - 'body' => json_encode([ - 'dst' => $identifier, - 'src' => $srcNumber, - 'text' => $message - ],JSON_FORCE_OBJECT), - 'headers' => [ - 'Content-Type' => "application/json", - 'Authorization' => "Basic " . base64_encode($authID.':'.$authToken) - ], - 'debug' => true - ]; + 'body' => json_encode([ + 'dst' => $identifier, + 'src' => $srcNumber, + 'text' => $message + ],JSON_FORCE_OBJECT), + 'headers' => [ + 'Content-Type' => "application/json", + 'Authorization' => "Basic " . base64_encode($authID.':'.$authToken) + ] + ]; try { + $this->logger->debug("api call: https://api.plivo.com/v1/Account/$authID/Message/" .print_r($apiParams,true)); $this->client->post("https://api.plivo.com/v1/Account/$authID/Message/", $apiParams); } catch (Exception $ex) { - $this->logger->error("api call: https://api.plivo.com/v1/Account/$authID/Message/" .print_r($apiParams,true)); $this->logger->error($ex->getMessage()); throw new SmsTransmissionException(); } From 0e89632ebd6d150eb59ff2cf155e9eb405e75aa5 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Fri, 12 Jun 2020 18:58:41 +0000 Subject: [PATCH 12/14] remove callback url to reduce complexity of app --- lib/Service/Gateway/SMS/Provider/Plivo.php | 1 - lib/Service/Gateway/SMS/Provider/PlivoConfig.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index 42eae49d..d8435d13 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -58,7 +58,6 @@ public function send(string $identifier, string $message) { $authToken = $config->getValue($config::AUTH_TOKEN_KEY); $authID = $config->getValue($config::AUTH_ID_KEY); $srcNumber = $config->getValue($config::SRC_NUMBER_KEY); - $callbackUrl = $config->getValue($config::CALLBACK_URL); $apiParams = [ 'body' => json_encode([ diff --git a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php index f8287ca1..83f2af3f 100644 --- a/lib/Service/Gateway/SMS/Provider/PlivoConfig.php +++ b/lib/Service/Gateway/SMS/Provider/PlivoConfig.php @@ -35,13 +35,11 @@ class PlivoConfig implements IProviderConfig { public const AUTH_ID_KEY = 'plivo_auth_id'; public const AUTH_TOKEN_KEY = 'plivo_auth_token'; - public const CALLBACK_URL = 'plivo_callback_url'; public const SRC_NUMBER_KEY = 'plivo_src_number'; private const EXPECTED_KEYS = [ self::AUTH_ID_KEY, self::AUTH_TOKEN_KEY, - self::CALLBACK_URL, self::SRC_NUMBER_KEY ]; From 72c1e256d5c1a80fbe2a3129e0f408904a592b9d Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Tue, 16 Jun 2020 23:02:38 +0000 Subject: [PATCH 13/14] use class constants with clas name. improve logging. use multi-line array --- lib/Command/Configure.php | 23 +++++++++++++++++----- lib/Service/Gateway/SMS/Provider/Plivo.php | 4 +++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index 65fc5c26..8fab952d 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -109,7 +109,20 @@ private function configureSignal(InputInterface $input, OutputInterface $output) private function configureSms(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); - $providerArray = ['websms', 'playsms', 'clockworksms', 'puzzelsms', 'ecallsms', 'voipms', 'huawei_e3531', 'spryng', 'sms77io', 'ovh', 'clickatellcentral', 'clicksend', 'plivo']; + $providerArray = ['websms', + 'playsms', + 'clockworksms', + 'puzzelsms', + 'ecallsms', + 'voipms', + 'huawei_e3531', + 'spryng', + 'sms77io', + 'ovh', + 'clickatellcentral', + 'clicksend', + 'plivo' + ]; sort($providerArray,SORT_STRING); $strOfProviders = implode(',', $providerArray); $providerQuestion = new Question("Please choose a SMS provider ($strOfProviders): ", 'websms'); @@ -334,10 +347,10 @@ private function configureSms(InputInterface $input, OutputInterface $output) { $callbackUrlQuestion = new Question('Please enter your plivo callback url: '); $callbackUrl = $helper->ask($input, $output, $callbackUrlQuestion); - $providerConfig->setValue($providerConfig::AUTH_ID_KEY,$authId); - $providerConfig->setValue($providerConfig::AUTH_TOKEN_KEY, $authToken); - $providerConfig->setValue($providerConfig::CALLBACK_URL, $callbackUrl); - $providerConfig->setValue($providerConfig::SRC_NUMBER_KEY, $srcNumber); + $providerConfig->setValue(PlivoConfig::AUTH_ID_KEY,$authId); + $providerConfig->setValue(PlivoConfig::AUTH_TOKEN_KEY, $authToken); + $providerConfig->setValue(PlivoConfig::CALLBACK_URL, $callbackUrl); + $providerConfig->setValue(PlivoConfig::SRC_NUMBER_KEY, $srcNumber); break; diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index d8435d13..d0e7ee17 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -75,7 +75,9 @@ public function send(string $identifier, string $message) { $this->logger->debug("api call: https://api.plivo.com/v1/Account/$authID/Message/" .print_r($apiParams,true)); $this->client->post("https://api.plivo.com/v1/Account/$authID/Message/", $apiParams); } catch (Exception $ex) { - $this->logger->error($ex->getMessage()); + $this->logger->logException($ex, [ + 'message' => 'Could not send Plivo message: ' . $ex->getMessage(), + ]); throw new SmsTransmissionException(); } } From 8d4e3054bc30e31bbf4e405bfc0a302d4d4b00d9 Mon Sep 17 00:00:00 2001 From: Chris Jenkins Date: Tue, 16 Jun 2020 23:11:39 +0000 Subject: [PATCH 14/14] fix php formatting --- lib/Command/Configure.php | 28 +++++++++++----------- lib/Service/Gateway/SMS/Provider/Plivo.php | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/Command/Configure.php b/lib/Command/Configure.php index 8fab952d..b72f63a4 100644 --- a/lib/Command/Configure.php +++ b/lib/Command/Configure.php @@ -109,20 +109,20 @@ private function configureSignal(InputInterface $input, OutputInterface $output) private function configureSms(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); - $providerArray = ['websms', - 'playsms', - 'clockworksms', - 'puzzelsms', - 'ecallsms', - 'voipms', - 'huawei_e3531', - 'spryng', - 'sms77io', - 'ovh', - 'clickatellcentral', - 'clicksend', - 'plivo' - ]; + $providerArray = ['websms', + 'playsms', + 'clockworksms', + 'puzzelsms', + 'ecallsms', + 'voipms', + 'huawei_e3531', + 'spryng', + 'sms77io', + 'ovh', + 'clickatellcentral', + 'clicksend', + 'plivo' + ]; sort($providerArray,SORT_STRING); $strOfProviders = implode(',', $providerArray); $providerQuestion = new Question("Please choose a SMS provider ($strOfProviders): ", 'websms'); diff --git a/lib/Service/Gateway/SMS/Provider/Plivo.php b/lib/Service/Gateway/SMS/Provider/Plivo.php index d0e7ee17..71aa3517 100644 --- a/lib/Service/Gateway/SMS/Provider/Plivo.php +++ b/lib/Service/Gateway/SMS/Provider/Plivo.php @@ -76,7 +76,7 @@ public function send(string $identifier, string $message) { $this->client->post("https://api.plivo.com/v1/Account/$authID/Message/", $apiParams); } catch (Exception $ex) { $this->logger->logException($ex, [ - 'message' => 'Could not send Plivo message: ' . $ex->getMessage(), + 'message' => 'Could not send Plivo message: ' . $ex->getMessage(), ]); throw new SmsTransmissionException(); }