|
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\Telegram; |
|
11
|
|
|
|
|
12
|
|
|
use OCA\TwoFactorGateway\AppInfo\Application; |
|
13
|
|
|
use OCA\TwoFactorGateway\Exception\ConfigurationException; |
|
14
|
|
|
use OCA\TwoFactorGateway\Provider\Channel\Telegram\Provider\AProvider; |
|
15
|
|
|
use OCA\TwoFactorGateway\Provider\Gateway\AGateway; |
|
16
|
|
|
use OCA\TwoFactorGateway\Provider\Settings; |
|
17
|
|
|
use OCP\IAppConfig; |
|
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\ChoiceQuestion; |
|
22
|
|
|
|
|
23
|
|
|
class Gateway extends AGateway { |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct( |
|
26
|
|
|
public IAppConfig $appConfig, |
|
27
|
|
|
private Factory $telegramProviderFactory, |
|
28
|
|
|
) { |
|
29
|
1 |
|
parent::__construct($appConfig); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
#[\Override] |
|
33
|
|
|
public function send(string $identifier, string $message, array $extra = []): void { |
|
34
|
|
|
$this->getProvider()->send($identifier, $message); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
#[\Override] |
|
38
|
|
|
final public function cliConfigure(InputInterface $input, OutputInterface $output): int { |
|
39
|
|
|
$namespaces = $this->telegramProviderFactory->getFqcnList(); |
|
40
|
|
|
$names = []; |
|
41
|
|
|
$providers = []; |
|
42
|
|
|
foreach ($namespaces as $ns) { |
|
43
|
|
|
$provider = $this->telegramProviderFactory->get($ns); |
|
44
|
|
|
$providers[] = $provider; |
|
45
|
|
|
$names[] = $provider->getSettings()->name; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$helper = new QuestionHelper(); |
|
49
|
|
|
$choiceQuestion = new ChoiceQuestion('Please choose a Telegram provider:', $names); |
|
50
|
|
|
$name = $helper->ask($input, $output, $choiceQuestion); |
|
51
|
|
|
$selectedIndex = array_search($name, $names); |
|
52
|
|
|
|
|
53
|
|
|
$providers[$selectedIndex]->cliConfigure($input, $output); |
|
54
|
|
|
return 0; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
#[\Override] |
|
58
|
|
|
public function createSettings(): Settings { |
|
59
|
|
|
try { |
|
60
|
|
|
$settings = $this->getProvider()->getSettings(); |
|
61
|
|
|
} catch (ConfigurationException) { |
|
62
|
|
|
$settings = new Settings( |
|
63
|
|
|
name: 'Telegram', |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
return $settings; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
#[\Override] |
|
70
|
|
|
public function isComplete(?Settings $settings = null): bool { |
|
71
|
2 |
|
if ($settings === null) { |
|
72
|
|
|
try { |
|
73
|
2 |
|
$provider = $this->getProvider(); |
|
74
|
2 |
|
} catch (ConfigurationException) { |
|
75
|
2 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
$settings = $provider->getSettings(); |
|
78
|
|
|
} |
|
79
|
|
|
return parent::isComplete($settings); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
#[\Override] |
|
83
|
|
|
public function getConfiguration(?Settings $settings = null): array { |
|
84
|
|
|
try { |
|
85
|
1 |
|
$provider = $this->getProvider(); |
|
86
|
|
|
$settings = $provider->getSettings(); |
|
87
|
|
|
$config = parent::getConfiguration($settings); |
|
88
|
|
|
$config['provider'] = $settings->name; |
|
89
|
|
|
return $config; |
|
90
|
1 |
|
} catch (ConfigurationException|\Throwable $e) { |
|
91
|
1 |
|
$providers = []; |
|
92
|
1 |
|
foreach ($this->telegramProviderFactory->getFqcnList() as $fqcn) { |
|
93
|
1 |
|
$p = $this->telegramProviderFactory->get($fqcn); |
|
94
|
1 |
|
$p->setAppConfig($this->appConfig); |
|
95
|
1 |
|
$providerSettings = $p->getSettings(); |
|
96
|
1 |
|
$providers[$providerSettings->name] = parent::getConfiguration($providerSettings); |
|
97
|
|
|
} |
|
98
|
1 |
|
return [ |
|
99
|
1 |
|
'provider' => 'none', |
|
100
|
1 |
|
'available_providers' => $providers, |
|
101
|
1 |
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
#[\Override] |
|
106
|
|
|
public function remove(?Settings $settings = null): void { |
|
107
|
1 |
|
foreach ($this->telegramProviderFactory->getFqcnList() as $fqcn) { |
|
108
|
1 |
|
$provider = $this->telegramProviderFactory->get($fqcn); |
|
109
|
1 |
|
$provider->setAppConfig($this->appConfig); |
|
110
|
1 |
|
$settings = $provider->getSettings(); |
|
111
|
1 |
|
parent::remove($settings); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
public function getProvider(string $providerName = ''): AProvider { |
|
116
|
2 |
|
if ($providerName) { |
|
117
|
|
|
$this->setProvider($providerName); |
|
118
|
|
|
} |
|
119
|
2 |
|
$providerName = $this->appConfig->getValueString(Application::APP_ID, 'telegram_provider_name'); |
|
120
|
2 |
|
if ($providerName === '') { |
|
121
|
2 |
|
throw new ConfigurationException(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->telegramProviderFactory->get($providerName); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function setProvider(string $provider): void { |
|
128
|
|
|
$this->appConfig->setValueString(Application::APP_ID, 'telegram_provider_name', $provider); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|