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