Skip to content

Commit 5f80cf6

Browse files
Moved setCookie into the GateKeeper class and redirect to clean url if token is in the url
1 parent 543fd8d commit 5f80cf6

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

src/GateKeeper.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,21 @@ public function __construct(Client $client, \Psr\Http\Message\ServerRequestInter
4949
$server = $_SERVER;
5050
$cookies = $_COOKIE;
5151
}
52+
53+
54+
// Token in URL
5255
if (isset($get[self::TOKEN_URL])) {
53-
$this->token = $get[self::TOKEN_URL];
56+
$this->setCookie($get[self::TOKEN_URL]);
57+
// clean url and redirect
58+
$this->sanitizeURL($this->url, $get);
59+
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
60+
header('location: '.$this->url, true, self::HTTP_REDIRECT_CODE);
61+
exit;
62+
5463
} elseif (isset($cookies[self::TOKEN_COOKIE])) {
5564
$this->token = $cookies[self::TOKEN_COOKIE];
5665
}
5766

58-
// now we've extracted the token we sanitize the url
59-
$this->url = $this->sanitizeURL($this->url, $get);
60-
6167
$this->detectClientIp($server);
6268
if (isset($server['HTTP_USER_AGENT'])) $this->agent = $server['HTTP_USER_AGENT'];
6369
if (isset($server['HTTP_ACCEPT_LANGUAGE'])) $this->lang = $server['HTTP_ACCEPT_LANGUAGE'];
@@ -70,8 +76,6 @@ public function __construct(Client $client, \Psr\Http\Message\ServerRequestInter
7076
*/
7177
private function sanitizeURL ($url, $get)
7278
{
73-
74-
$isPromoted = false;
7579

7680
$parsed_url = parse_url($url);
7781
$this->url = 'https://' . $parsed_url['host'] . $parsed_url['path'];
@@ -80,7 +84,6 @@ private function sanitizeURL ($url, $get)
8084
for ($i=0; $i < Count(self::CROWDHANDLER_PARAMS); $i++) {
8185
if (isset($get[self::CROWDHANDLER_PARAMS[$i]]))
8286
{
83-
$isPromoted = true;
8487
array_push($ch_params_to_remove, $get[self::CROWDHANDLER_PARAMS[$i]]);
8588
}
8689
}
@@ -90,16 +93,6 @@ private function sanitizeURL ($url, $get)
9093
if (Count($remaining_query_parameters) > 0) {
9194
$this->url = $this->url .= '?' . http_build_query($remaining_query_parameters);
9295
}
93-
94-
if($isPromoted) {
95-
// and redirect
96-
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
97-
header('location: '.$this->url, true, self::HTTP_REDIRECT_CODE);
98-
exit;
99-
} else {
100-
return $this->url;
101-
}
102-
10396

10497
}
10598

@@ -204,7 +197,10 @@ public function checkRequest()
204197
$this->result = $this->client->requests->get($this->token, $params);
205198
} else {
206199
$this->result = $this->client->requests->post($params);
207-
}
200+
}
201+
if(isset($this->result->token)) {
202+
$this->setCookie($this->result->token);
203+
}
208204
}
209205
catch (\Exception $e) {
210206
$mock = new ApiObject;
@@ -250,11 +246,11 @@ public function getRedirectUrl()
250246
/**
251247
* Set CrowdHandler session cookie
252248
*/
253-
public function setCookie()
249+
private function setCookie($cookie)
254250
{
255-
if (!is_null($this->result->token)) {
256-
setcookie(self::TOKEN_COOKIE, $this->result->token, 0, '/', '', $this->debug ? false: true);
257-
$this->debug('Setting cookie '.$this->result->token);
251+
if (!is_null($cookie)) {
252+
setcookie(self::TOKEN_COOKIE, $cookie, 0, '/', '', $this->debug ? false: true);
253+
$this->debug('Setting cookie '.$cookie);
258254
}
259255
}
260256

0 commit comments

Comments
 (0)