-
Notifications
You must be signed in to change notification settings - Fork 62
Add Plivo as SMS Provider #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 12 commits
dab96f0
6545e7e
2d44d85
8cd0c96
d22c44d
0e99191
83cb8b4
16c9729
209cb8a
f126375
a614378
0e89632
72c1e25
8d4e305
44fd17e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,89 @@ | ||||||||||
| <?php | ||||||||||
|
|
||||||||||
| declare(strict_types=1); | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @author Chris Jenkins <[email protected]> | ||||||||||
| * | ||||||||||
| * 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 <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\ILogger; | ||||||||||
|
|
||||||||||
| class Plivo implements IProvider { | ||||||||||
| public const PROVIDER_ID = 'plivo'; | ||||||||||
|
|
||||||||||
| /** @var IClient */ | ||||||||||
| private $client; | ||||||||||
|
|
||||||||||
| /** @var PlivoConfig */ | ||||||||||
| private $config; | ||||||||||
|
|
||||||||||
| private $logger; | ||||||||||
|
|
||||||||||
| public function __construct(IClientService $clientService, | ||||||||||
| PlivoConfig $config, ILogger $logger) { | ||||||||||
| $this->client = $clientService->newClient(); | ||||||||||
| $this->config = $config; | ||||||||||
| $this->logger = $logger; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @param string $identifier | ||||||||||
| * @param string $message | ||||||||||
| * | ||||||||||
| * @throws SmsTransmissionException | ||||||||||
| */ | ||||||||||
| public function send(string $identifier, string $message) { | ||||||||||
| $config = $this->getConfig(); | ||||||||||
| $authToken = $config->getValue($config::AUTH_TOKEN_KEY); | ||||||||||
| $authID = $config->getValue($config::AUTH_ID_KEY); | ||||||||||
| $srcNumber = $config->getValue($config::SRC_NUMBER_KEY); | ||||||||||
|
|
||||||||||
| $apiParams = [ | ||||||||||
| '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($ex->getMessage()); | ||||||||||
|
||||||||||
| $this->logger->error($ex->getMessage()); | |
| $this->logger->logException($ex, [ | |
| 'message' => 'Could not send Plivo message: ' . $ex->getMessage(), | |
| ]); |
then the logged line will also contain a trace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy that. Given the 2 choices, when do you use error and when do you use logException?
@ChristophWurst @boppy
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @author Chris Jenkins <[email protected]> | ||
| * | ||
| * 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 <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 PlivoConfig implements IProviderConfig { | ||
|
|
||
| /** @var IConfig */ | ||
| private $config; | ||
|
|
||
| public const AUTH_ID_KEY = 'plivo_auth_id'; | ||
| public const AUTH_TOKEN_KEY = 'plivo_auth_token'; | ||
| public const SRC_NUMBER_KEY = 'plivo_src_number'; | ||
|
|
||
| private const EXPECTED_KEYS = [ | ||
| self::AUTH_ID_KEY, | ||
| self::AUTH_TOKEN_KEY, | ||
| 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(self::EXPECTED_KEYS); | ||
| } | ||
|
|
||
| public function remove() { | ||
| foreach (self::EXPECTED_KEYS as $key) { | ||
| $this->config->deleteAppValue(Application::APP_NAME, $key); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.