|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SocialiteProviders\Manager\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use SocialiteProviders\Manager\Config; |
|
6
|
|
|
use SocialiteProviders\Manager\SocialiteWasCalled; |
|
7
|
|
|
use SocialiteProviders\Manager\Exception\MissingConfigException; |
|
8
|
|
|
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface; |
|
9
|
|
|
|
|
10
|
|
|
class ConfigRetriever implements ConfigRetrieverInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $providerName; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $providerIdentifier; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $servicesArray; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $additionalConfigKeys; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
2 |
|
* @param string $providerIdentifier |
|
34
|
|
|
* @param array $additionalConfigKeys |
|
35
|
2 |
|
* |
|
36
|
|
|
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException |
|
37
|
2 |
|
* |
|
38
|
2 |
|
* @return \SocialiteProviders\Manager\Contracts\ConfigInterface |
|
39
|
1 |
|
*/ |
|
40
|
1 |
|
public function fromEnv($providerIdentifier, array $additionalConfigKeys = []) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->providerIdentifier = $providerIdentifier; |
|
43
|
1 |
|
$this->additionalConfigKeys = $additionalConfigKeys; |
|
44
|
|
|
|
|
45
|
|
|
return new Config( |
|
46
|
|
|
$this->getFromEnv('KEY'), |
|
47
|
|
|
$this->getFromEnv('SECRET'), |
|
48
|
|
|
$this->getFromEnv('REDIRECT_URI'), |
|
49
|
|
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
|
50
|
|
|
return $this->getFromEnv(strtoupper($key)); |
|
51
|
|
|
})); |
|
52
|
|
|
} |
|
53
|
2 |
|
|
|
54
|
|
|
/** |
|
55
|
2 |
|
* @param string $providerName |
|
56
|
2 |
|
* @param array $additionalConfigKeys |
|
57
|
|
|
* |
|
58
|
1 |
|
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException |
|
59
|
1 |
|
* |
|
60
|
1 |
|
* @return \SocialiteProviders\Manager\Contracts\ConfigInterface |
|
61
|
1 |
|
*/ |
|
62
|
1 |
|
public function fromServices($providerName, array $additionalConfigKeys = []) |
|
63
|
1 |
|
{ |
|
64
|
1 |
|
$this->providerName = $providerName; |
|
65
|
|
|
$this->getConfigFromServicesArray($providerName); |
|
66
|
|
|
|
|
67
|
|
|
$this->additionalConfigKeys = $additionalConfigKeys; |
|
68
|
|
|
|
|
69
|
|
|
return new Config( |
|
70
|
|
|
$this->getFromServices('client_id'), |
|
71
|
|
|
$this->getFromServices('client_secret'), |
|
72
|
|
|
$this->getFromServices('redirect'), |
|
73
|
2 |
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
|
74
|
|
|
return $this->getFromServices(strtolower($key)); |
|
75
|
2 |
|
})); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
2 |
|
* @param array $configKeys |
|
80
|
|
|
* @param \Closure $keyRetrievalClosure |
|
81
|
|
|
* |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function getConfigItems(array $configKeys, \Closure $keyRetrievalClosure) |
|
85
|
|
|
{ |
|
86
|
|
|
if (count($configKeys) < 1) { |
|
87
|
|
|
return []; |
|
88
|
2 |
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $this->retrieveItemsFromConfig($configKeys, $keyRetrievalClosure); |
|
91
|
|
|
} |
|
92
|
2 |
|
|
|
93
|
2 |
|
/** |
|
94
|
2 |
|
* @param array $keys |
|
95
|
|
|
* @param \Closure $keyRetrievalClosure |
|
96
|
2 |
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClosure) |
|
100
|
|
|
{ |
|
101
|
|
|
$out = []; |
|
102
|
|
|
|
|
103
|
|
|
foreach ($keys as $key) { |
|
104
|
|
|
$out[$key] = $keyRetrievalClosure($key); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return $out; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
1 |
|
* @param string $key |
|
112
|
|
|
* |
|
113
|
|
|
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function getFromServices($key) |
|
118
|
|
|
{ |
|
119
|
|
|
$keyExists = array_key_exists($key, $this->servicesArray); |
|
120
|
2 |
|
|
|
121
|
|
|
// ADDITIONAL value is empty |
|
122
|
2 |
|
if (! $keyExists && $this->isAdditionalConfig($key)) { |
|
123
|
2 |
|
return; |
|
124
|
|
|
} |
|
125
|
2 |
|
|
|
126
|
1 |
|
// REQUIRED value is empty |
|
127
|
|
|
if (! $keyExists) { |
|
128
|
|
|
throw new MissingConfigException("Missing services entry for {$this->providerName}.$key"); |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->servicesArray[$key]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $key |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException |
|
138
|
2 |
|
* |
|
139
|
|
|
* @return string |
|
140
|
2 |
|
*/ |
|
141
|
2 |
|
protected function getFromEnv($key) |
|
142
|
|
|
{ |
|
143
|
2 |
|
$providerKey = "{$this->providerIdentifier}_{$key}"; |
|
144
|
1 |
|
$item = env($providerKey); |
|
145
|
|
|
|
|
146
|
1 |
|
// ADDITIONAL value is empty |
|
147
|
|
|
if (empty($item) && $this->isAdditionalConfig($key)) { |
|
148
|
1 |
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// REQUIRED value is empty |
|
152
|
|
|
if (empty($item)) { |
|
153
|
|
|
// If we are running in console we should spoof values to make Socialite happy... |
|
154
|
|
|
if (! app()->runningInConsole()) { |
|
155
|
|
|
throw new MissingConfigException("Configuration for $providerKey is missing."); |
|
156
|
|
|
} |
|
157
|
|
|
$item = $providerKey; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $item; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param string $providerName |
|
165
|
|
|
* |
|
166
|
|
|
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException |
|
167
|
|
|
* |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function getConfigFromServicesArray($providerName) |
|
171
|
|
|
{ |
|
172
|
|
|
/** @var array $configArray */ |
|
173
|
|
|
$configArray = config("services.$providerName"); |
|
174
|
|
|
|
|
175
|
|
|
if (empty($configArray)) { |
|
176
|
|
|
// If we are running in console we should spoof values to make Socialite happy... |
|
177
|
|
|
if (app()->runningInConsole()) { |
|
178
|
|
|
$configArray = [ |
|
179
|
|
|
'client_id' => "{$this->providerIdentifier}_KEY", |
|
180
|
|
|
'client_secret' => "{$this->providerIdentifier}_SECRET", |
|
181
|
|
|
'redirect' => "{$this->providerIdentifier}_REDIRECT_URI", |
|
182
|
|
|
]; |
|
183
|
|
|
} else { |
|
184
|
|
|
throw new MissingConfigException("There is no services entry for $providerName"); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->servicesArray = $configArray; |
|
189
|
|
|
|
|
190
|
|
|
return $this->servicesArray; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param string $key |
|
195
|
|
|
* |
|
196
|
|
|
* @return bool |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function isAdditionalConfig($key) |
|
199
|
|
|
{ |
|
200
|
|
|
return in_array(strtolower($key), $this->additionalConfigKeys); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|