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; |
11
|
|
|
|
12
|
|
|
use OCA\TwoFactorGateway\AppInfo\Application; |
13
|
|
|
use OCA\TwoFactorGateway\Exception\ConfigurationException; |
14
|
|
|
use OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\IProvider; |
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
|
|
|
use Symfony\Component\Console\Question\Question; |
23
|
|
|
|
24
|
|
|
class Gateway extends AGateway { |
25
|
1 |
|
public function __construct( |
26
|
|
|
public IAppConfig $appConfig, |
27
|
|
|
private Factory $smsProviderFactory, |
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->smsProviderFactory->getFqcnList(); |
40
|
|
|
$names = []; |
41
|
|
|
$providers = []; |
42
|
|
|
foreach ($namespaces as $ns) { |
43
|
|
|
$provider = $this->smsProviderFactory->get($ns); |
44
|
|
|
$providers[] = $provider; |
45
|
|
|
$names[] = $provider->getSettings()->name; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$helper = new QuestionHelper(); |
49
|
|
|
$choiceQuestion = new ChoiceQuestion('Please choose a SMS provider:', $names); |
50
|
|
|
$name = $helper->ask($input, $output, $choiceQuestion); |
51
|
|
|
$selectedIndex = array_search($name, $names); |
52
|
|
|
|
53
|
|
|
$provider = $providers[$selectedIndex]; |
54
|
|
|
|
55
|
|
|
foreach ($provider->getSettings()->fields as $field) { |
56
|
|
|
$id = $field->field; |
57
|
|
|
$prompt = $field->prompt . ' '; |
58
|
|
|
$defaultVal = $field->default ?? null; |
59
|
|
|
$optional = (bool)($field->optional ?? false); |
60
|
|
|
|
61
|
|
|
$answer = (string)$helper->ask($input, $output, new Question($prompt, $defaultVal)); |
62
|
|
|
|
63
|
|
|
if ($optional && $answer === '') { |
64
|
|
|
$method = 'delete' . $this->toCamel($id); |
65
|
|
|
$provider->{$method}(); |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$method = 'set' . $this->toCamel($id); |
70
|
|
|
$provider->{$method}($answer); |
71
|
|
|
} |
72
|
|
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
#[\Override] |
76
|
|
|
public function createSettings(): Settings { |
77
|
|
|
try { |
78
|
1 |
|
$settings = $this->getProvider()->getSettings(); |
79
|
1 |
|
} catch (ConfigurationException) { |
80
|
1 |
|
$settings = new Settings( |
81
|
1 |
|
name: 'SMS', |
82
|
1 |
|
); |
83
|
|
|
} |
84
|
1 |
|
return $settings; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
#[\Override] |
88
|
|
|
public function isComplete(?Settings $settings = null): bool { |
89
|
1 |
|
if ($settings === null) { |
90
|
|
|
try { |
91
|
1 |
|
$provider = $this->getProvider(); |
92
|
1 |
|
} catch (ConfigurationException) { |
93
|
1 |
|
return false; |
94
|
|
|
} |
95
|
|
|
$settings = $provider->getSettings(); |
96
|
|
|
} |
97
|
|
|
return parent::isComplete($settings); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
#[\Override] |
101
|
|
|
public function remove(?Settings $settings = null): void { |
102
|
1 |
|
foreach ($this->smsProviderFactory->getFqcnList() as $fqcn) { |
103
|
1 |
|
$provider = $this->smsProviderFactory->get($fqcn); |
104
|
1 |
|
$provider->setAppConfig($this->appConfig); |
105
|
1 |
|
$settings = $provider->getSettings(); |
106
|
1 |
|
parent::remove($settings); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
public function getProvider(string $providerName = ''): IProvider { |
111
|
2 |
|
if ($providerName) { |
112
|
|
|
$this->setProvider($providerName); |
113
|
|
|
} |
114
|
2 |
|
$providerName = $this->appConfig->getValueString(Application::APP_ID, 'sms_provider_name'); |
115
|
2 |
|
if ($providerName === '') { |
116
|
2 |
|
throw new ConfigurationException(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->smsProviderFactory->get($providerName); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setProvider(string $provider): void { |
123
|
|
|
$this->appConfig->setValueString(Application::APP_ID, 'sms_provider_name', $provider); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|