|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Parroauth2\Client\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Cache pool for provider configurations |
|
9
|
|
|
*/ |
|
10
|
|
|
final class ProviderConfigPool |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var CacheInterface|null |
|
14
|
|
|
*/ |
|
15
|
|
|
private $cache; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Default provider config to apply |
|
19
|
|
|
* |
|
20
|
|
|
* @var array<string, mixed> |
|
21
|
|
|
*/ |
|
22
|
|
|
private $defaults; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ProviderConfigPool constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param CacheInterface|null $cache The cache system. If null the cache is disabled |
|
28
|
|
|
* @param array<string, mixed> $defaults Default provider config to apply |
|
29
|
|
|
*/ |
|
30
|
190 |
|
public function __construct(?CacheInterface $cache = null, array $defaults = []) |
|
31
|
|
|
{ |
|
32
|
190 |
|
$this->cache = $cache; |
|
33
|
190 |
|
$this->defaults = $defaults; |
|
34
|
190 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get a provider config from the cache pool |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $url The provider URL |
|
40
|
|
|
* |
|
41
|
|
|
* @return ProviderConfig|null The cached config, or null if not in cache |
|
42
|
|
|
*/ |
|
43
|
93 |
|
public function get(string $url): ?ProviderConfig |
|
44
|
|
|
{ |
|
45
|
93 |
|
if (!$this->cache) { |
|
46
|
89 |
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
$config = $this->cache->get(self::urlToKey($url)); |
|
50
|
|
|
|
|
51
|
4 |
|
if (!$config instanceof ProviderConfig) { |
|
52
|
3 |
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
$config->setCache($this->cache); |
|
56
|
|
|
|
|
57
|
2 |
|
return $config; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Creates the provider config |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $url The provider URL |
|
64
|
|
|
* @param array<string, mixed> $config The config data |
|
65
|
|
|
* @param bool $openid Does the provider is an OpenID connect provider ? |
|
66
|
|
|
* |
|
67
|
|
|
* @return ProviderConfig |
|
68
|
|
|
*/ |
|
69
|
185 |
|
public function create(string $url, array $config, bool $openid): ProviderConfig |
|
70
|
|
|
{ |
|
71
|
185 |
|
$config = new ProviderConfig($url, $config + $this->defaults, $openid); |
|
72
|
185 |
|
$config->setCache($this->cache); |
|
73
|
|
|
|
|
74
|
185 |
|
return $config; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Creates the provider config from the config array |
|
79
|
|
|
* Use issuer as URL |
|
80
|
|
|
* |
|
81
|
|
|
* @param array<string, mixed> $config The provider config |
|
82
|
|
|
* @param bool|null $openid Does the provider is an OpenID connect provider ? |
|
83
|
|
|
* If null, try to detect is it's an OpenID config |
|
84
|
|
|
* |
|
85
|
|
|
* @return ProviderConfig |
|
86
|
|
|
*/ |
|
87
|
76 |
|
public function createFromArray(array $config, ?bool $openid = null): ProviderConfig |
|
88
|
|
|
{ |
|
89
|
76 |
|
if ($openid === null) { |
|
90
|
1 |
|
$openid = !empty($config['userinfo_endpoint']); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
76 |
|
return $this->create($config['issuer'] ?? '', $config, $openid); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Creates the provider config from a JSON object |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $url The provider URL |
|
100
|
|
|
* @param string $config The JSON config |
|
101
|
|
|
* @param bool $openid Does the provider is an OpenID connect provider |
|
102
|
|
|
* |
|
103
|
|
|
* @return ProviderConfig |
|
104
|
|
|
*/ |
|
105
|
89 |
|
public function createFromJson(string $url, string $config, bool $openid): ProviderConfig |
|
106
|
|
|
{ |
|
107
|
89 |
|
return $this->create($url, (array) json_decode($config, true), $openid); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Convert an URL to a cache key |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $url The provider URL |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
5 |
|
public static function urlToKey(string $url): string |
|
118
|
|
|
{ |
|
119
|
5 |
|
return hash('sha256', $url); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|