1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Zefy\SimpleSSO\SSOBroker; |
4
|
|
|
use GuzzleHttp; |
|
|
|
|
5
|
|
|
|
6
|
|
|
class Broker extends SSOBroker |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* SSO servers URL. |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $ssoServerUrl; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Broker name by which it will be identified. |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $brokerName; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Super secret broker's key. |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $brokerSecret; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set base class options (sso server url, broker name and secret, etc). |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
* |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
|
|
protected function setOptions() |
35
|
|
|
{ |
36
|
|
|
$this->ssoServerUrl = null; |
37
|
|
|
$this->brokerName = null; |
38
|
|
|
$this->brokerSecret = null; |
39
|
|
|
|
40
|
|
|
if (!$this->ssoServerUrl || !$this->brokerName || !$this->brokerSecret) { |
41
|
|
|
throw new Exception('Missing configuration values.'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Somehow save random token for client. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
protected function saveToken() |
51
|
|
|
{ |
52
|
|
|
if (isset($this->token) && $this->token) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($this->token = $this->getCookie($this->getCookieName())) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// If cookie token doesn't exist, we need to create it with unique token... |
61
|
|
|
$this->token = base_convert(md5(uniqid(rand(), true)), 16, 36); |
62
|
|
|
setcookie($this->getCookieName(), $this->token, time() + 60 * 60 * 12, '/'); |
63
|
|
|
|
64
|
|
|
// ... and attach it to broker session in SSO server. |
65
|
|
|
$this->attach(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Delete saved token. |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
protected function deleteToken() |
74
|
|
|
{ |
75
|
|
|
$this->token = null; |
76
|
|
|
setcookie($this->getCookieName(), null, -1, '/'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Make request to SSO server. |
81
|
|
|
* |
82
|
|
|
* @param string $method Request method 'post' or 'get'. |
83
|
|
|
* @param string $command Request command name. |
84
|
|
|
* @param array $parameters Parameters for URL query string if GET request and form parameters if it's POST request. |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected function makeRequest(string $method, string $command, array $parameters = []) |
89
|
|
|
{ |
90
|
|
|
$commandUrl = $this->generateCommandUrl($command); |
91
|
|
|
$headers = [ |
92
|
|
|
'Accept' => 'application/json', |
93
|
|
|
'Authorization' => 'Bearer '. $this->getSessionId(), |
94
|
|
|
]; |
95
|
|
|
switch ($method) { |
96
|
|
|
case 'POST': |
97
|
|
|
$body = ['form_params' => $parameters]; |
98
|
|
|
break; |
99
|
|
|
case 'GET': |
100
|
|
|
$body = ['query' => $parameters]; |
101
|
|
|
break; |
102
|
|
|
default: |
103
|
|
|
$body = []; |
104
|
|
|
break; |
105
|
|
|
} |
106
|
|
|
$client = new GuzzleHttp\Client; |
|
|
|
|
107
|
|
|
$response = $client->request($method, $commandUrl, $body + ['headers' => $headers]); |
108
|
|
|
return json_decode($response->getBody(), true); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Redirect client to specified url. |
113
|
|
|
* |
114
|
|
|
* @param string $url URL to be redirected. |
115
|
|
|
* @param array $parameters HTTP query string. |
116
|
|
|
* @param int $httpResponseCode HTTP response code for redirection. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function redirect(string $url, array $parameters = [], int $httpResponseCode = 307) |
121
|
|
|
{ |
122
|
|
|
$query = ''; |
123
|
|
|
// Making URL query string if parameters given. |
124
|
|
|
if (!empty($parameters)) { |
125
|
|
|
$query = '?'; |
126
|
|
|
if (parse_url($url, PHP_URL_QUERY)) { |
127
|
|
|
$query = '&'; |
128
|
|
|
} |
129
|
|
|
$query .= http_build_query($parameters); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
header('Location: ' . $url . $query, true, $httpResponseCode); |
133
|
|
|
exit; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Getting current url which can be used as return to url. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function getCurrentUrl() |
142
|
|
|
{ |
143
|
|
|
$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
144
|
|
|
|
145
|
|
|
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Cookie name in which we save unique client token. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function getCookieName() |
154
|
|
|
{ |
155
|
|
|
// Cookie name based on broker's name because there can be some brokers on same domain |
156
|
|
|
// and we need to prevent duplications. |
157
|
|
|
return 'sso_token_' . preg_replace('/[_\W]+/', '_', strtolower($this->brokerName)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get COOKIE value by it's name. |
162
|
|
|
* |
163
|
|
|
* @param string $cookieName |
164
|
|
|
* |
165
|
|
|
* @return string|null |
166
|
|
|
*/ |
167
|
|
|
protected function getCookie(string $cookieName) |
168
|
|
|
{ |
169
|
|
|
if (isset($_COOKIE[$cookieName])) { |
170
|
|
|
return $_COOKIE[$cookieName]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return null; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths