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 OCP\IAppConfig; |
17
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
21
|
|
|
use Symfony\Component\Console\Question\Question; |
22
|
|
|
|
23
|
|
|
class Gateway extends AGateway { |
24
|
|
|
public const SCHEMA = [ |
25
|
|
|
'name' => 'SMS', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
public IAppConfig $appConfig, |
30
|
|
|
private Factory $providerFactory, |
31
|
|
|
) { |
32
|
|
|
parent::__construct($appConfig); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
#[\Override] |
36
|
|
|
public function send(string $identifier, string $message, array $extra = []): void { |
37
|
|
|
$this->getProvider()->send($identifier, $message); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
#[\Override] |
41
|
|
|
final public function cliConfigure(InputInterface $input, OutputInterface $output): int { |
42
|
|
|
$namespaces = $this->providerFactory->getFqcnList(); |
43
|
|
|
$schemas = []; |
44
|
|
|
foreach ($namespaces as $ns) { |
45
|
|
|
$schemas[] = $ns::SCHEMA; |
46
|
|
|
} |
47
|
|
|
$names = array_column($schemas, 'name'); |
48
|
|
|
|
49
|
|
|
$helper = new QuestionHelper(); |
50
|
|
|
$choiceQuestion = new ChoiceQuestion('Please choose a SMS provider:', $names); |
51
|
|
|
$name = $helper->ask($input, $output, $choiceQuestion); |
52
|
|
|
$selectedIndex = array_search($name, $names); |
53
|
|
|
$schema = $schemas[$selectedIndex]; |
54
|
|
|
|
55
|
|
|
$provider = $this->getProvider($namespaces[$selectedIndex]::getProviderId()); |
56
|
|
|
|
57
|
|
|
foreach ($schema['fields'] as $field) { |
58
|
|
|
$id = $field['field']; |
59
|
|
|
$prompt = $field['prompt'] . ' '; |
60
|
|
|
$defaultVal = $field['default'] ?? null; |
61
|
|
|
$optional = (bool)($field['optional'] ?? false); |
62
|
|
|
|
63
|
|
|
$answer = (string)$helper->ask($input, $output, new Question($prompt, $defaultVal)); |
64
|
|
|
|
65
|
|
|
if ($optional && $answer === '') { |
66
|
|
|
$method = 'delete' . $this->toCamel($id); |
67
|
|
|
$provider->{$method}(); |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$method = 'set' . $this->toCamel($id); |
72
|
|
|
$provider->{$method}($answer); |
73
|
|
|
} |
74
|
|
|
return 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
#[\Override] |
78
|
|
|
public function getSettings(): array { |
79
|
|
|
try { |
80
|
|
|
$provider = $this->getProvider(); |
81
|
|
|
} catch (ConfigurationException) { |
82
|
|
|
return static::SCHEMA; |
83
|
|
|
} |
84
|
|
|
return $provider::SCHEMA; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
#[\Override] |
88
|
|
|
public function isComplete(array $schema = []): bool { |
89
|
|
|
if (empty($schema)) { |
90
|
|
|
try { |
91
|
|
|
$provider = $this->getProvider(); |
92
|
|
|
} catch (ConfigurationException) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
$schema = $provider::SCHEMA; |
96
|
|
|
} |
97
|
|
|
return parent::isComplete($schema); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
#[\Override] |
101
|
|
|
public function remove(array $schema = []): void { |
102
|
|
|
if (empty($schema)) { |
103
|
|
|
$schema = static::SCHEMA; |
104
|
|
|
} |
105
|
|
|
parent::remove($schema); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getProvider(string $providerName = ''): IProvider { |
109
|
|
|
if ($providerName) { |
110
|
|
|
$this->setProvider($providerName); |
111
|
|
|
} |
112
|
|
|
$providerName = $this->appConfig->getValueString(Application::APP_ID, 'sms_provider_name'); |
113
|
|
|
if ($providerName === '') { |
114
|
|
|
throw new ConfigurationException(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->providerFactory->get($providerName); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function setProvider(string $provider): void { |
121
|
|
|
$this->appConfig->setValueString(Application::APP_ID, 'sms_provider_name', $provider); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|