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
|
1 |
|
#[\Override] |
36
|
|
|
public function isComplete(?Settings $settings = null): bool { |
37
|
1 |
|
if (!is_object($settings)) { |
38
|
1 |
|
$settings = $this->getSettings(); |
39
|
|
|
} |
40
|
1 |
|
$savedKeys = $this->appConfig->getKeys(Application::APP_ID); |
41
|
1 |
|
$providerId = $settings->id ?? $this->getProviderId(); |
42
|
1 |
|
$fields = []; |
43
|
1 |
|
foreach ($settings->fields as $field) { |
44
|
1 |
|
$fields[] = $providerId . '_' . $field->field; |
45
|
|
|
} |
46
|
1 |
|
$intersect = array_intersect($fields, $savedKeys); |
47
|
1 |
|
return count($intersect) === count($fields); |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
#[\Override] |
51
|
|
|
public function getSettings(): Settings { |
52
|
5 |
|
if ($this->settings !== null) { |
53
|
5 |
|
return $this->settings; |
54
|
|
|
} |
55
|
1 |
|
$this->settings = $this->createSettings(); |
56
|
1 |
|
$this->settings->id = $this->settings->id ?? $this->getProviderId(); |
57
|
1 |
|
return $this->settings; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
#[\Override] |
61
|
|
|
abstract public function cliConfigure(InputInterface $input, OutputInterface $output): int; |
62
|
|
|
|
63
|
3 |
|
#[\Override] |
64
|
|
|
public function remove(?Settings $settings = null): void { |
65
|
3 |
|
if (!is_object($settings)) { |
66
|
1 |
|
$settings = $this->getSettings(); |
67
|
|
|
} else { |
68
|
2 |
|
$this->settings = $settings; |
69
|
|
|
} |
70
|
3 |
|
foreach ($settings->fields as $field) { |
71
|
3 |
|
$method = 'delete' . $this->toCamel($field->field); |
72
|
3 |
|
$this->{$method}(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
protected function toCamel(string $field): string { |
77
|
3 |
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $field))); |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
#[\Override] |
81
|
|
|
public function getProviderId(): string { |
82
|
4 |
|
$id = self::deriveIdFromFqcn(static::class); |
83
|
4 |
|
if ($id === null) { |
84
|
|
|
throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class); |
85
|
|
|
} |
86
|
4 |
|
return $id; |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
private static function deriveIdFromFqcn(string $fqcn): ?string { |
90
|
4 |
|
$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\'; |
91
|
4 |
|
if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
4 |
|
$rest = substr($fqcn, strlen($prefix)); |
95
|
4 |
|
$sep = strpos($rest, '\\'); |
96
|
4 |
|
if ($sep === false || substr($rest, $sep + 1) !== 'Gateway') { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
4 |
|
$type = substr($rest, 0, $sep); |
100
|
4 |
|
return $type !== '' ? strtolower($type) : null; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|