|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Pierre LEROUGE <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Nextcloud - Two-factor Gateway for Vortext |
|
9
|
|
|
* |
|
10
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
12
|
|
|
* as published by the Free Software Foundation. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider; |
|
25
|
|
|
|
|
26
|
|
|
use Exception; |
|
27
|
|
|
use OCA\TwoFactorGateway\Exception\SmsTransmissionException; |
|
28
|
|
|
use OCA\TwoFactorGateway\Exception\InvalidSmsProviderException; |
|
29
|
|
|
use OCP\Http\Client\IClient; |
|
30
|
|
|
use OCP\Http\Client\IClientService; |
|
31
|
|
|
|
|
32
|
|
|
class CegedimVortext implements IProvider { |
|
33
|
|
|
public const PROVIDER_ID = 'cegedim.cloud'; |
|
34
|
|
|
|
|
35
|
|
|
/** @var IClient */ |
|
36
|
|
|
private $client; |
|
37
|
|
|
|
|
38
|
|
|
/** @var CegedimVortextConfig */ |
|
39
|
|
|
private $config; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Url to communicate with Vortext API |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
private $endpoints = [ |
|
47
|
|
|
'eb4' => 'https://vortext.cloud.cegedim.com' |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Array of the 3 needed parameters to connect to the API |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private $attrs = [ |
|
55
|
|
|
'user' => null, |
|
56
|
|
|
'password' => null, |
|
57
|
|
|
'endpoint' => null |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
public function __construct(IClientService $clientService, |
|
62
|
|
|
CegedimVortextConfig $config) { |
|
63
|
|
|
$this->client = $clientService->newClient(); |
|
64
|
|
|
$this->config = $config; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $identifier |
|
69
|
|
|
* @param string $message |
|
70
|
|
|
* |
|
71
|
|
|
* @throws SmsTransmissionException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function send(string $identifier, string $message) { |
|
74
|
|
|
$config = $this->getConfig(); |
|
75
|
|
|
$endpoint = $config->getEndpoint(); |
|
76
|
|
|
$this->attrs['user'] = $config->getUsername(); |
|
77
|
|
|
$this->attrs['password'] = $config->getPassword(); |
|
78
|
|
|
$this->attrs['endpoint'] = $config->getEndpoint(); |
|
79
|
|
|
if (!isset($this->endpoints[$endpoint])) { |
|
80
|
|
|
throw new InvalidSmsProviderException("Endpoint $endpoint not found"); |
|
81
|
|
|
} |
|
82
|
|
|
$this->attrs['endpoint'] = $this->endpoints[$endpoint]; |
|
83
|
|
|
|
|
84
|
|
|
$content = [ |
|
85
|
|
|
"message"=> $message, |
|
86
|
|
|
"phoneNumber"=> $identifier, |
|
87
|
|
|
]; |
|
88
|
|
|
$body = json_encode($content); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
$response = $this->client->post( |
|
91
|
|
|
$this->attrs['endpoint']."/sms",[ |
|
92
|
|
|
'json' => $content, |
|
93
|
|
|
'auth' => [$this->attrs['user'],$this->attrs['password']] |
|
94
|
|
|
] |
|
95
|
|
|
); |
|
96
|
|
|
$resultPostJob = json_decode($response->getBody(),true); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
if (strlen($resultPostJob["messageId"]) === 0) { |
|
99
|
|
|
throw new SmsTransmissionException("Bad receiver $identifier"); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return CegedimVortextConfig |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getConfig(): IProviderConfig { |
|
107
|
|
|
return $this->config; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|