|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Christoph Wurst <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Nextcloud - Two-factor Gateway |
|
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 OCP\Http\Client\IClient; |
|
29
|
|
|
use OCP\Http\Client\IClientService; |
|
30
|
|
|
|
|
31
|
|
|
class WebSms implements IProvider { |
|
32
|
|
|
|
|
33
|
|
|
const PROVIDER_ID = 'websms'; |
|
34
|
|
|
|
|
35
|
|
|
/** @var IClient */ |
|
36
|
|
|
private $client; |
|
37
|
|
|
|
|
38
|
|
|
/** @var WebSmsConfig */ |
|
39
|
|
|
private $config; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(IClientService $clientService, |
|
42
|
|
|
WebSmsConfig $config) { |
|
43
|
|
|
$this->client = $clientService->newClient(); |
|
44
|
|
|
$this->config = $config; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $identifier |
|
49
|
|
|
* @param string $message |
|
50
|
|
|
* |
|
51
|
|
|
* @throws SmsTransmissionException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function send(string $identifier, string $message) { |
|
54
|
|
|
$config = $this->getConfig(); |
|
55
|
|
|
$user = $config->getUser(); |
|
56
|
|
|
$password = $config->getPassword(); |
|
57
|
|
|
try { |
|
58
|
|
|
$this->client->post('https://api.websms.com/rest/smsmessaging/text', [ |
|
59
|
|
|
'headers' => [ |
|
60
|
|
|
'Authorization' => 'Basic ' . base64_encode("$user:$password"), |
|
61
|
|
|
'Content-Type' => 'application/json', |
|
62
|
|
|
], |
|
63
|
|
|
'json' => [ |
|
64
|
|
|
'messageContent' => $message, |
|
65
|
|
|
'test' => false, |
|
66
|
|
|
'recipientAddressList' => [$identifier], |
|
67
|
|
|
], |
|
68
|
|
|
]); |
|
69
|
|
|
} catch (Exception $ex) { |
|
70
|
|
|
throw new SmsTransmissionException(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return WebSmsConfig |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getConfig(): IProviderConfig { |
|
78
|
|
|
return $this->config; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|