-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cegedim.Cloud's vortext platform support
Signed-off-by: LEROUGE Pierre <[email protected]>
- Loading branch information
1 parent
ed687c0
commit 575dd5d
Showing
5 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Pierre LEROUGE <[email protected]> | ||
* | ||
* Nextcloud - Two-factor Gateway for Vortext | ||
* | ||
* 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 OCA\TwoFactorGateway\Exception\InvalidSmsProviderException; | ||
use OCP\Http\Client\IClient; | ||
use OCP\Http\Client\IClientService; | ||
|
||
class CegedimVortext implements IProvider { | ||
public const PROVIDER_ID = 'cegedim.cloud'; | ||
|
||
/** @var IClient */ | ||
private $client; | ||
|
||
/** @var CegedimVortextConfig */ | ||
private $config; | ||
|
||
/** | ||
* Url to communicate with Vortext API | ||
* | ||
* @var array | ||
*/ | ||
private $endpoints = [ | ||
'eb4' => 'https://vortext.cloud.cegedim.com' | ||
]; | ||
|
||
/** | ||
* Array of the 3 needed parameters to connect to the API | ||
* @var array | ||
*/ | ||
private $attrs = [ | ||
'user' => null, | ||
'password' => null, | ||
'endpoint' => null | ||
]; | ||
|
||
|
||
public function __construct(IClientService $clientService, | ||
CegedimVortextConfig $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(); | ||
$endpoint = $config->getEndpoint(); | ||
$this->attrs['user'] = $config->getUsername(); | ||
$this->attrs['password'] = $config->getPassword(); | ||
$this->attrs['endpoint'] = $config->getEndpoint(); | ||
if (!isset($this->endpoints[$endpoint])) { | ||
throw new InvalidSmsProviderException("Endpoint $endpoint not found"); | ||
} | ||
$this->attrs['endpoint'] = $this->endpoints[$endpoint]; | ||
|
||
$content = [ | ||
"message"=> $message, | ||
"phoneNumber"=> $identifier, | ||
]; | ||
$body = json_encode($content); | ||
|
||
$response = $this->client->post( | ||
$this->attrs['endpoint']."/sms",[ | ||
'json' => $content, | ||
'auth' => [$this->attrs['user'],$this->attrs['password']] | ||
] | ||
); | ||
$resultPostJob = json_decode($response->getBody(),true); | ||
|
||
if (strlen($resultPostJob["messageId"]) === 0) { | ||
throw new SmsTransmissionException("Bad receiver $identifier"); | ||
} | ||
} | ||
|
||
/** | ||
* @return CegedimVortextConfig | ||
*/ | ||
public function getConfig(): IProviderConfig { | ||
return $this->config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Pierre LEROUGE <[email protected]> | ||
* | ||
* Nextcloud - Two-factor Gateway for Vortext | ||
* | ||
* 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 CegedimVortextConfig implements IProviderConfig { | ||
|
||
const expected = [ | ||
'cegedimvortext_username', | ||
'cegedimvortext_password', | ||
'cegedimvortext_endpoint' | ||
]; | ||
|
||
/** @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 getUsername(): string { | ||
return $this->getOrFail('cegedimvortext_username'); | ||
} | ||
|
||
public function getPassword(): string { | ||
return $this->getOrFail('cegedimvortext_password'); | ||
} | ||
|
||
public function getEndpoint(): string { | ||
return $this->getOrFail('cegedimvortext_endpoint'); | ||
} | ||
|
||
public function setUsername(string $appKey) { | ||
$this->config->setAppValue(Application::APP_ID, 'cegedimvortext_username', $appKey); | ||
} | ||
|
||
public function setPassword(string $appSecret) { | ||
$this->config->setAppValue(Application::APP_ID, 'cegedimvortext_password', $appSecret); | ||
} | ||
|
||
public function setEndpoint(string $endpoint) { | ||
$this->config->setAppValue(Application::APP_ID, 'cegedimvortext_endpoint', $endpoint); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters