1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Krtv\Bundle\SingleSignOnServiceProviderBundle\Context; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AuthenticationContext |
11
|
|
|
* @package Krtv\Bundle\SingleSignOnServiceProviderBundle\Context |
12
|
|
|
*/ |
13
|
|
|
class AuthenticationContext |
14
|
|
|
{ |
15
|
|
|
const CTX_TARGET_PATH = '_target_path'; |
16
|
|
|
const CTX_FAILURE_PATH = '_failure_path'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Firewall options. |
20
|
|
|
* |
21
|
|
|
* @var ParameterBag |
22
|
|
|
*/ |
23
|
|
|
private $options; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Options with a highest priority of firewall options. |
27
|
|
|
* |
28
|
|
|
* @var ParameterBag |
29
|
|
|
*/ |
30
|
|
|
private $context; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Options to be proxied to IdP. |
34
|
|
|
* |
35
|
|
|
* @var ParameterBag |
36
|
|
|
*/ |
37
|
|
|
private $extra; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Options to be proxied to IdP and come back to SP. |
41
|
|
|
* |
42
|
|
|
* @var ParameterBag |
43
|
|
|
*/ |
44
|
|
|
private $proxy; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ParameterBag $options |
48
|
|
|
*/ |
49
|
|
|
public function __construct(ParameterBag $options) |
50
|
|
|
{ |
51
|
|
|
$this->options = $options; |
52
|
|
|
$this->context = new ParameterBag(); |
53
|
|
|
$this->extra = new ParameterBag(); |
54
|
|
|
$this->proxy = new ParameterBag(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $name |
59
|
|
|
* @param string $value |
60
|
|
|
* @param bool $proxy |
61
|
|
|
*/ |
62
|
|
|
public function setServiceExtra($name, $value, $proxy = false) |
63
|
|
|
{ |
64
|
|
|
$this->extra->set($name, $value); |
65
|
|
|
|
66
|
|
|
if ($proxy === true) { |
67
|
|
|
$this->proxy->set($name, true); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
* @param string $value |
74
|
|
|
*/ |
75
|
|
|
public function setContext($name, $value) |
76
|
|
|
{ |
77
|
|
|
$this->context->set($name, $value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string|null |
82
|
|
|
*/ |
83
|
|
|
public function getService() |
84
|
|
|
{ |
85
|
|
|
return $this->options->get('sso_service'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return ParameterBag |
90
|
|
|
*/ |
91
|
|
|
public function getServiceExtra() |
92
|
|
|
{ |
93
|
|
|
return $this->extra; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return ParameterBag |
98
|
|
|
*/ |
99
|
|
|
public function getServiceProxy() |
100
|
|
|
{ |
101
|
|
|
return $this->proxy; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return ParameterBag |
106
|
|
|
*/ |
107
|
|
|
public function getOptions() |
108
|
|
|
{ |
109
|
|
|
return $this->options; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Request $request |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getTargetPath(Request $request) |
118
|
|
|
{ |
119
|
|
|
if ($this->context->has(static::CTX_TARGET_PATH)) { |
120
|
|
|
return $this->context->get(static::CTX_TARGET_PATH); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($this->options->get('always_use_default_target_path') === true) { |
124
|
|
|
return $this->options->get('default_target_path'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($targetUrl = $request->get($this->options->get('target_path_parameter'), null, true)) { |
128
|
|
|
return $targetUrl; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$session = $request->getSession(); |
132
|
|
|
if ($targetUrl = $session->get(sprintf('_security.%s.target_path', $this->options->get('firewall_id')))) { |
133
|
|
|
return $targetUrl; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($this->options->get('use_referer') && $targetUrl = $request->headers->get('Referer')) { |
137
|
|
|
return $targetUrl; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->options->get('default_target_path'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Request $request |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function getFailurePath(Request $request) |
149
|
|
|
{ |
150
|
|
|
if ($this->context->has(static::CTX_FAILURE_PATH)) { |
151
|
|
|
return $this->context->get(static::CTX_FAILURE_PATH); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($failureUrl = $request->get($this->options->get('failure_path_parameter'), null, true)) { |
155
|
|
|
return $failureUrl; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$session = $request->getSession(); |
159
|
|
|
if ($failureUrl = $session->get(sprintf('_security.%s.failure_path', $this->options->get('firewall_id')))) { |
160
|
|
|
return $failureUrl; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this->options->get('failure_path'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param Request $request |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getOtpValidationPath(Request $request) |
172
|
|
|
{ |
173
|
|
|
$scheme = $this->options->get('sso_otp_scheme'); |
174
|
|
|
$host = $this->options->get('sso_otp_host'); |
175
|
|
|
$checkPath = $this->options->get('check_path'); |
176
|
|
|
|
177
|
|
|
if ($scheme !== null && $host !== null) { |
178
|
|
|
return sprintf('%s://%s%s', $scheme, $host, $checkPath); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $request->getUriForPath($checkPath); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|