forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamlRequest.php
More file actions
51 lines (42 loc) · 1.73 KB
/
SamlRequest.php
File metadata and controls
51 lines (42 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace ProcessMaker\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Symfony\Component\HttpFoundation\Response;
class SamlRequest
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if ($request->hasCookie('saml_request') && !$request->filled(['SAMLRequest', 'RelayState'])) {
// Get the SAMLRequest and RelayState from the cookie
$samlRequestCookie = json_decode($request->cookie('saml_request'), true);
$samlRequest = $samlRequestCookie['SAMLRequest'];
$relayState = $samlRequestCookie['RelayState'];
// Add SAMLRequest and RelayState to the query string
$request->query->add([
'SAMLRequest' => $samlRequest,
'RelayState' => $relayState,
]);
// Check if the current URL is allowed
$appUrl = config('app.url');
$allowedRedirectUrls = [$appUrl . '/login'];
$url = $request->url();
if (in_array($url, $allowedRedirectUrls)) {
// Remove saml_request cookie
Cookie::queue(Cookie::forget('saml_request'));
// Build the query string and set it
$queryString = http_build_query($request->query());
$request->server->set('QUERY_STRING', $queryString);
// To redirect with the new query string, you can use the following:
return redirect($url . '?' . $queryString);
}
}
return $next($request);
}
}