diff options
author | Vitor Mattos <vitor@php.rio> | 2022-05-06 17:43:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-06 17:43:12 +0300 |
commit | 993fcdb258588631d47a6173914f0da2f3834626 (patch) | |
tree | 1d39a3642845d389de7fd0418cf535d31a872bc2 /lib/Service | |
parent | f465ef41b2f738d0c357349685bfeec59c370020 (diff) | |
parent | e09474ac62c83de7602279fb42deac7b91a6f420 (diff) |
Merge branch 'master' into master
Diffstat (limited to 'lib/Service')
23 files changed, 297 insertions, 107 deletions
diff --git a/lib/Service/Gateway/SMS/GatewayConfig.php b/lib/Service/Gateway/SMS/GatewayConfig.php index 83c5371..84a7ca9 100644 --- a/lib/Service/Gateway/SMS/GatewayConfig.php +++ b/lib/Service/Gateway/SMS/GatewayConfig.php @@ -45,7 +45,7 @@ class GatewayConfig implements IGatewayConfig { } public function getProvider(): IProvider { - $providerName = $this->config->getAppValue(Application::APP_NAME, 'sms_provider_name'); + $providerName = $this->config->getAppValue(Application::APP_ID, 'sms_provider_name'); if ($providerName === '') { throw new ConfigurationException(); } @@ -54,7 +54,7 @@ class GatewayConfig implements IGatewayConfig { } public function setProvider(string $provider) { - $this->config->setAppValue(Application::APP_NAME, 'sms_provider_name', $provider); + $this->config->setAppValue(Application::APP_ID, 'sms_provider_name', $provider); } public function isComplete(): bool { diff --git a/lib/Service/Gateway/SMS/Provider/ClickSendConfig.php b/lib/Service/Gateway/SMS/Provider/ClickSendConfig.php index 14523f8..95cf31d 100644 --- a/lib/Service/Gateway/SMS/Provider/ClickSendConfig.php +++ b/lib/Service/Gateway/SMS/Provider/ClickSendConfig.php @@ -42,7 +42,7 @@ class ClickSendConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -54,7 +54,7 @@ class ClickSendConfig implements IProviderConfig { } public function setUser(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'clicksend_user', $user); + $this->config->setAppValue(Application::APP_ID, 'clicksend_user', $user); } public function getApiKey(): string { @@ -62,17 +62,17 @@ class ClickSendConfig implements IProviderConfig { } public function setApiKey(string $password) { - $this->config->setAppValue(Application::APP_NAME, 'clicksend_apikey', $password); + $this->config->setAppValue(Application::APP_ID, 'clicksend_apikey', $password); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php b/lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php index c1cdd59..6310feb 100644 --- a/lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php +++ b/lib/Service/Gateway/SMS/Provider/ClickatellCentralConfig.php @@ -44,7 +44,7 @@ class ClickatellCentralConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -56,7 +56,7 @@ class ClickatellCentralConfig implements IProviderConfig { } public function setApi(string $api) { - $this->config->setAppValue(Application::APP_NAME, 'clickatell_central_api', $api); + $this->config->setAppValue(Application::APP_ID, 'clickatell_central_api', $api); } public function getUser(): string { @@ -64,7 +64,7 @@ class ClickatellCentralConfig implements IProviderConfig { } public function setUser(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'clickatell_central_user', $user); + $this->config->setAppValue(Application::APP_ID, 'clickatell_central_user', $user); } public function getPassword(): string { @@ -72,17 +72,17 @@ class ClickatellCentralConfig implements IProviderConfig { } public function setPassword(string $password) { - $this->config->setAppValue(Application::APP_NAME, 'clickatell_central_password', $password); + $this->config->setAppValue(Application::APP_ID, 'clickatell_central_password', $password); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/ClockworkSMSConfig.php b/lib/Service/Gateway/SMS/Provider/ClockworkSMSConfig.php index 955aa24..7eb46fe 100644 --- a/lib/Service/Gateway/SMS/Provider/ClockworkSMSConfig.php +++ b/lib/Service/Gateway/SMS/Provider/ClockworkSMSConfig.php @@ -41,7 +41,7 @@ class ClockworkSMSConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -53,17 +53,17 @@ class ClockworkSMSConfig implements IProviderConfig { } public function setApiToken(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'clockworksms_apitoken', $user); + $this->config->setAppValue(Application::APP_ID, 'clockworksms_apitoken', $user); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/EcallSMSConfig.php b/lib/Service/Gateway/SMS/Provider/EcallSMSConfig.php index e11d28b..c289501 100644 --- a/lib/Service/Gateway/SMS/Provider/EcallSMSConfig.php +++ b/lib/Service/Gateway/SMS/Provider/EcallSMSConfig.php @@ -43,7 +43,7 @@ class EcallSMSConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -55,7 +55,7 @@ class EcallSMSConfig implements IProviderConfig { } public function setUser(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'ecallsms_username', $user); + $this->config->setAppValue(Application::APP_ID, 'ecallsms_username', $user); } public function getPassword(): string { @@ -63,7 +63,7 @@ class EcallSMSConfig implements IProviderConfig { } public function setPassword(string $password) { - $this->config->setAppValue(Application::APP_NAME, 'ecallsms_password', $password); + $this->config->setAppValue(Application::APP_ID, 'ecallsms_password', $password); } public function getSenderId(): string { @@ -71,17 +71,17 @@ class EcallSMSConfig implements IProviderConfig { } public function setSenderId(string $senderid) { - $this->config->setAppValue(Application::APP_NAME, 'ecallsms_senderid', $senderid); + $this->config->setAppValue(Application::APP_ID, 'ecallsms_senderid', $senderid); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php b/lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php index efafa88..7020667 100644 --- a/lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php +++ b/lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php @@ -41,7 +41,7 @@ class HuaweiE3531Config implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -53,17 +53,17 @@ class HuaweiE3531Config implements IProviderConfig { } public function setUrl(string $url) { - $this->config->setAppValue(Application::APP_NAME, 'huawei_e3531_api', $url); + $this->config->setAppValue(Application::APP_ID, 'huawei_e3531_api', $url); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/Ovh.php b/lib/Service/Gateway/SMS/Provider/Ovh.php index 4149f72..68dca01 100644 --- a/lib/Service/Gateway/SMS/Provider/Ovh.php +++ b/lib/Service/Gateway/SMS/Provider/Ovh.php @@ -95,11 +95,11 @@ class Ovh implements IProvider { $this->getTimeDelta(); - $header = $this->getHeader('GET',$this->attrs['endpoint'].'/sms'); - $response = $this->client->get($this->attrs['endpoint'].'/sms',[ + $header = $this->getHeader('GET', $this->attrs['endpoint'].'/sms'); + $response = $this->client->get($this->attrs['endpoint'].'/sms', [ 'headers' => $header, ]); - $smsServices = json_decode($response->getBody(),true); + $smsServices = json_decode($response->getBody(), true); $smsAccountFound = false; foreach ($smsServices as $smsService) { @@ -123,12 +123,12 @@ class Ovh implements IProvider { ]; $body = json_encode($content); - $header = $this->getHeader('POST',$this->attrs['endpoint']."/sms/$smsAccount/jobs",$body); - $response = $this->client->post($this->attrs['endpoint']."/sms/$smsAccount/jobs",[ + $header = $this->getHeader('POST', $this->attrs['endpoint']."/sms/$smsAccount/jobs", $body); + $response = $this->client->post($this->attrs['endpoint']."/sms/$smsAccount/jobs", [ 'headers' => $header, 'json' => $content, ]); - $resultPostJob = json_decode($response->getBody(),true); + $resultPostJob = json_decode($response->getBody(), true); if (count($resultPostJob["validReceivers"]) === 0) { throw new SmsTransmissionException("Bad receiver $identifier"); @@ -168,7 +168,7 @@ class Ovh implements IProvider { * @param string $body JSON encoded body content for the POST request * @return array $header Contains the data for the request need by OVH */ - private function getHeader($method,$query,$body = '') { + private function getHeader($method, $query, $body = '') { $timestamp = time() + $this->attrs['timedelta']; $prehash = $this->attrs['AS'].'+'.$this->attrs['CK'].'+'.$method.'+'.$query.'+'.$body.'+'.$timestamp; $header = [ diff --git a/lib/Service/Gateway/SMS/Provider/OvhConfig.php b/lib/Service/Gateway/SMS/Provider/OvhConfig.php index 0e4793d..80b6538 100644 --- a/lib/Service/Gateway/SMS/Provider/OvhConfig.php +++ b/lib/Service/Gateway/SMS/Provider/OvhConfig.php @@ -46,7 +46,7 @@ class OvhConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -78,37 +78,37 @@ class OvhConfig implements IProviderConfig { } public function setApplicationKey(string $appKey) { - $this->config->setAppValue(Application::APP_NAME, 'ovh_application_key', $appKey); + $this->config->setAppValue(Application::APP_ID, 'ovh_application_key', $appKey); } public function setApplicationSecret(string $appSecret) { - $this->config->setAppValue(Application::APP_NAME, 'ovh_application_secret', $appSecret); + $this->config->setAppValue(Application::APP_ID, 'ovh_application_secret', $appSecret); } public function setConsumerKey(string $consumerKey) { - $this->config->setAppValue(Application::APP_NAME, 'ovh_consumer_key', $consumerKey); + $this->config->setAppValue(Application::APP_ID, 'ovh_consumer_key', $consumerKey); } public function setEndpoint(string $endpoint) { - $this->config->setAppValue(Application::APP_NAME, 'ovh_endpoint', $endpoint); + $this->config->setAppValue(Application::APP_ID, 'ovh_endpoint', $endpoint); } public function setAccount($account) { - $this->config->setAppValue(Application::APP_NAME, 'ovh_account', $account); + $this->config->setAppValue(Application::APP_ID, 'ovh_account', $account); } public function setSender($sender) { - $this->config->setAppValue(Application::APP_NAME, 'ovh_sender', $sender); + $this->config->setAppValue(Application::APP_ID, 'ovh_sender', $sender); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php b/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php index 5f0f2ec..35d6c69 100644 --- a/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php +++ b/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php @@ -43,7 +43,7 @@ class PlaySMSConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -55,7 +55,7 @@ class PlaySMSConfig implements IProviderConfig { } public function setUrl(string $url) { - $this->config->setAppValue(Application::APP_NAME, 'playsms_url', $url); + $this->config->setAppValue(Application::APP_ID, 'playsms_url', $url); } public function getUser(): string { @@ -63,7 +63,7 @@ class PlaySMSConfig implements IProviderConfig { } public function setUser(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'playsms_user', $user); + $this->config->setAppValue(Application::APP_ID, 'playsms_user', $user); } public function getPassword(): string { @@ -71,17 +71,17 @@ class PlaySMSConfig implements IProviderConfig { } public function setPassword(string $password) { - $this->config->setAppValue(Application::APP_NAME, 'playsms_password', $password); + $this->config->setAppValue(Application::APP_ID, 'playsms_password', $password); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php index e399d29..7aaf939 100644 --- a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php +++ b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php @@ -37,6 +37,8 @@ class ProviderFactory { public function getProvider(string $id): IProvider { switch ($id) { + case SipGate::PROVIDER_ID: + return $this->container->query(SipGate::class); case PuzzelSMS::PROVIDER_ID: return $this->container->query(PuzzelSMS::class); case PlaySMS::PROVIDER_ID: diff --git a/lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php b/lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php index 09d6b30..2133a6f 100644 --- a/lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php +++ b/lib/Service/Gateway/SMS/Provider/PuzzelSMSConfig.php @@ -44,7 +44,7 @@ class PuzzelSMSConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -56,7 +56,7 @@ class PuzzelSMSConfig implements IProviderConfig { } public function setUrl(string $url) { - $this->config->setAppValue(Application::APP_NAME, 'puzzel_url', $url); + $this->config->setAppValue(Application::APP_ID, 'puzzel_url', $url); } public function getUser(): string { @@ -64,7 +64,7 @@ class PuzzelSMSConfig implements IProviderConfig { } public function setUser(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'puzzel_user', $user); + $this->config->setAppValue(Application::APP_ID, 'puzzel_user', $user); } public function getPassword(): string { @@ -72,7 +72,7 @@ class PuzzelSMSConfig implements IProviderConfig { } public function setPassword(string $password) { - $this->config->setAppValue(Application::APP_NAME, 'puzzel_password', $password); + $this->config->setAppValue(Application::APP_ID, 'puzzel_password', $password); } public function getServiceId() { @@ -80,17 +80,17 @@ class PuzzelSMSConfig implements IProviderConfig { } public function setServiceId(string $serviceid) { - $this->config->setAppValue(Application::APP_NAME, 'puzzel_serviceid', $serviceid); + $this->config->setAppValue(Application::APP_ID, 'puzzel_serviceid', $serviceid); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/SipGate.php b/lib/Service/Gateway/SMS/Provider/SipGate.php new file mode 100644 index 0000000..3d18ad5 --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/SipGate.php @@ -0,0 +1,83 @@ +<?php + +declare(strict_types=1); + +/** + * @author Claus-Justus Heine <himself@claus-justus-heine.de> + * + * 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 SipGate implements IProvider { + public const PROVIDER_ID = 'sipgate'; + + /** @var IClient */ + private $client; + + /** @var WebSmsConfig */ + private $config; + + public function __construct(IClientService $clientService, + SipGateConfig $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(); + $tokenId = $config->getTokenId(); + $accessToken = $config->getAccessToken(); + $webSmsExtension = $config->getWebSmsExtension(); + + try { + $this->client->post('https://api.sipgate.com/v2/sessions/sms', [ + 'headers' => [ + 'Authorization' => 'Basic ' . base64_encode("$tokenId:$accessToken"), + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + ], + 'json' => [ + "smsId" => $webSmsExtension, + "message" => $message, + "recipient" => $identifier, + "sendAt" => null, + ], + ]); + } catch (Exception $ex) { + throw new SmsTransmissionException('SipGate Send Failed', $ex->getCode(), $ex); + } + } + + /** + * @return SipGateConfig + */ + public function getConfig(): IProviderConfig { + return $this->config; + } +} diff --git a/lib/Service/Gateway/SMS/Provider/SipGateConfig.php b/lib/Service/Gateway/SMS/Provider/SipGateConfig.php new file mode 100644 index 0000000..274bd85 --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/SipGateConfig.php @@ -0,0 +1,85 @@ +<?php + +declare(strict_types=1); + +/** + * @author Claus-Justus Heine <himself@claus-justus-heine.de> + * + * Nextcloud - Two-factor Gateway for SipGate + * + * 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 SipGateConfig implements IProviderConfig { + private const expected = [ + 'sipgate_token_id', + 'sipgate_access_token', + 'sipgate_web_sms_extension', + ]; + + /** @var IConfig */ + private $config; + + public function __construct(IConfig $config) { + $this->config = $config; + } + + private function getOrFail(string $key): string { + $val = $this->config->getAppValue(Application::APP_ID, $key, null); + if (is_null($val)) { + throw new ConfigurationException(); + } + return $val; + } + + public function getTokenId(): string { + return $this->getOrFail('sipgate_token_id'); + } + + public function setTokenId(string $tokenId) { + $this->config->setAppValue(Application::APP_ID, 'sipgate_token_id', $tokenId); + } + + public function getAccessToken(): string { + return $this->getOrFail('sipgate_access_token'); + } + + public function setAccessToken(string $accessToken) { + $this->config->setAppValue(Application::APP_ID, 'sipgate_access_token', $accessToken); + } + + public function getWebSmsExtension(): string { + return $this->getOrFail('sipgate_web_sms_extension'); + } + + public function setWebSmsExtension(string $webSmsExtension) { + $this->config->setAppValue(Application::APP_ID, 'sipgate_web_sms_extension', $webSmsExtension); + } + public function isComplete(): bool { + $set = $this->config->getAppKeys(Application::APP_ID); + return count(array_intersect($set, self::expected)) === count(self::expected); + } + + public function remove() { + foreach (self::expected as $key) { + $this->config->deleteAppValue(Application::APP_ID, $key); + } + } +} diff --git a/lib/Service/Gateway/SMS/Provider/Sms77IoConfig.php b/lib/Service/Gateway/SMS/Provider/Sms77IoConfig.php index eebc71c..7d4549c 100644 --- a/lib/Service/Gateway/SMS/Provider/Sms77IoConfig.php +++ b/lib/Service/Gateway/SMS/Provider/Sms77IoConfig.php @@ -41,7 +41,7 @@ class Sms77IoConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -53,17 +53,17 @@ class Sms77IoConfig implements IProviderConfig { } public function setApiKey(string $apiKey) { - $this->config->setAppValue(Application::APP_NAME, 'sms77io_api_key', $apiKey); + $this->config->setAppValue(Application::APP_ID, 'sms77io_api_key', $apiKey); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/SpryngSMSConfig.php b/lib/Service/Gateway/SMS/Provider/SpryngSMSConfig.php index 4670573..ade02bf 100644 --- a/lib/Service/Gateway/SMS/Provider/SpryngSMSConfig.php +++ b/lib/Service/Gateway/SMS/Provider/SpryngSMSConfig.php @@ -41,7 +41,7 @@ class SpryngSMSConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if ($val === null) { throw new ConfigurationException(); } @@ -53,17 +53,17 @@ class SpryngSMSConfig implements IProviderConfig { } public function setApiToken(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'spryng_apitoken', $user); + $this->config->setAppValue(Application::APP_ID, 'spryng_apitoken', $user); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/VoipMsConfig.php b/lib/Service/Gateway/SMS/Provider/VoipMsConfig.php index 7b3cdca..c54029f 100644 --- a/lib/Service/Gateway/SMS/Provider/VoipMsConfig.php +++ b/lib/Service/Gateway/SMS/Provider/VoipMsConfig.php @@ -43,7 +43,7 @@ class VoipMsConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -55,7 +55,7 @@ class VoipMsConfig implements IProviderConfig { } public function setUser(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'voipms_api_username', $user); + $this->config->setAppValue(Application::APP_ID, 'voipms_api_username', $user); } public function getPassword(): string { @@ -63,7 +63,7 @@ class VoipMsConfig implements IProviderConfig { } public function setPassword(string $password) { - $this->config->setAppValue(Application::APP_NAME, 'voipms_api_password', $password); + $this->config->setAppValue(Application::APP_ID, 'voipms_api_password', $password); } public function getDid(): string { @@ -71,17 +71,17 @@ class VoipMsConfig implements IProviderConfig { } public function setDid(string $did) { - $this->config->setAppValue(Application::APP_NAME, 'voipms_did', $did); + $this->config->setAppValue(Application::APP_ID, 'voipms_did', $did); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/VoipbusterConfig.php b/lib/Service/Gateway/SMS/Provider/VoipbusterConfig.php index 27d8127..a731cab 100644 --- a/lib/Service/Gateway/SMS/Provider/VoipbusterConfig.php +++ b/lib/Service/Gateway/SMS/Provider/VoipbusterConfig.php @@ -43,7 +43,7 @@ class VoipbusterConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -55,7 +55,7 @@ class VoipbusterConfig implements IProviderConfig { } public function setUser(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'voipbuster_api_username', $user); + $this->config->setAppValue(Application::APP_ID, 'voipbuster_api_username', $user); } public function getPassword(): string { @@ -63,7 +63,7 @@ class VoipbusterConfig implements IProviderConfig { } public function setPassword(string $password) { - $this->config->setAppValue(Application::APP_NAME, 'voipbuster_api_password', $password); + $this->config->setAppValue(Application::APP_ID, 'voipbuster_api_password', $password); } public function getDid(): string { @@ -71,17 +71,17 @@ class VoipbusterConfig implements IProviderConfig { } public function setDid(string $did) { - $this->config->setAppValue(Application::APP_NAME, 'voipbuster_did', $did); + $this->config->setAppValue(Application::APP_ID, 'voipbuster_did', $did); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php b/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php index facfdea..8a41faf 100644 --- a/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php +++ b/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php @@ -42,7 +42,7 @@ class WebSmsConfig implements IProviderConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + $val = $this->config->getAppValue(Application::APP_ID, $key, null); if (is_null($val)) { throw new ConfigurationException(); } @@ -54,7 +54,7 @@ class WebSmsConfig implements IProviderConfig { } public function setUser(string $user) { - $this->config->setAppValue(Application::APP_NAME, 'websms_de_user', $user); + $this->config->setAppValue(Application::APP_ID, 'websms_de_user', $user); } public function getPassword(): string { @@ -62,17 +62,17 @@ class WebSmsConfig implements IProviderConfig { } public function setPassword(string $password) { - $this->config->setAppValue(Application::APP_NAME, 'websms_de_password', $password); + $this->config->setAppValue(Application::APP_ID, 'websms_de_password', $password); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/Signal/Gateway.php b/lib/Service/Gateway/Signal/Gateway.php index b458517..e1c6476 100644 --- a/lib/Service/Gateway/Signal/Gateway.php +++ b/lib/Service/Gateway/Signal/Gateway.php @@ -62,21 +62,41 @@ class Gateway implements IGateway { */ public function send(IUser $user, string $identifier, string $message) { $client = $this->clientService->newClient(); - $response = $client->post( - $this->config->getUrl(), - [ - 'body' => [ - 'to' => $identifier, - 'message' => $message, - ], - ] - ); - $body = $response->getBody(); - $json = json_decode($body, true); + // determine type of gateway + $response = $client->get($this->config->getUrl() . '/v1/about'); + if ($response->getStatusCode() === 200) { + // New style gateway https://gitlab.com/morph027/signal-cli-dbus-rest-api + $response = $client->post( + $this->config->getUrl() . '/v1/send/' . $identifier, + [ + 'json' => [ 'message' => $message ], + ] + ); + $body = $response->getBody(); + $json = json_decode($body, true); + if ($response->getStatusCode() !== 201 || is_null($json) || !is_array($json) || !isset($json['timestamp'])) { + $status = $response->getStatusCode(); + throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); + } + } else { + // Try old deprecated gateway https://gitlab.com/morph027/signal-web-gateway + $response = $client->post( + $this->config->getUrl() . '/v1/send/' . $identifier, + [ + 'body' => [ + 'to' => $identifier, + 'message' => $message, + ], + 'json' => [ 'message' => $message ], + ] + ); + $body = $response->getBody(); + $json = json_decode($body, true); - if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) { - $status = $response->getStatusCode(); - throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); + if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) { + $status = $response->getStatusCode(); + throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); + } } } diff --git a/lib/Service/Gateway/Signal/GatewayConfig.php b/lib/Service/Gateway/Signal/GatewayConfig.php index d35d958..48d3e5c 100644 --- a/lib/Service/Gateway/Signal/GatewayConfig.php +++ b/lib/Service/Gateway/Signal/GatewayConfig.php @@ -41,7 +41,7 @@ class GatewayConfig implements IGatewayConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key); + $val = $this->config->getAppValue(Application::APP_ID, $key); if ($val === '') { throw new ConfigurationException(); } @@ -53,17 +53,17 @@ class GatewayConfig implements IGatewayConfig { } public function setUrl(string $url) { - $this->config->setAppValue(Application::APP_NAME, 'signal_url', $url); + $this->config->setAppValue(Application::APP_ID, 'signal_url', $url); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/Gateway/Telegram/GatewayConfig.php b/lib/Service/Gateway/Telegram/GatewayConfig.php index 3aff370..45e6e26 100644 --- a/lib/Service/Gateway/Telegram/GatewayConfig.php +++ b/lib/Service/Gateway/Telegram/GatewayConfig.php @@ -43,7 +43,7 @@ class GatewayConfig implements IGatewayConfig { } private function getOrFail(string $key): string { - $val = $this->config->getAppValue(Application::APP_NAME, $key); + $val = $this->config->getAppValue(Application::APP_ID, $key); if ($val === '') { throw new ConfigurationException(); } @@ -55,17 +55,17 @@ class GatewayConfig implements IGatewayConfig { } public function setBotToken(string $token) { - $this->config->setAppValue(Application::APP_NAME, 'telegram_bot_token', $token); + $this->config->setAppValue(Application::APP_ID, 'telegram_bot_token', $token); } public function isComplete(): bool { - $set = $this->config->getAppKeys(Application::APP_NAME); + $set = $this->config->getAppKeys(Application::APP_ID); return count(array_intersect($set, self::expected)) === count(self::expected); } public function remove() { foreach (self::expected as $key) { - $this->config->deleteAppValue(Application::APP_NAME, $key); + $this->config->deleteAppValue(Application::APP_ID, $key); } } } diff --git a/lib/Service/SetupService.php b/lib/Service/SetupService.php index dd287a3..22adb81 100644 --- a/lib/Service/SetupService.php +++ b/lib/Service/SetupService.php @@ -91,7 +91,7 @@ class SetupService { try { $gateway->send($user, $identifier, "$verificationNumber is your Nextcloud verification code."); } catch (SmsTransmissionException $ex) { - throw new VerificationTransmissionException('could not send verification code'); + throw new VerificationTransmissionException('could not send verification code', $ex->getCode(), $ex); } return $this->stateStorage->persist( diff --git a/lib/Service/StateStorage.php b/lib/Service/StateStorage.php index cbaf7ed..6517bb1 100644 --- a/lib/Service/StateStorage.php +++ b/lib/Service/StateStorage.php @@ -46,17 +46,17 @@ class StateStorage { private function getUserValue(IUser $user, string $gatewayName, string $key, $default = '') { $gatewayKey = $this->buildConfigKey($gatewayName, $key); - return $this->config->getUserValue($user->getUID(), Application::APP_NAME, $gatewayKey, $default); + return $this->config->getUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $default); } private function setUserValue(IUser $user, string $gatewayName, string $key, $value) { $gatewayKey = $this->buildConfigKey($gatewayName, $key); - $this->config->setUserValue($user->getUID(), Application::APP_NAME, $gatewayKey, $value); + $this->config->setUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $value); } private function deleteUserValue(IUser $user, string $gatewayName, string $key) { $gatewayKey = $this->buildConfigKey($gatewayName, $key); - $this->config->deleteUserValue($user->getUID(), Application::APP_NAME, $gatewayKey); + $this->config->deleteUserValue($user->getUID(), Application::APP_ID, $gatewayKey); } public function get(IUser $user, string $gatewayName): State { |