|
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
|
2 |
|
#[\Override] |
|
51
|
|
|
public function getSettings(): Settings { |
|
52
|
2 |
|
if ($this->settings !== null) { |
|
53
|
2 |
|
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
|
1 |
|
#[\Override] |
|
64
|
|
|
public function remove(?Settings $settings = null): void { |
|
65
|
1 |
|
if (!is_object($settings)) { |
|
66
|
1 |
|
$settings = $this->getSettings(); |
|
67
|
|
|
} |
|
68
|
1 |
|
foreach ($settings->fields as $field) { |
|
69
|
1 |
|
$method = 'delete' . $this->toCamel($field->field); |
|
70
|
1 |
|
$this->{$method}(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
protected function toCamel(string $field): string { |
|
75
|
1 |
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $field))); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
#[\Override] |
|
79
|
|
|
public static function getProviderId(): string { |
|
80
|
2 |
|
$id = self::deriveIdFromFqcn(static::class); |
|
81
|
2 |
|
if ($id === null) { |
|
82
|
|
|
throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class); |
|
83
|
|
|
} |
|
84
|
2 |
|
return $id; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
private static function deriveIdFromFqcn(string $fqcn): ?string { |
|
88
|
2 |
|
$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\'; |
|
89
|
2 |
|
if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) { |
|
90
|
|
|
return null; |
|
91
|
|
|
} |
|
92
|
2 |
|
$rest = substr($fqcn, strlen($prefix)); |
|
93
|
2 |
|
$sep = strpos($rest, '\\'); |
|
94
|
2 |
|
if ($sep === false || substr($rest, $sep + 1) !== 'Gateway') { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
2 |
|
$type = substr($rest, 0, $sep); |
|
98
|
2 |
|
return $type !== '' ? strtolower($type) : null; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|