|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\AuthClient; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Collection is a storage for all auth clients in the application. |
|
12
|
|
|
* |
|
13
|
|
|
* Example application configuration: |
|
14
|
|
|
* |
|
15
|
|
|
* ```php |
|
16
|
|
|
* 'authClients' => [ |
|
17
|
|
|
* 'google' => [ |
|
18
|
|
|
* '__class' => Yiisoft\Yii\AuthClient\Clients\Google::class, |
|
19
|
|
|
* 'setClientId()' => ['google_client_id'], |
|
20
|
|
|
* 'setClientSecret()' => ['google_client_secret'], |
|
21
|
|
|
* ], |
|
22
|
|
|
* 'facebook' => [ |
|
23
|
|
|
* '__class' => Yiisoft\Yii\AuthClient\Clients\Facebook::class, |
|
24
|
|
|
* 'setClientId()' => ['facebook_client_id'], |
|
25
|
|
|
* 'setClientSecret()' => ['facebook_client_secret'], |
|
26
|
|
|
* ] |
|
27
|
|
|
* ... |
|
28
|
|
|
* ] |
|
29
|
|
|
* ``` |
|
30
|
|
|
*/ |
|
31
|
|
|
final class Collection |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var AuthClientInterface[]|array list of Auth clients with their configuration in format: 'clientName' => [...] |
|
35
|
|
|
*/ |
|
36
|
|
|
private array $clients; |
|
37
|
|
|
|
|
38
|
3 |
|
public function __construct(array $clients) |
|
39
|
|
|
{ |
|
40
|
3 |
|
$this->clients = $clients; |
|
41
|
3 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return AuthClientInterface[] list of auth clients. |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function getClients(): array |
|
47
|
|
|
{ |
|
48
|
1 |
|
$clients = []; |
|
49
|
1 |
|
foreach ($this->clients as $name => $client) { |
|
50
|
1 |
|
$clients[$name] = $this->getClient($name); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
return $clients; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array $clients list of auth clients indexed by their names |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function setClients(array $clients): void |
|
60
|
|
|
{ |
|
61
|
1 |
|
$this->clients = $clients; |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $name client name |
|
66
|
|
|
* |
|
67
|
|
|
* @throws InvalidArgumentException on non existing client request. |
|
68
|
|
|
* @return AuthClientInterface auth client instance. |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function getClient(string $name): AuthClientInterface |
|
71
|
|
|
{ |
|
72
|
2 |
|
if (!$this->hasClient($name)) { |
|
73
|
|
|
throw new InvalidArgumentException("Unknown auth client '{$name}'."); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
$client = $this->clients[$name]; |
|
77
|
2 |
|
if (!($client instanceof AuthClientInterface)) { |
|
78
|
|
|
throw new RuntimeException( |
|
79
|
|
|
'Client should be ClientInterface instance. "' . get_class($client) . '" given.' |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
2 |
|
return $client; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Checks if client exists in the hub. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name client id. |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool whether client exist. |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function hasClient(string $name): bool |
|
93
|
|
|
{ |
|
94
|
3 |
|
return array_key_exists($name, $this->clients); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|