1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\casserver\Controller\Traits; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\CAS\Constants as C; |
8
|
|
|
use SimpleSAML\Configuration; |
9
|
|
|
use SimpleSAML\Logger; |
10
|
|
|
use SimpleSAML\Module\casserver\Cas\ServiceValidator; |
11
|
|
|
use SimpleSAML\Module\casserver\Cas\TicketValidator; |
12
|
|
|
use SimpleSAML\Module\casserver\Http\XmlResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
|
16
|
|
|
trait UrlTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @deprecated |
20
|
|
|
* @see ServiceValidator |
21
|
|
|
* @param string $service |
22
|
|
|
* @param array $legal_service_urls |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
|
|
public function checkServiceURL(string $service, array $legal_service_urls): bool |
26
|
|
|
{ |
27
|
|
|
//delegate to ServiceValidator until all references to this can be cleaned up |
28
|
|
|
$config = Configuration::loadFromArray(['legal_service_urls' => $legal_service_urls]); |
29
|
|
|
$serviceValidator = new ServiceValidator($config); |
30
|
|
|
return $serviceValidator->checkServiceURL($service) !== null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $parameter |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function sanitize(string $parameter): string |
38
|
|
|
{ |
39
|
|
|
return TicketValidator::sanitize($parameter); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Parse the query Parameters from $_GET global and return them in an array. |
44
|
|
|
* |
45
|
|
|
* @param Request $request |
46
|
|
|
* @param array|null $sessionTicket |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function parseQueryParameters(Request $request, ?array $sessionTicket): array |
51
|
|
|
{ |
52
|
|
|
$forceAuthn = $this->getRequestParam($request, 'renew'); |
53
|
|
|
$sessionRenewId = !empty($sessionTicket['renewId']) ? $sessionTicket['renewId'] : null; |
54
|
|
|
|
55
|
|
|
$queryParameters = $request->query->all(); |
56
|
|
|
$requestParameters = $request->request->all(); |
57
|
|
|
|
58
|
|
|
$query = array_merge($requestParameters, $queryParameters); |
59
|
|
|
|
60
|
|
|
if ($sessionRenewId && $forceAuthn) { |
61
|
|
|
$query['renewId'] = $sessionRenewId; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (isset($query['language'])) { |
65
|
|
|
$query['language'] = is_string($query['language']) ? $query['language'] : null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $query; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Request $request |
73
|
|
|
* @param string $paramName |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function getRequestParam(Request $request, string $paramName): mixed |
78
|
|
|
{ |
79
|
|
|
return $request->query->get($paramName) ?? $request->request->get($paramName) ?? null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Request $request |
84
|
|
|
* @param string $method |
85
|
|
|
* @param bool $renew |
86
|
|
|
* @param string|null $target |
87
|
|
|
* @param string|null $ticket |
88
|
|
|
* @param string|null $service |
89
|
|
|
* @param string|null $pgtUrl |
90
|
|
|
* |
91
|
|
|
* @return XmlResponse |
92
|
|
|
*/ |
93
|
|
|
public function validate( |
94
|
|
|
Request $request, |
95
|
|
|
string $method, |
96
|
|
|
bool $renew = false, |
97
|
|
|
?string $target = null, |
98
|
|
|
?string $ticket = null, |
99
|
|
|
?string $service = null, |
100
|
|
|
?string $pgtUrl = null, |
101
|
|
|
): XmlResponse { |
102
|
|
|
$forceAuthn = $renew; |
103
|
|
|
$serviceUrl = $service ?? $target ?? null; |
104
|
|
|
|
105
|
|
|
// Check if any of the required query parameters are missing |
106
|
|
|
if ($serviceUrl === null || $ticket === null) { |
107
|
|
|
$messagePostfix = $serviceUrl === null ? 'service' : 'ticket'; |
108
|
|
|
$message = "casserver: Missing service parameter: [{$messagePostfix}]"; |
109
|
|
|
Logger::debug($message); |
110
|
|
|
|
111
|
|
|
return new XmlResponse( |
112
|
|
|
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), |
113
|
|
|
Response::HTTP_BAD_REQUEST, |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
// Get the service ticket |
119
|
|
|
// `getTicket` uses the unserializable method and Objects may throw Throwables in their |
120
|
|
|
// unserialization handlers. |
121
|
|
|
$serviceTicket = $this->ticketStore->getTicket($ticket); |
122
|
|
|
// Delete the ticket |
123
|
|
|
$this->ticketStore->deleteTicket($ticket); |
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
$messagePostfix = ''; |
126
|
|
|
if (!empty($e->getMessage())) { |
127
|
|
|
$messagePostfix = ': ' . var_export($e->getMessage(), true); |
128
|
|
|
} |
129
|
|
|
$message = 'casserver:serviceValidate: internal server error' . $messagePostfix; |
130
|
|
|
Logger::error($message); |
131
|
|
|
|
132
|
|
|
return new XmlResponse( |
133
|
|
|
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), |
134
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$failed = false; |
139
|
|
|
$message = ''; |
140
|
|
|
if (empty($serviceTicket)) { |
141
|
|
|
// No ticket |
142
|
|
|
$message = 'Ticket ' . var_export($ticket, true) . ' not recognized'; |
143
|
|
|
$failed = true; |
144
|
|
|
} elseif ($method === 'serviceValidate' && $this->ticketFactory->isProxyTicket($serviceTicket)) { |
145
|
|
|
$message = 'Ticket ' . var_export($ticket, true) . |
146
|
|
|
' is a proxy ticket. Use proxyValidate instead.'; |
147
|
|
|
$failed = true; |
148
|
|
|
} elseif (!$this->ticketFactory->isServiceTicket($serviceTicket)) { |
149
|
|
|
// This is not a service ticket |
150
|
|
|
$message = 'Ticket ' . var_export($ticket, true) . ' is not a service ticket'; |
151
|
|
|
$failed = true; |
152
|
|
|
} elseif ($this->ticketFactory->isExpired($serviceTicket)) { |
153
|
|
|
// the ticket has expired |
154
|
|
|
$message = 'Ticket ' . var_export($ticket, true) . ' has expired'; |
155
|
|
|
$failed = true; |
156
|
|
|
} elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($serviceUrl)) { |
157
|
|
|
// The service url we passed to the query parameters does not match the one in the ticket. |
158
|
|
|
$message = 'Mismatching service parameters: expected ' . |
159
|
|
|
var_export($serviceTicket['service'], true) . |
160
|
|
|
' but was: ' . var_export($serviceUrl, true); |
161
|
|
|
$failed = true; |
162
|
|
|
} elseif ($forceAuthn && !$serviceTicket['forceAuthn']) { |
163
|
|
|
// If `forceAuthn` is required but not set in the ticket |
164
|
|
|
$message = 'Ticket was issued from single sign on session'; |
165
|
|
|
$failed = true; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($failed) { |
169
|
|
|
$finalMessage = 'casserver:validate: ' . $message; |
170
|
|
|
Logger::error($finalMessage); |
171
|
|
|
|
172
|
|
|
return new XmlResponse( |
173
|
|
|
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), |
174
|
|
|
Response::HTTP_BAD_REQUEST, |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$attributes = $serviceTicket['attributes']; |
179
|
|
|
$this->cas20Protocol->setAttributes($attributes); |
180
|
|
|
|
181
|
|
|
if (isset($pgtUrl)) { |
182
|
|
|
$sessionTicket = $this->ticketStore->getTicket($serviceTicket['sessionId']); |
183
|
|
|
if ( |
184
|
|
|
$sessionTicket !== null |
185
|
|
|
&& $this->ticketFactory->isSessionTicket($sessionTicket) |
186
|
|
|
&& !$this->ticketFactory->isExpired($sessionTicket) |
187
|
|
|
) { |
188
|
|
|
$proxyGrantingTicket = $this->ticketFactory->createProxyGrantingTicket( |
189
|
|
|
[ |
190
|
|
|
'userName' => $serviceTicket['userName'], |
191
|
|
|
'attributes' => $attributes, |
192
|
|
|
'forceAuthn' => false, |
193
|
|
|
'proxies' => array_merge( |
194
|
|
|
[$serviceUrl], |
195
|
|
|
$serviceTicket['proxies'], |
196
|
|
|
), |
197
|
|
|
'sessionId' => $serviceTicket['sessionId'], |
198
|
|
|
], |
199
|
|
|
); |
200
|
|
|
try { |
201
|
|
|
// Here we assume that the fetch will throw on any error. |
202
|
|
|
// The generation of the proxy-granting-ticket or the corresponding proxy granting ticket IOU may |
203
|
|
|
// fail due to the proxy callback url failing to meet the minimum security requirements such as |
204
|
|
|
// failure to establish trust between peers or unresponsiveness of the endpoint, etc. |
205
|
|
|
// In case of failure, no proxy-granting ticket will be issued and the CAS service response |
206
|
|
|
// as described in Section 2.5.2 MUST NOT contain a <proxyGrantingTicket> block. |
207
|
|
|
// At this point, the issuance of a proxy-granting ticket is halted and service ticket |
208
|
|
|
// validation will fail. |
209
|
|
|
$data = $this->httpUtils->fetch( |
210
|
|
|
$pgtUrl . '?pgtIou=' . $proxyGrantingTicket['iou'] . '&pgtId=' . $proxyGrantingTicket['id'], |
211
|
|
|
); |
212
|
|
|
Logger::debug(__METHOD__ . '::data: ' . var_export($data, true)); |
213
|
|
|
$this->cas20Protocol->setProxyGrantingTicketIOU($proxyGrantingTicket['iou']); |
214
|
|
|
$this->ticketStore->addTicket($proxyGrantingTicket); |
215
|
|
|
} catch (\Exception $e) { |
216
|
|
|
return new XmlResponse( |
217
|
|
|
(string)$this->cas20Protocol->getValidateFailureResponse( |
218
|
|
|
C::ERR_INVALID_SERVICE, |
219
|
|
|
'Proxy callback url is failing.', |
220
|
|
|
), |
221
|
|
|
Response::HTTP_BAD_REQUEST, |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return new XmlResponse( |
228
|
|
|
(string)$this->cas20Protocol->getValidateSuccessResponse($serviceTicket['userName']), |
229
|
|
|
Response::HTTP_OK, |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|