1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SocialiteProviders\Manager\Helpers; |
4
|
|
|
|
5
|
|
|
use SocialiteProviders\Manager\Config; |
6
|
|
|
use SocialiteProviders\Manager\Exception\MissingConfigException; |
7
|
|
|
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface; |
8
|
|
|
|
9
|
|
|
class ConfigRetriever implements ConfigRetrieverInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $providerName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $providerIdentifier; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $servicesArray; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $additionalConfigKeys; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $providerName |
33
|
2 |
|
* @param array $additionalConfigKeys |
34
|
|
|
* |
35
|
2 |
|
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException |
36
|
|
|
* |
37
|
2 |
|
* @return \SocialiteProviders\Manager\Contracts\ConfigInterface |
38
|
2 |
|
*/ |
39
|
1 |
|
public function fromServices($providerName, array $additionalConfigKeys = []) |
40
|
1 |
|
{ |
41
|
|
|
$this->providerName = $providerName; |
42
|
1 |
|
$this->getConfigFromServicesArray($providerName); |
43
|
1 |
|
|
44
|
|
|
$this->additionalConfigKeys = $additionalConfigKeys; |
45
|
|
|
|
46
|
|
|
return new Config( |
47
|
|
|
$this->getFromServices('client_id'), |
48
|
|
|
$this->getFromServices('client_secret'), |
49
|
|
|
$this->getFromServices('redirect'), |
50
|
|
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
51
|
|
|
return $this->getFromServices(strtolower($key)); |
52
|
|
|
}) |
53
|
2 |
|
); |
54
|
|
|
} |
55
|
2 |
|
|
56
|
2 |
|
/** |
57
|
|
|
* @param array $configKeys |
58
|
1 |
|
* @param \Closure $keyRetrievalClosure |
59
|
1 |
|
* |
60
|
1 |
|
* @return array |
61
|
1 |
|
*/ |
62
|
1 |
|
protected function getConfigItems(array $configKeys, \Closure $keyRetrievalClosure) |
63
|
1 |
|
{ |
64
|
1 |
|
if (count($configKeys) < 1) { |
65
|
|
|
return []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->retrieveItemsFromConfig($configKeys, $keyRetrievalClosure); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $keys |
73
|
2 |
|
* @param \Closure $keyRetrievalClosure |
74
|
|
|
* |
75
|
2 |
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
protected function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClosure) |
78
|
|
|
{ |
79
|
2 |
|
$out = []; |
80
|
|
|
|
81
|
|
|
foreach ($keys as $key) { |
82
|
|
|
$out[$key] = $keyRetrievalClosure($key); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $out; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
/** |
89
|
|
|
* @param string $key |
90
|
2 |
|
* |
91
|
|
|
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException |
92
|
2 |
|
* |
93
|
2 |
|
* @return string |
94
|
2 |
|
*/ |
95
|
|
|
protected function getFromServices($key) |
96
|
2 |
|
{ |
97
|
|
|
$keyExists = array_key_exists($key, $this->servicesArray); |
98
|
|
|
|
99
|
|
|
// ADDITIONAL value is empty |
100
|
|
|
if (! $keyExists && $this->isAdditionalConfig($key)) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// REQUIRED value is empty |
105
|
1 |
|
if (! $keyExists) { |
106
|
|
|
throw new MissingConfigException("Missing services entry for {$this->providerName}.$key"); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
return $this->servicesArray[$key]; |
110
|
|
|
} |
111
|
1 |
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $providerName |
114
|
|
|
* |
115
|
|
|
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
protected function getConfigFromServicesArray($providerName) |
120
|
2 |
|
{ |
121
|
|
|
/** @var array $configArray */ |
122
|
2 |
|
$configArray = config("services.$providerName"); |
123
|
2 |
|
|
124
|
|
|
if (empty($configArray)) { |
125
|
2 |
|
// If we are running in console we should spoof values to make Socialite happy... |
126
|
1 |
|
if (app()->runningInConsole()) { |
127
|
|
|
$configArray = [ |
128
|
|
|
'client_id' => "{$this->providerIdentifier}_KEY", |
129
|
1 |
|
'client_secret' => "{$this->providerIdentifier}_SECRET", |
130
|
|
|
'redirect' => "{$this->providerIdentifier}_REDIRECT_URI", |
131
|
|
|
]; |
132
|
|
|
} else { |
133
|
|
|
throw new MissingConfigException("There is no services entry for $providerName"); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->servicesArray = $configArray; |
138
|
2 |
|
} |
139
|
|
|
|
140
|
2 |
|
/** |
141
|
2 |
|
* @param string $key |
142
|
|
|
* |
143
|
2 |
|
* @return bool |
144
|
1 |
|
*/ |
145
|
|
|
protected function isAdditionalConfig($key) |
146
|
1 |
|
{ |
147
|
|
|
return in_array(strtolower($key), $this->additionalConfigKeys, true); |
148
|
1 |
|
} |
149
|
|
|
} |
150
|
|
|
|