1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Openidconnect\Provider; |
4
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Openidconnect\Component\Uriable; |
6
|
|
|
use Sludio\HelperBundle\Script\Security\Exception\ErrorException; |
7
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
8
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
9
|
|
|
|
10
|
|
|
class Uri implements Uriable |
11
|
|
|
{ |
12
|
|
|
protected $params = []; |
13
|
|
|
protected $urlParams = []; |
14
|
|
|
protected $url; |
15
|
|
|
protected $base; |
16
|
|
|
protected $session; |
17
|
|
|
protected $useSession; |
18
|
|
|
protected $method; |
19
|
|
|
|
20
|
|
|
public function __construct(array $options, array $additional = [], $useSession = false, $method = OpenIDConnectProvider::METHOD_POST, Session $session = null) |
21
|
|
|
{ |
22
|
|
|
$this->base = rtrim($additional['base_uri'], '/').'/'; |
23
|
|
|
unset($additional['base_uri']); |
24
|
|
|
$this->session = $session; |
25
|
|
|
$this->useSession = $useSession; |
26
|
|
|
$this->method = $method; |
27
|
|
|
|
28
|
|
|
$this->params = !empty($options['params']) ? $options['params'] : []; |
29
|
|
|
|
30
|
|
|
$this->setGetParams($options, $additional); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function setGetParams($options, $additional) |
34
|
|
|
{ |
35
|
|
|
if ($this->method === OpenIDConnectProvider::METHOD_GET) { |
36
|
|
|
if (isset($options['url_params']['post_logout_redirect_uri'])) { |
37
|
|
|
$options['url_params']['post_logout_redirect_uri'] = $additional['redirect_uri']; |
38
|
|
|
unset($additional['redirect_uri']); |
39
|
|
|
} |
40
|
|
|
$this->urlParams = !empty($options['url_params']) ? array_merge($options['url_params'], $additional) : $additional; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return RedirectResponse |
46
|
|
|
* |
47
|
|
|
* @throws ErrorException |
48
|
|
|
*/ |
49
|
|
|
public function redirect() |
50
|
|
|
{ |
51
|
|
|
return new RedirectResponse($this->getUrl()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the value of Url |
56
|
|
|
* |
57
|
|
|
* @param null|string $language |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
* @throws ErrorException |
61
|
|
|
*/ |
62
|
|
|
public function getUrl($language = null) |
63
|
|
|
{ |
64
|
|
|
$this->buildUrl($language); |
65
|
|
|
|
66
|
|
|
return $this->url; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the value of Url |
71
|
|
|
* |
72
|
|
|
* @param mixed $url |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setUrl($url) |
77
|
|
|
{ |
78
|
|
|
$this->url = $url; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param null|string $language |
85
|
|
|
* |
86
|
|
|
* @throws ErrorException |
87
|
|
|
*/ |
88
|
|
|
private function buildUrl($language = null) |
89
|
|
|
{ |
90
|
|
|
$this->setIdToken(); |
91
|
|
|
|
92
|
|
|
if ($language !== null) { |
93
|
|
|
$this->urlParams['lang'] = (string)$language; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$url = $this->base; |
97
|
|
|
if (!empty($this->params)) { |
98
|
|
|
$url .= implode('/', $this->params); |
99
|
|
|
} |
100
|
|
|
if (!empty($this->urlParams)) { |
101
|
|
|
$params = http_build_query($this->urlParams); |
102
|
|
|
$url .= '?'.$params; |
103
|
|
|
} |
104
|
|
|
$url = urldecode($url); |
105
|
|
|
$this->setUrl($url); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @throws ErrorException |
110
|
|
|
*/ |
111
|
|
|
private function setIdToken() |
112
|
|
|
{ |
113
|
|
|
if ($this->method === OpenIDConnectProvider::METHOD_GET && isset($this->urlParams['id_token_hint']) && $this->session !== null && $this->session->has('id_token')) { |
114
|
|
|
if ($this->useSession === false) { |
115
|
|
|
throw new ErrorException(sprintf('"%s" parameter must be set to "true" in order to use id_token_hint', 'use_session')); |
116
|
|
|
} |
117
|
|
|
$this->urlParams['id_token_hint'] = $this->session->get('id_token'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function addParam($value) |
122
|
|
|
{ |
123
|
|
|
$this->params[] = $value; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function addUrlParam($name, $value) |
127
|
|
|
{ |
128
|
|
|
$this->urlParams[$name] = $value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getBase() |
135
|
|
|
{ |
136
|
|
|
return $this->base; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|