|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace OCA\TwoFactorGateway\Provider\Gateway; |
|
11
|
|
|
|
|
12
|
|
|
use OCA\TwoFactorGateway\AppInfo\Application; |
|
13
|
|
|
use OCA\TwoFactorGateway\Exception\MessageTransmissionException; |
|
14
|
|
|
use OCA\TwoFactorGateway\Provider\Settings; |
|
15
|
|
|
use OCP\IAppConfig; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AGateway implements IGateway { |
|
20
|
|
|
use TConfigurable; |
|
|
|
|
|
|
21
|
|
|
public const SCHEMA = []; |
|
22
|
|
|
protected ?Settings $settings = null; |
|
23
|
|
|
|
|
24
|
1 |
|
public function __construct( |
|
25
|
|
|
public IAppConfig $appConfig, |
|
26
|
|
|
) { |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @throws MessageTransmissionException |
|
31
|
|
|
*/ |
|
32
|
|
|
#[\Override] |
|
33
|
|
|
abstract public function send(string $identifier, string $message, array $extra = []): void; |
|
34
|
|
|
|
|
35
|
2 |
|
#[\Override] |
|
36
|
|
|
public function isComplete(?Settings $settings = null): bool { |
|
37
|
2 |
|
if (!is_object($settings)) { |
|
38
|
2 |
|
$settings = $this->getSettings(); |
|
39
|
|
|
} |
|
40
|
2 |
|
$savedKeys = $this->appConfig->getKeys(Application::APP_ID); |
|
41
|
2 |
|
$providerId = $settings->id ?? $this->getProviderId(); |
|
42
|
2 |
|
$fields = []; |
|
43
|
2 |
|
foreach ($settings->fields as $field) { |
|
44
|
2 |
|
$fields[] = self::keyFromFieldName($providerId, $field->field); |
|
45
|
|
|
} |
|
46
|
2 |
|
$intersect = array_intersect($fields, $savedKeys); |
|
47
|
2 |
|
return count($intersect) === count($fields); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
#[\Override] |
|
51
|
|
|
public function getConfiguration(?Settings $settings = null): array { |
|
52
|
1 |
|
if (!is_object($settings)) { |
|
53
|
1 |
|
$settings = $this->getSettings(); |
|
54
|
|
|
} |
|
55
|
1 |
|
$providerId = $settings->id ?? $this->getProviderId(); |
|
56
|
1 |
|
$config = []; |
|
57
|
1 |
|
foreach ($settings->fields as $field) { |
|
58
|
1 |
|
$config[$field->field] = $this->appConfig->getValueString( |
|
59
|
1 |
|
Application::APP_ID, |
|
60
|
1 |
|
self::keyFromFieldName($providerId, $field->field), |
|
61
|
1 |
|
$field->default, |
|
62
|
1 |
|
); |
|
63
|
|
|
} |
|
64
|
1 |
|
return $config; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
#[\Override] |
|
68
|
|
|
public function getSettings(): Settings { |
|
69
|
5 |
|
if ($this->settings !== null) { |
|
70
|
5 |
|
return $this->settings; |
|
71
|
|
|
} |
|
72
|
1 |
|
$this->settings = $this->createSettings(); |
|
73
|
1 |
|
$this->settings->id = $this->settings->id ?? $this->getProviderId(); |
|
74
|
1 |
|
return $this->settings; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
#[\Override] |
|
78
|
|
|
abstract public function cliConfigure(InputInterface $input, OutputInterface $output): int; |
|
79
|
|
|
|
|
80
|
3 |
|
#[\Override] |
|
81
|
|
|
public function remove(?Settings $settings = null): void { |
|
82
|
3 |
|
if (!is_object($settings)) { |
|
83
|
1 |
|
$settings = $this->getSettings(); |
|
84
|
|
|
} else { |
|
85
|
2 |
|
$this->settings = $settings; |
|
86
|
|
|
} |
|
87
|
3 |
|
foreach ($settings->fields as $field) { |
|
88
|
3 |
|
$method = 'delete' . $this->toCamel($field->field); |
|
89
|
3 |
|
$this->{$method}(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
protected function toCamel(string $field): string { |
|
94
|
4 |
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $field))); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
5 |
|
#[\Override] |
|
98
|
|
|
public function getProviderId(): string { |
|
99
|
5 |
|
$id = self::deriveIdFromFqcn(static::class); |
|
100
|
5 |
|
if ($id === null) { |
|
101
|
|
|
throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class); |
|
102
|
|
|
} |
|
103
|
5 |
|
return $id; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
5 |
|
private static function deriveIdFromFqcn(string $fqcn): ?string { |
|
107
|
5 |
|
$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\'; |
|
108
|
5 |
|
if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) { |
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
5 |
|
$rest = substr($fqcn, strlen($prefix)); |
|
112
|
5 |
|
$sep = strpos($rest, '\\'); |
|
113
|
5 |
|
if ($sep === false || substr($rest, $sep + 1) !== 'Gateway') { |
|
114
|
|
|
return null; |
|
115
|
|
|
} |
|
116
|
5 |
|
$type = substr($rest, 0, $sep); |
|
117
|
5 |
|
return $type !== '' ? strtolower($type) : null; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|