|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MadWizard\WebAuthn\Metadata\Source; |
|
4
|
|
|
|
|
5
|
|
|
use MadWizard\WebAuthn\Metadata\Provider\Apple\AppleDevicesProvider; |
|
6
|
|
|
use MadWizard\WebAuthn\Metadata\Provider\MetadataProviderInterface; |
|
7
|
|
|
use UnexpectedValueException; |
|
8
|
|
|
|
|
9
|
|
|
final class BundledSource implements MetadataSourceInterface |
|
10
|
|
|
{ |
|
11
|
|
|
private const SETS = |
|
12
|
|
|
[ |
|
13
|
|
|
'apple' => false, |
|
14
|
|
|
// 'yubico-u2f' => true, |
|
15
|
|
|
]; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string[] |
|
19
|
|
|
* @phpstan-var array<string, class-string> |
|
20
|
|
|
*/ |
|
21
|
|
|
private const PROVIDERS = [ |
|
22
|
|
|
'apple' => AppleDevicesProvider::class, |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array<string, bool> |
|
27
|
|
|
*/ |
|
28
|
|
|
private $enabledSets; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(array $sets = ['@all']) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->enabledSets = self::SETS; |
|
33
|
|
|
foreach ($sets as $set) { |
|
34
|
|
|
if ($set === '') { |
|
35
|
|
|
throw new UnexpectedValueException('Empty set name'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($set === '@all') { |
|
39
|
|
|
$this->enabledSets = array_fill_keys(array_keys(self::SETS), true); |
|
40
|
|
|
} else { |
|
41
|
|
|
$add = true; |
|
42
|
|
|
if ($set[0] === '-') { |
|
43
|
|
|
$set = substr($set, 1); |
|
44
|
|
|
$add = false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (!isset(self::SETS[$set])) { |
|
48
|
|
|
throw new UnexpectedValueException(sprintf("Invalid set name '%s'.", $set)); |
|
49
|
|
|
} |
|
50
|
|
|
if ($add) { |
|
51
|
|
|
$this->enabledSets[$set] = true; |
|
52
|
|
|
} elseif (isset($this->enabledSets[$set])) { |
|
53
|
|
|
$this->enabledSets[$set] = false; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getEnableSets(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return array_keys(array_filter($this->enabledSets, static function ($value) { return $value; })); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return MetadataProviderInterface[] |
|
66
|
|
|
*/ |
|
67
|
|
|
public function createProviders(): array |
|
68
|
|
|
{ |
|
69
|
|
|
$providers = []; |
|
70
|
|
|
foreach ($this->enabledSets as $type => $enabled) { |
|
71
|
|
|
if ($enabled) { |
|
72
|
|
|
$className = self::PROVIDERS[$type]; |
|
73
|
|
|
$providers[] = new $className(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $providers; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|