Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/twofactor_gateway.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2018-08-21 19:07:03 +0300
committerChristoph Wurst <christoph@winzerhof-wurst.at>2018-08-21 19:07:03 +0300
commitefdc89dd6fd07b7c221f0ccc47e186085c1043c1 (patch)
tree4f2f64d57516db92dda320dea9f06c9f4e3cf798
parent6de6fca2486413d9d92c2756b8d25583aed82780 (diff)
Extract configuration from gateways
This makes it possible to use them in an abstract way and check if their config is sufficient/valid. Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
-rw-r--r--lib/AppInfo/Application.php34
-rw-r--r--lib/Exception/ConfigurationException.php35
-rw-r--r--lib/Exception/InvalidSmsProviderException.php (renamed from appinfo/app.php)13
-rw-r--r--lib/Provider/SmsProvider.php7
-rw-r--r--lib/Service/Gateway/IGateway.php11
-rw-r--r--lib/Service/Gateway/IGatewayConfig.php30
-rw-r--r--lib/Service/Gateway/SMS/Provider/IProvider.php40
-rw-r--r--lib/Service/Gateway/SMS/Provider/IProviderConfig.php30
-rw-r--r--lib/Service/Gateway/SMS/Provider/PlaySMS.php83
-rw-r--r--lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php69
-rw-r--r--lib/Service/Gateway/SMS/Provider/ProviderFactory.php49
-rw-r--r--lib/Service/Gateway/SMS/Provider/WebSms.php (renamed from lib/Service/Gateway/SMS/WebSmsGateway.php)47
-rw-r--r--lib/Service/Gateway/SMS/Provider/WebSmsConfig.php65
-rw-r--r--lib/Service/Gateway/SMS/SMS.php (renamed from lib/Service/Gateway/SMS/PlaySMSGateway.php)53
-rw-r--r--lib/Service/Gateway/SMS/SMSConfig.php65
-rw-r--r--lib/Service/Gateway/Signal/Gateway.php21
-rw-r--r--lib/Service/Gateway/State.php62
-rw-r--r--lib/Service/Gateway/Telegram/Gateway.php22
-rw-r--r--lib/Service/StateStorage.php9
19 files changed, 598 insertions, 147 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
index 2c38aa8..56532e2 100644
--- a/lib/AppInfo/Application.php
+++ b/lib/AppInfo/Application.php
@@ -1,6 +1,6 @@
<?php
-declare(strict_types = 1);
+declare(strict_types=1);
/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
@@ -23,15 +23,7 @@ declare(strict_types = 1);
namespace OCA\TwoFactorGateway\AppInfo;
-use Exception;
-use OCA\TwoFactorGateway\Service\Gateway\IGateway;
-use OCA\TwoFactorGateway\Service\Gateway\SMS\PlaySMSGateway;
-use OCA\TwoFactorGateway\Service\Gateway\Signal\Gateway as SignalGateway;
-use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway;
-use OCA\TwoFactorGateway\Service\Gateway\TestGateway;
-use OCA\TwoFactorGateway\Service\Gateway\SMS\WebSmsGateway;
use OCP\AppFramework\App;
-use OCP\IConfig;
class Application extends App {
@@ -39,30 +31,6 @@ class Application extends App {
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_NAME, $urlParams);
-
- $container = $this->getContainer();
-
- /* @var $config IConfig */
- $config = $container->query(IConfig::class);
- $provider = $config->getAppValue('twofactor_gateway', 'sms_provider', 'websms.de');
-
- $container->registerAlias(IGateway::class, $this->getSmsProviderClass($provider));
- }
-
- private function getSmsProviderClass(string $name): string {
- switch ($name) {
- case 'playsms':
- return PlaySMSGateway::class;
- case 'signal':
- return SignalGateway::class;
- case 'telegram':
- return TelegramGateway::class;
- case 'test':
- return TestGateway::class;
- case 'websms.de':
- return WebSmsGateway::class;
- }
- throw new Exception('invalid configuration for twofactor_gateway app');
}
}
diff --git a/lib/Exception/ConfigurationException.php b/lib/Exception/ConfigurationException.php
new file mode 100644
index 0000000..cef49f0
--- /dev/null
+++ b/lib/Exception/ConfigurationException.php
@@ -0,0 +1,35 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Pascal Clémot <pascal.clemot@free.fr>
+ *
+ * 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\Exception;
+
+use Exception;
+use Throwable;
+
+class ConfigurationException extends Exception {
+
+ public function __construct(int $code = 0, Throwable $previous = null) {
+ parent::__construct('Invalid gateway/provider configuration set', $code, $previous);
+ }
+
+}
diff --git a/appinfo/app.php b/lib/Exception/InvalidSmsProviderException.php
index 8482273..f6e7e18 100644
--- a/appinfo/app.php
+++ b/lib/Exception/InvalidSmsProviderException.php
@@ -1,7 +1,9 @@
<?php
+declare(strict_types=1);
+
/**
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ * @author Pascal Clémot <pascal.clemot@free.fr>
*
* Nextcloud - Two-factor Gateway
*
@@ -18,6 +20,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
-include_once __DIR__ . '/../vendor/autoload.php';
-$app = new OCA\TwoFactorGateway\AppInfo\Application();
+namespace OCA\TwoFactorGateway\Exception;
+
+use Exception;
+
+class InvalidSmsProviderException extends Exception {
+
+}
diff --git a/lib/Provider/SmsProvider.php b/lib/Provider/SmsProvider.php
index 73174c6..12fec2f 100644
--- a/lib/Provider/SmsProvider.php
+++ b/lib/Provider/SmsProvider.php
@@ -26,6 +26,7 @@ namespace OCA\TwoFactorGateway\Provider;
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
use OCA\TwoFactorGateway\PhoneNumberMask;
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
+use OCA\TwoFactorGateway\Service\Gateway\SMS\SMS;
use OCA\TwoFactorGateway\Service\StateStorage;
use OCP\Authentication\TwoFactorAuth\IProvider;
use OCP\IL10N;
@@ -57,12 +58,12 @@ class SmsProvider implements IProvider {
/** @var IL10N */
private $l10n;
- public function __construct(IGateway $gateway,
+ public function __construct(SMS $smsGateway,
StateStorage $stateStorage,
ISession $session,
ISecureRandom $secureRandom,
IL10N $l10n) {
- $this->gateway = $gateway;
+ $this->gateway = $smsGateway;
$this->stateStorage = $stateStorage;
$this->session = $session;
$this->secureRandom = $secureRandom;
@@ -89,7 +90,7 @@ class SmsProvider implements IProvider {
* Get the description for selecting the 2FA provider
*/
public function getDescription(): string {
- return $this->gateway->getProviderDescription();
+ return $this->l10n->t('Authenticate via SMS');
}
private function getSecret(): string {
diff --git a/lib/Service/Gateway/IGateway.php b/lib/Service/Gateway/IGateway.php
index 588c476..da467c5 100644
--- a/lib/Service/Gateway/IGateway.php
+++ b/lib/Service/Gateway/IGateway.php
@@ -37,19 +37,18 @@ interface IGateway {
public function getShortName(): string;
/**
- * Get a gateway-specific description for the gateway 2FA provider,
- * e.g. `Authenticate via SMS`.
+ * Get the gateway-specific configuration
*
- * @return string
+ * @return IGatewayConfig
*/
- public function getProviderDescription(): string;
+ public function getConfig(): IGatewayConfig;
/**
* @param IUser $user
- * @param string $idenfier
+ * @param string $identifier
* @param string $message
*
* @throws SmsTransmissionException
*/
- public function send(IUser $user, string $idenfier, string $message);
+ public function send(IUser $user, string $identifier, string $message);
}
diff --git a/lib/Service/Gateway/IGatewayConfig.php b/lib/Service/Gateway/IGatewayConfig.php
new file mode 100644
index 0000000..5b91e47
--- /dev/null
+++ b/lib/Service/Gateway/IGatewayConfig.php
@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Pascal Clémot <pascal.clemot@free.fr>
+ *
+ * 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;
+
+interface IGatewayConfig {
+
+ public function isComplete(): bool;
+
+} \ No newline at end of file
diff --git a/lib/Service/Gateway/SMS/Provider/IProvider.php b/lib/Service/Gateway/SMS/Provider/IProvider.php
new file mode 100644
index 0000000..c6dab88
--- /dev/null
+++ b/lib/Service/Gateway/SMS/Provider/IProvider.php
@@ -0,0 +1,40 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Pascal Clémot <pascal.clemot@free.fr>
+ *
+ * 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 OCP\IConfig;
+
+interface IProvider {
+
+ /**
+ * @param string $identifier
+ * @param string $message
+ *
+ * @throws SmsTransmissionException
+ */
+ public function send(string $identifier, string $message);
+
+ public function getConfig(): IProviderConfig;
+
+} \ No newline at end of file
diff --git a/lib/Service/Gateway/SMS/Provider/IProviderConfig.php b/lib/Service/Gateway/SMS/Provider/IProviderConfig.php
new file mode 100644
index 0000000..6feba46
--- /dev/null
+++ b/lib/Service/Gateway/SMS/Provider/IProviderConfig.php
@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Pascal Clémot <pascal.clemot@free.fr>
+ *
+ * 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;
+
+interface IProviderConfig {
+
+ public function isComplete(): bool;
+
+}
diff --git a/lib/Service/Gateway/SMS/Provider/PlaySMS.php b/lib/Service/Gateway/SMS/Provider/PlaySMS.php
new file mode 100644
index 0000000..8c27c6b
--- /dev/null
+++ b/lib/Service/Gateway/SMS/Provider/PlaySMS.php
@@ -0,0 +1,83 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Pascal Clémot <pascal.clemot@free.fr>
+ *
+ * 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 PlaySMS implements IProvider {
+
+ const PROVIDER_ID = 'playsms';
+
+ /** @var IClient */
+ private $client;
+
+ /** @var PlaySMSConfig */
+ private $config;
+
+ public function __construct(IClientService $clientService,
+ PlaySMSConfig $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();
+
+ try {
+ $this->client->get(
+ $config->getUrl(),
+ [
+ 'query' => [
+ 'app' => 'ws',
+ 'u' => $config->getUser(),
+ 'h' => $config->getPassword(),
+ 'op' => 'pv',
+ 'to' => $identifier,
+ 'msg' => $message,
+ ],
+ ]
+ );
+ } catch (Exception $ex) {
+ throw new SmsTransmissionException();
+ }
+ }
+
+ /**
+ * @return PlaySMSConfig
+ */
+ public function getConfig(): IProviderConfig {
+ return $this->config;
+ }
+
+}
diff --git a/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php b/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php
new file mode 100644
index 0000000..76e32e8
--- /dev/null
+++ b/lib/Service/Gateway/SMS/Provider/PlaySMSConfig.php
@@ -0,0 +1,69 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Pascal Clémot <pascal.clemot@free.fr>
+ *
+ * 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 PlaySMSConfig 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 getUrl(): string {
+ return $this->getOrFail('playsms_url');
+ }
+
+ public function getUser(): string {
+ return $this->getOrFail('playsms_user');
+ }
+
+ public function getPassword(): string {
+ return $this->getOrFail('playsms_password');
+ }
+
+ public function isComplete(): bool {
+ $set = $this->config->getAppKeys(Application::APP_NAME);
+ $expected = [
+ 'playsms_url',
+ 'playsms_user',
+ 'playsms_password',
+ ];
+ return count(array_intersect($set, $expected)) === count($expected);
+ }
+} \ No newline at end of file
diff --git a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php
new file mode 100644
index 0000000..5281061
--- /dev/null
+++ b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php
@@ -0,0 +1,49 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Pascal Clémot <pascal.clemot@free.fr>
+ *
+ * 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 OCA\TwoFactorGateway\Exception\InvalidSmsProviderException;
+use OCP\AppFramework\IAppContainer;
+
+class ProviderFactory {
+
+ /** @var IAppContainer */
+ private $container;
+
+ public function __construct(IAppContainer $container) {
+ $this->container = $container;
+ }
+
+ public function getProvider(string $id): IProvider {
+ switch ($id) {
+ case PlaySMS::PROVIDER_ID:
+ return $this->container->query(PlaySMS::class);
+ case WebSms::PROVIDER_ID:
+ return $this->container->query(WebSms::class);
+ default:
+ throw new InvalidSmsProviderException("Provider <$id> does not exist");
+ }
+ }
+
+}
diff --git a/lib/Service/Gateway/SMS/WebSmsGateway.php b/lib/Service/Gateway/SMS/Provider/WebSms.php
index a238a1c..7a2edcc 100644
--- a/lib/Service/Gateway/SMS/WebSmsGateway.php
+++ b/lib/Service/Gateway/SMS/Provider/WebSms.php
@@ -21,46 +21,39 @@ declare(strict_types=1);
*
*/
-namespace OCA\TwoFactorGateway\Service\Gateway\SMS;
+namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider;
use Exception;
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
-use OCA\TwoFactorGateway\Service\Gateway\IGateway;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
-use OCP\IConfig;
-use OCP\IL10N;
-use OCP\IUser;
-class WebSmsGateway implements IGateway {
+class WebSms implements IProvider {
+
+ const PROVIDER_ID = 'websms';
/** @var IClient */
private $client;
- /** @var IConfig */
+ /** @var WebSmsConfig */
private $config;
- /** @var IL10N */
- private $l10n;
-
public function __construct(IClientService $clientService,
- IConfig $config,
- IL10N $l10n) {
+ WebSmsConfig $config) {
$this->client = $clientService->newClient();
$this->config = $config;
- $this->l10n = $l10n;
}
/**
- * @param IUser $user
- * @param string $idenfier
+ * @param string $identifier
* @param string $message
*
* @throws SmsTransmissionException
*/
- public function send(IUser $user, string $idenfier, string $message) {
- $user = $this->config->getAppValue('twofactor_gateway', 'websms_de_user');
- $password = $this->config->getAppValue('twofactor_gateway', 'websms_de_password');
+ public function send(string $identifier, string $message) {
+ $config = $this->getConfig();
+ $user = $config->getUser();
+ $password = $config->getPassword();
try {
$this->client->post('https://api.websms.com/rest/smsmessaging/text', [
'headers' => [
@@ -70,7 +63,7 @@ class WebSmsGateway implements IGateway {
'json' => [
'messageContent' => $message,
'test' => false,
- 'recipientAddressList' => [$idenfier],
+ 'recipientAddressList' => [$identifier],
],
]);
} catch (Exception $ex) {
@@ -79,19 +72,9 @@ class WebSmsGateway implements IGateway {
}
/**
- * Get a short description of this gateway's name so that users know how
- * their messages are delivered, e.g. "Telegram"
- *
- * @return string
- */
- public function getShortName(): string {
- return 'SMS';
- }
-
- /**
- * @return string
+ * @return WebSmsConfig
*/
- public function getProviderDescription(): string {
- return $this->l10n->t('Authenticate via SMS');
+ public function getConfig(): IProviderConfig {
+ return $this->config;
}
}
diff --git a/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php b/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php
new file mode 100644
index 0000000..93eafd0
--- /dev/null
+++ b/lib/Service/Gateway/SMS/Provider/WebSmsConfig.php
@@ -0,0 +1,65 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ * @author André Fondse <andre@hetnetwerk.org>
+ *
+ * Nextcloud - Two-factor Gateway for Telegram
+ *
+ * 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 WebSmsConfig 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('websms_de_user');
+ }
+
+ public function getPassword(): string {
+ return $this->getOrFail('websms_de_password');
+ }
+
+ public function isComplete(): bool {
+ $set = $this->config->getAppKeys(Application::APP_NAME);
+ $expected = [
+ 'websms_de_user',
+ 'websms_de_password',
+ ];
+ return count(array_intersect($set, $expected)) === count($expected);
+ }
+
+}
diff --git a/lib/Service/Gateway/SMS/PlaySMSGateway.php b/lib/Service/Gateway/SMS/SMS.php
index c38763e..8a36011 100644
--- a/lib/Service/Gateway/SMS/PlaySMSGateway.php
+++ b/lib/Service/Gateway/SMS/SMS.php
@@ -23,59 +23,29 @@ declare(strict_types=1);
namespace OCA\TwoFactorGateway\Service\Gateway\SMS;
-use Exception;
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
-use OCP\Http\Client\IClient;
-use OCP\Http\Client\IClientService;
-use OCP\IConfig;
-use OCP\IL10N;
+use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
use OCP\IUser;
-class PlaySMSGateway implements IGateway {
+class SMS implements IGateway {
- /** @var IClient */
- private $client;
-
- /** @var IConfig */
+ /** @var SMSConfig */
private $config;
- /** @var IL10N */
- private $l10n;
-
- public function __construct(IClientService $clientService,
- IConfig $config,
- IL10N $l10n) {
- $this->client = $clientService->newClient();
+ public function __construct(SMSConfig $config) {
$this->config = $config;
- $this->l10n = $l10n;
}
/**
* @param IUser $user
- * @param string $idenfier
+ * @param string $identifier
* @param string $message
*
* @throws SmsTransmissionException
*/
- public function send(IUser $user, string $idenfier, string $message) {
- $url = $this->config->getAppValue('twofactor_gateway', 'playsms_url');
- $user = $this->config->getAppValue('twofactor_gateway', 'playsms_user');
- $password = $this->config->getAppValue('twofactor_gateway', 'playsms_password');
- try {
- $this->client->get($url, [
- 'query' => [
- 'app' => 'ws',
- 'u' => $user,
- 'h' => $password,
- 'op' => 'pv',
- 'to' => $idenfier,
- 'msg' => $message,
- ],
- ]);
- } catch (Exception $ex) {
- throw new SmsTransmissionException();
- }
+ public function send(IUser $user, string $identifier, string $message) {
+ $this->config->getProvider()->send($identifier, $message);
}
/**
@@ -89,9 +59,12 @@ class PlaySMSGateway implements IGateway {
}
/**
- * @return string
+ * Get the gateway-specific configuration
+ *
+ * @return SMSConfig
*/
- public function getProviderDescription(): string {
- return $this->l10n->t('Authenticate via SMS');
+ public function getConfig(): IGatewayConfig {
+ return $this->config;
}
+
}
diff --git a/lib/Service/Gateway/SMS/SMSConfig.php b/lib/Service/Gateway/SMS/SMSConfig.php
new file mode 100644
index 0000000..4e3da11
--- /dev/null
+++ b/lib/Service/Gateway/SMS/SMSConfig.php
@@ -0,0 +1,65 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @author Pascal Clémot <pascal.clemot@free.fr>
+ *
+ * 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;
+
+use OCA\TwoFactorGateway\AppInfo\Application;
+use OCA\TwoFactorGateway\Exception\ConfigurationException;
+use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
+use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\IProvider;
+use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\ProviderFactory;
+use OCP\IConfig;
+
+class SMSConfig implements IGatewayConfig {
+
+ /** @var IConfig */
+ private $config;
+
+ /** @var ProviderFactory */
+ private $providerFactory;
+
+ public function __construct(IConfig $config,
+ ProviderFactory $providerFactory) {
+ $this->config = $config;
+ $this->providerFactory = $providerFactory;
+ }
+
+ public function getProvider(): IProvider {
+ $providerName = $this->config->getAppValue(Application::APP_NAME, 'sms_provider_name', null);
+ if (is_null($providerName)) {
+ throw new ConfigurationException();
+ }
+
+ return $this->providerFactory->getProvider($providerName);
+ }
+
+ public function isComplete(): bool {
+ try {
+ $provider = $this->getProvider();
+ return $provider->getConfig($this->config)->isComplete();
+ } catch (ConfigurationException $ex) {
+ return false;
+ }
+ }
+
+} \ No newline at end of file
diff --git a/lib/Service/Gateway/Signal/Gateway.php b/lib/Service/Gateway/Signal/Gateway.php
index d4c3f48..f87f37d 100644
--- a/lib/Service/Gateway/Signal/Gateway.php
+++ b/lib/Service/Gateway/Signal/Gateway.php
@@ -26,6 +26,7 @@ namespace OCA\TwoFactorGateway\Service\Gateway\Signal;
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
+use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
use OCP\Http\Client\IClientService;
use OCP\IL10N;
use OCP\ILogger;
@@ -42,25 +43,20 @@ class Gateway implements IGateway {
/** @var ILogger */
private $logger;
- /** @var IL10N */
- private $l10n;
-
public function __construct(IClientService $clientService,
- ILogger $logger,
- IL10N $l10n) {
+ ILogger $logger) {
$this->clientService = $clientService;
$this->logger = $logger;
- $this->l10n = $l10n;
}
/**
* @param IUser $user
- * @param string $idenfier
+ * @param string $identifier
* @param string $message
*
* @throws SmsTransmissionException
*/
- public function send(IUser $user, string $idenfier, string $message) {
+ public function send(IUser $user, string $identifier, string $message) {
// TODO: make configurable
$endpoint = 'http://localhost:5000';
@@ -91,9 +87,12 @@ class Gateway implements IGateway {
}
/**
- * @return string
+ * Get the gateway-specific configuration
+ *
+ * @return IGatewayConfig
*/
- public function getProviderDescription(): string {
- return $this->l10n->t('Authenticate via Signal');
+ public function getConfig(): IGatewayConfig {
+ // TODO: Implement getConfig() method.
}
+
}
diff --git a/lib/Service/Gateway/State.php b/lib/Service/Gateway/State.php
new file mode 100644
index 0000000..00c4c22
--- /dev/null
+++ b/lib/Service/Gateway/State.php
@@ -0,0 +1,62 @@
+<?php
+
+/**
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * 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;
+
+
+class State {
+
+ const DISABLED = 0;
+ const VERIFYING = 1;
+ const ENABLED = 2;
+
+ /** @var int */
+ private $state;
+
+ private function __construct(int $state) {
+ $this->state = $state;
+ }
+
+ public static function disabled(): State {
+ return new self(self::DISABLED);
+ }
+
+ public static function verifying(): State {
+ return new self(self::VERIFYING);
+ }
+
+ public static function enabled(): State {
+ return new self(self::ENABLED);
+ }
+
+ public function isDisabled(): bool {
+ return $this->state === self::DISABLED;
+ }
+
+ public function isVerifying(): bool {
+ return $this->state === self::DISABLED;
+ }
+
+ public function isEnabled(): bool {
+ return $this->state === self::DISABLED;
+ }
+
+}
diff --git a/lib/Service/Gateway/Telegram/Gateway.php b/lib/Service/Gateway/Telegram/Gateway.php
index 444eaaf..0a6b326 100644
--- a/lib/Service/Gateway/Telegram/Gateway.php
+++ b/lib/Service/Gateway/Telegram/Gateway.php
@@ -27,6 +27,7 @@ namespace OCA\TwoFactorGateway\Service\Gateway\Telegram;
use Exception;
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
+use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
@@ -43,25 +44,20 @@ class Gateway implements IGateway {
/** @var IConfig */
private $config;
- /** @var IL10N */
- private $l10n;
-
public function __construct(IClientService $clientService,
- IConfig $config,
- IL10N $l10n) {
+ IConfig $config) {
$this->client = $clientService->newClient();
$this->config = $config;
- $this->l10n = $l10n;
}
/**
* @param IUser $user
- * @param string $idenfier
+ * @param string $identifier
* @param string $message
*
- * @throws \Telegram\Bot\Exceptions\TelegramSDKException
+ * @throws SmsTransmissionException
*/
- public function send(IUser $user, string $idenfier, string $message) {
+ public function send(IUser $user, string $identifier, string $message) {
$token = $this->config->getAppValue('twofactor_gateway', 'telegram_bot_token', null);
// TODO: token missing handling
@@ -108,9 +104,11 @@ class Gateway implements IGateway {
}
/**
- * @return string
+ * Get the gateway-specific configuration
+ *
+ * @return IGatewayConfig
*/
- public function getProviderDescription(): string {
- return $this->l10n->t('Authenticate via Telegram');
+ public function getConfig(): IGatewayConfig {
+ // TODO: Implement getConfig() method.
}
}
diff --git a/lib/Service/StateStorage.php b/lib/Service/StateStorage.php
index e53f637..75e3f0f 100644
--- a/lib/Service/StateStorage.php
+++ b/lib/Service/StateStorage.php
@@ -37,13 +37,8 @@ class StateStorage {
/** @var IConfig */
private $config;
- /** @var IGateway */
- private $gateway;
-
- public function __construct(IConfig $config,
- IGateway $gateway) {
+ public function __construct(IConfig $config) {
$this->config = $config;
- $this->gateway = $gateway;
}
public function get(IUser $user): State {
@@ -62,7 +57,7 @@ class StateStorage {
return new State(
$user,
$state,
- $this->gateway->getShortName(),
+ '', // TODO: fix
$identifier,
$verificationCode
);