|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Krtv\Bundle\SingleSignOnIdentityProviderBundle\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ServiceManager |
|
10
|
|
|
* @package Krtv\Bundle\SingleSignOnIdentityProviderBundle\Manager |
|
11
|
|
|
*/ |
|
12
|
|
|
class ServiceManager |
|
13
|
|
|
{ |
|
14
|
|
|
const SERVICE_SESSION_NS = '_target'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var RequestStack |
|
18
|
|
|
*/ |
|
19
|
|
|
private $requestStack; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var SessionInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $session; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $firewall; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ServiceProviderInterface[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $services; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $options; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param RequestStack $requestStack |
|
43
|
|
|
* @param SessionInterface $session |
|
44
|
|
|
* @param string $firewall |
|
45
|
|
|
* @param array $services |
|
46
|
|
|
* @param array $options |
|
47
|
|
|
*/ |
|
48
|
17 |
|
public function __construct(RequestStack $requestStack, SessionInterface $session, $firewall = 'main', array $services = array(), $options = array()) |
|
49
|
|
|
{ |
|
50
|
17 |
|
if (count($services) === 0) { |
|
51
|
1 |
|
throw new \RuntimeException('No ServiceProvider managers found. Make sure that you have at least one ServiceProvider manager tagged with "sso.service_provider"'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
16 |
|
$this->requestStack = $requestStack; |
|
55
|
16 |
|
$this->session = $session; |
|
56
|
16 |
|
$this->firewall = $firewall; |
|
57
|
16 |
|
$this->services = $services; |
|
58
|
16 |
|
$this->options = $options; |
|
59
|
16 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function getServiceParameter() |
|
65
|
|
|
{ |
|
66
|
5 |
|
return $this->options['service_parameter']; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function getServiceExtraParameter() |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->options['service_extra_parameter']; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getTargetPathParameter() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->options['target_path_parameter']; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string[] |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function getServices() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return array_keys($this->services); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $service |
|
95
|
|
|
* @return ServiceProviderInterface |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function getServiceManager($service) |
|
98
|
|
|
{ |
|
99
|
2 |
|
if (!isset($this->services[$service])) { |
|
100
|
1 |
|
throw new \InvalidArgumentException('Unknown service ' . $service); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
return $this->services[$service]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string|null |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function getRequestService() |
|
110
|
|
|
{ |
|
111
|
1 |
|
$request = $this->requestStack->getMasterRequest(); |
|
112
|
|
|
|
|
113
|
1 |
|
return $request->get($this->getServiceParameter()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array|null |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getRequestServiceExtra() |
|
120
|
|
|
{ |
|
121
|
|
|
$request = $this->requestStack->getMasterRequest(); |
|
122
|
|
|
|
|
123
|
|
|
return $request->get($this->getServiceExtraParameter()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get target path from the request. |
|
128
|
|
|
* |
|
129
|
|
|
* @return array|null |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getRequestTargetPath() |
|
132
|
|
|
{ |
|
133
|
|
|
$request = $this->requestStack->getMasterRequest(); |
|
134
|
|
|
|
|
135
|
|
|
return $request->get($this->getTargetPathParameter()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get target service name. |
|
140
|
|
|
* |
|
141
|
|
|
* @return string|null |
|
142
|
|
|
*/ |
|
143
|
3 |
|
public function getSessionService() |
|
144
|
|
|
{ |
|
145
|
3 |
|
return $this->session->get($this->getSessionKey()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get extra data. |
|
150
|
|
|
* |
|
151
|
|
|
* @return array|null |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getSessionExtra() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->session->get($this->getSessionExtraKey()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get target path from _security.ID.target_path. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $default |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getSessionTargetPath($default = '/') |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->session->get($this->getSessionTargetPathKey(), $default); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Save target service name. |
|
171
|
|
|
* |
|
172
|
|
|
* @param $service |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function setSessionService($service) |
|
176
|
|
|
{ |
|
177
|
1 |
|
$this->session->set($this->getSessionKey(), $service); |
|
178
|
|
|
|
|
179
|
1 |
|
return true; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Save target path to _security.ID.target_path. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $targetPath |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
public function setSessionTargetPath($targetPath) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->session->set($this->getSessionTargetPathKey(), $targetPath); |
|
191
|
|
|
|
|
192
|
|
|
return true; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Save extra data. |
|
197
|
|
|
* |
|
198
|
|
|
* @param array $extra |
|
199
|
|
|
* |
|
200
|
|
|
* @return bool |
|
201
|
|
|
*/ |
|
202
|
|
|
public function setSessionExtra(array $extra = array()) |
|
203
|
|
|
{ |
|
204
|
|
|
$this->session->set($this->getSessionExtraKey(), $extra); |
|
205
|
|
|
|
|
206
|
|
|
return true; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return bool |
|
211
|
|
|
*/ |
|
212
|
1 |
|
public function setDefaults() |
|
213
|
|
|
{ |
|
214
|
1 |
|
if ($this->getSessionService() === false) { |
|
215
|
1 |
|
return false; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
$this->session->set($this->getSessionKey(), false); |
|
219
|
|
|
|
|
220
|
1 |
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Clear services management session variables. |
|
225
|
|
|
*/ |
|
226
|
1 |
|
public function clear() |
|
227
|
|
|
{ |
|
228
|
1 |
|
$this->session->remove($this->getSessionKey()); |
|
229
|
1 |
|
$this->session->remove($this->getSessionExtraKey()); |
|
230
|
1 |
|
$this->session->remove($this->getSessionTargetPathKey()); |
|
231
|
1 |
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return string |
|
235
|
|
|
*/ |
|
236
|
4 |
|
private function getSessionKey() |
|
237
|
|
|
{ |
|
238
|
4 |
|
return sprintf('%s/%s', static::SERVICE_SESSION_NS, $this->getServiceParameter()); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return string |
|
243
|
|
|
*/ |
|
244
|
1 |
|
private function getSessionExtraKey() |
|
245
|
|
|
{ |
|
246
|
1 |
|
return sprintf('%s/%s', static::SERVICE_SESSION_NS, $this->getServiceExtraParameter()); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @return string |
|
251
|
|
|
*/ |
|
252
|
1 |
|
private function getSessionTargetPathKey() |
|
253
|
|
|
{ |
|
254
|
1 |
|
return sprintf('_security.%s.target_path', $this->firewall); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|