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\Signal; |
11
|
|
|
|
12
|
|
|
use OCA\TwoFactorGateway\Exception\MessageTransmissionException; |
13
|
|
|
use OCA\TwoFactorGateway\Provider\Gateway\AGateway; |
14
|
|
|
use OCP\Http\Client\IClientService; |
15
|
|
|
use OCP\IAppConfig; |
16
|
|
|
use OCP\IUser; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
use Symfony\Component\Console\Question\Question; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* An integration of https://gitlab.com/morph027/signal-web-gateway |
25
|
|
|
* |
26
|
|
|
* @method string getUrl() |
27
|
|
|
* @method static setUrl(string $url) |
28
|
|
|
*/ |
29
|
|
|
class Gateway extends AGateway { |
30
|
|
|
public const SCHEMA = [ |
31
|
|
|
'name' => 'Signal', |
32
|
|
|
'instructions' => 'The gateway can send authentication to your Signal mobile and deskop app.', |
33
|
|
|
'fields' => [ |
34
|
|
|
['field' => 'url', 'prompt' => 'Please enter the URL of the Signal gateway (leave blank to use default):', 'default' => 'http://localhost:5000'], |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
public IAppConfig $appConfig, |
40
|
|
|
private IClientService $clientService, |
41
|
|
|
private LoggerInterface $logger, |
42
|
|
|
) { |
43
|
|
|
parent::__construct($appConfig); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
#[\Override] |
47
|
|
|
public function send(IUser $user, string $identifier, string $message, array $extra = []): void { |
48
|
|
|
$client = $this->clientService->newClient(); |
49
|
|
|
// determine type of gateway |
50
|
|
|
$response = $client->get($this->getUrl() . '/v1/about'); |
51
|
|
|
if ($response->getStatusCode() === 200) { |
52
|
|
|
// New style gateway https://gitlab.com/morph027/signal-cli-dbus-rest-api |
53
|
|
|
$response = $client->post( |
54
|
|
|
$this->getUrl() . '/v1/send/' . $identifier, |
55
|
|
|
[ |
56
|
|
|
'json' => [ 'message' => $message ], |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
$body = (string) $response->getBody(); |
60
|
|
|
$json = json_decode($body, true); |
61
|
|
|
if ($response->getStatusCode() !== 201 || is_null($json) || !is_array($json) || !isset($json['timestamp'])) { |
62
|
|
|
$status = $response->getStatusCode(); |
63
|
|
|
throw new MessageTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
// Try old deprecated gateway https://gitlab.com/morph027/signal-web-gateway |
67
|
|
|
$response = $client->post( |
68
|
|
|
$this->getUrl() . '/v1/send/' . $identifier, |
69
|
|
|
[ |
70
|
|
|
'body' => [ |
71
|
|
|
'to' => $identifier, |
72
|
|
|
'message' => $message, |
73
|
|
|
], |
74
|
|
|
'json' => [ 'message' => $message ], |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
$body = $response->getBody(); |
78
|
|
|
$json = json_decode($body, true); |
|
|
|
|
79
|
|
|
|
80
|
|
|
if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) { |
81
|
|
|
$status = $response->getStatusCode(); |
82
|
|
|
throw new MessageTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
#[\Override] |
88
|
|
|
public function cliConfigure(InputInterface $input, OutputInterface $output): int { |
89
|
|
|
$helper = new QuestionHelper(); |
90
|
|
|
$urlQuestion = new Question(self::SCHEMA['fields'][0]['prompt'], self::SCHEMA['fields'][0]['default']); |
91
|
|
|
$url = $helper->ask($input, $output, $urlQuestion); |
92
|
|
|
$output->writeln("Using $url."); |
93
|
|
|
|
94
|
|
|
$this->setUrl($url); |
95
|
|
|
return 0; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|