1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\OAuth; |
4
|
|
|
|
5
|
|
|
use HWI\Bundle\OAuthBundle\Controller\ConnectController as HWIConnectController; |
6
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ConnectController |
12
|
|
|
*/ |
13
|
|
|
class ConnectController extends HWIConnectController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public function redirectToServiceAction(Request $request, $service) |
19
|
|
|
{ |
20
|
|
|
try { |
21
|
|
|
$authorizationUrl = $this->container->get('hwi_oauth.security.oauth_utils')->getAuthorizationUrl($request, $service); |
22
|
|
|
} catch (\RuntimeException $e) { |
23
|
|
|
throw new NotFoundHttpException($e->getMessage(), $e); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$url = parse_url($authorizationUrl); |
27
|
|
|
parse_str($url['query'], $url['query']); |
28
|
|
|
$success = $this->get('ds_config.service.config')->get('app.spa.portal.oauth.success'); |
29
|
|
|
$tenant = $this->get('ds_tenant.service.tenant')->getContext(); |
30
|
|
|
$url['query']['redirect_uri'] = $success.'?tenant='.urlencode($tenant).'&service='.urlencode($service); |
31
|
|
|
$url['query'] = http_build_query($url['query']); |
32
|
|
|
$authorizationUrl = (function(array $url) { |
33
|
|
|
$scheme = isset($url['scheme']) ? $url['scheme'] . '://' : ''; |
34
|
|
|
$host = isset($url['host']) ? $url['host'] : ''; |
35
|
|
|
$port = isset($url['port']) ? ':' . $url['port'] : ''; |
36
|
|
|
$user = isset($url['user']) ? $url['user'] : ''; |
37
|
|
|
$pass = isset($url['pass']) ? ':' . $url['pass'] : ''; |
38
|
|
|
$pass = ($user || $pass) ? "$pass@" : ''; |
39
|
|
|
$path = isset($url['path']) ? $url['path'] : ''; |
40
|
|
|
$query = isset($url['query']) ? '?' . $url['query'] : ''; |
41
|
|
|
$fragment = isset($url['fragment']) ? '#' . $url['fragment'] : ''; |
42
|
|
|
|
43
|
|
|
return "$scheme$user$pass$host$port$path$query$fragment"; |
44
|
|
|
})($url); |
45
|
|
|
|
46
|
|
|
// Check for a return path and store it before redirect |
47
|
|
|
if ($request->hasSession()) { |
48
|
|
|
// initialize the session for preventing SessionUnavailableException |
49
|
|
|
$session = $request->getSession(); |
50
|
|
|
$session->start(); |
51
|
|
|
|
52
|
|
|
foreach ($this->container->getParameter('hwi_oauth.firewall_names') as $providerKey) { |
53
|
|
|
$sessionKey = '_security.'.$providerKey.'.target_path'; |
54
|
|
|
$sessionKeyFailure = '_security.'.$providerKey.'.failed_target_path'; |
55
|
|
|
|
56
|
|
|
$param = $this->container->getParameter('hwi_oauth.target_path_parameter'); |
57
|
|
|
if (!empty($param) && $targetUrl = $request->get($param)) { |
58
|
|
|
$session->set($sessionKey, $targetUrl); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($this->container->getParameter('hwi_oauth.failed_use_referer') && !$session->has($sessionKeyFailure) && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $authorizationUrl) { |
62
|
|
|
$session->set($sessionKeyFailure, $targetUrl); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->container->getParameter('hwi_oauth.use_referer') && !$session->has($sessionKey) && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $authorizationUrl) { |
66
|
|
|
$session->set($sessionKey, $targetUrl); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->redirect($authorizationUrl); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|