|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SPDX-FileCopyrightText: 2024 Christoph Wurst <[email protected]> |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\Drivers; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use OCA\TwoFactorGateway\Exception\MessageTransmissionException; |
|
14
|
|
|
use OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\AProvider; |
|
15
|
|
|
use OCA\TwoFactorGateway\Provider\FieldDefinition; |
|
16
|
|
|
use OCA\TwoFactorGateway\Provider\Settings; |
|
17
|
|
|
use OCP\Http\Client\IClient; |
|
18
|
|
|
use OCP\Http\Client\IClientService; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @method string getUser() |
|
22
|
|
|
* @method static setUser(string $user) |
|
23
|
|
|
* @method string getPassword() |
|
24
|
|
|
* @method static setPassword(string $password) |
|
25
|
|
|
*/ |
|
26
|
|
|
class WebSms extends AProvider { |
|
27
|
|
|
private IClient $client; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( |
|
30
|
|
|
IClientService $clientService, |
|
31
|
|
|
) { |
|
32
|
|
|
$this->client = $clientService->newClient(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function createSettings(): Settings { |
|
36
|
|
|
return new Settings( |
|
37
|
|
|
id: 'websms_de', |
|
38
|
|
|
name: 'WebSMS.de', |
|
39
|
|
|
fields: [ |
|
40
|
|
|
new FieldDefinition( |
|
41
|
|
|
field: 'user', |
|
42
|
|
|
prompt: 'Please enter your websms.de username:', |
|
43
|
|
|
), |
|
44
|
|
|
new FieldDefinition( |
|
45
|
|
|
field: 'password', |
|
46
|
|
|
prompt: 'Please enter your websms.de password:', |
|
47
|
|
|
), |
|
48
|
|
|
] |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
#[\Override] |
|
53
|
|
|
public function send(string $identifier, string $message) { |
|
54
|
|
|
$user = $this->getUser(); |
|
55
|
|
|
$password = $this->getPassword(); |
|
56
|
|
|
try { |
|
57
|
|
|
$this->client->post('https://api.websms.com/rest/smsmessaging/text', [ |
|
58
|
|
|
'headers' => [ |
|
59
|
|
|
'Authorization' => 'Basic ' . base64_encode("$user:$password"), |
|
60
|
|
|
'Content-Type' => 'application/json', |
|
61
|
|
|
], |
|
62
|
|
|
'json' => [ |
|
63
|
|
|
'messageContent' => $message, |
|
64
|
|
|
'test' => false, |
|
65
|
|
|
'recipientAddressList' => [$identifier], |
|
66
|
|
|
], |
|
67
|
|
|
]); |
|
68
|
|
|
} catch (Exception $ex) { |
|
69
|
|
|
throw new MessageTransmissionException(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|