Skip to content

Commit 82786db

Browse files
committed
added section 'Protecting ajax calls on static pages'
1 parent 4113b86 commit 82786db

1 file changed

Lines changed: 82 additions & 27 deletions

File tree

README.md

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ the following method is all that is needed to validate that a user has been thro
5757

5858

5959
```php
60-
6160
require_once( __DIR__ .'Models.php');
6261
require_once( __DIR__ .'KnownUser.php');
6362

64-
65-
6663
$configText = file_get_contents('integrationconfig.json');
6764
$customerID = ""; //Your Queue-it customer ID
6865
$secretKey = ""; //Your 72 char secret key as specified in Go Queue-it self-service platform
@@ -71,12 +68,10 @@ $queueittoken = isset( $_GET["queueittoken"] )? $_GET["queueittoken"] :'';
7168

7269
try
7370
{
74-
//Verify if the user has been through the queue
71+
//Verify if the user has been through the queue
7572
$result = QueueIT\KnownUserV3\SDK\KnownUser::validateRequestByIntegrationConfig(getFullRequestUri(),
7673
$queueittoken, $configText, $customerID, $secretKey);
77-
78-
79-
74+
8075
if($result->doRedirect())
8176
{
8277
//Adding no cache headers to prevent browsers to cache requests
@@ -86,21 +81,20 @@ try
8681
//end
8782

8883
//Send the user to the queue - either because hash was missing or because it was invalid
89-
header('Location: '.$result->redirectUrl);
84+
header('Location: '.$result->redirectUrl);
9085
die();
9186
}
9287
if(!empty($queueittoken))
9388
{
94-
//Request can continue - we remove queueittoken from querystring parameter to avoid sharing of user specific token
95-
header('Location: '.str_replace("?queueittoken=".$queueittoken,"", getFullRequestUri()));
96-
die();
89+
//Request can continue - we remove queueittoken from querystring parameter to avoid sharing of user specific token
90+
header('Location: '.str_replace("?queueittoken=".$queueittoken,"", getFullRequestUri()));
91+
die();
9792
}
9893
}
9994
catch(\Exception $e)
10095
{
101-
//log the exception
96+
//log the exception
10297
}
103-
10498
```
10599

106100
Helper method to get the current url (you can have your own).
@@ -126,6 +120,9 @@ So if your webserver is e.g. behind a load balancer that modifies the host name
126120

127121

128122
## Alternative Implementation
123+
124+
### Queue configuration
125+
129126
If your application server (maybe due to security reasons) is not allowed to do external GET requests, then you have three options:
130127

131128
1. Manually download the configuration file from Queue-it Go self-service portal, save it on your application server and load it from local disk
@@ -139,8 +136,6 @@ The following is an example of how to specify the configuration in code:
139136
require_once( __DIR__ .'Models.php');
140137
require_once( __DIR__ .'KnownUser.php');
141138

142-
143-
144139
$customerID = ""; //Your Queue-it customer ID
145140
$secretKey = ""; //Your 72 char secret key as specified in Go Queue-it self-service platform
146141

@@ -157,32 +152,92 @@ $queueittoken = isset( $_GET["queueittoken"] )? $_GET["queueittoken"] :'';
157152

158153
try
159154
{
160-
//Verify if the user has been through the queue
155+
//Verify if the user has been through the queue
161156
$result = QueueIT\KnownUserV3\SDK\KnownUser::resolveRequestByLocalEventConfig(getFullRequestUri(),
162157
$queueittoken, $eventConfig, $customerID, $secretKey);
163-
164158

165159
if($result->doRedirect())
166160
{
167-
//Adding no cache headers to prevent browsers to cache requests
168-
header("Expires:Fri, 01 Jan 1990 00:00:00 GMT");
169-
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
170-
header("Pragma: no-cache");
171-
//end
161+
//Adding no cache headers to prevent browsers to cache requests
162+
header("Expires:Fri, 01 Jan 1990 00:00:00 GMT");
163+
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
164+
header("Pragma: no-cache");
165+
//end
172166
//Send the user to the queue - either because hash was missing or because it was invalid
173-
header('Location: '.$result->redirectUrl);
167+
header('Location: '.$result->redirectUrl);
174168
die();
175169
}
176170
if(!empty($queueittoken))
177171
{
178-
//Request can continue - we remove queueittoken from querystring parameter to avoid sharing of user specific token
179-
header('Location: '.str_replace("?queueittoken=".$queueittoken,"", getFullRequestUri()));
180-
die();
172+
//Request can continue - we remove queueittoken from querystring parameter to avoid sharing of user specific token
173+
header('Location: '.str_replace("?queueittoken=".$queueittoken,"", getFullRequestUri()));
174+
die();
181175
}
182176
}
183177
catch(\Exception $e)
184178
{
185179
//log the exception
186180
}
187-
188181
```
182+
### Protecting ajax calls on static pages
183+
If you have some static html pages (might be behind cache servers) and you have some ajax calls from those pages needed to be protected by KnownUser library you need to follow these steps:
184+
1) You are using v.3.5.1 (or later) of the KnownUser library.
185+
2) Make sure KnownUser code will not run on static pages (by ignoring those URLs in your integration configuration).
186+
3) Protect static pages by including this Javascript code:
187+
```
188+
<script
189+
type="text/javascript"
190+
src="//static.queue-it.net/script/knownuserv3.js">
191+
</script>
192+
```
193+
4) Use the following method to protect all dynamic calls (including dynamic pages and ajax calls).
194+
195+
```php
196+
require_once( __DIR__ .'Models.php');
197+
require_once( __DIR__ .'KnownUser.php');
198+
199+
$configText = file_get_contents('integrationconfig.json');
200+
$customerID = ""; //Your Queue-it customer ID
201+
$secretKey = ""; //Your 72 char secret key as specified in Go Queue-it self-service platform
202+
203+
$queueittoken = isset( $_GET["queueittoken"] )? $_GET["queueittoken"] :'';
204+
205+
try
206+
{
207+
//Verify if the user has been through the queue
208+
$result = QueueIT\KnownUserV3\SDK\KnownUser::validateRequestByIntegrationConfig(getFullRequestUri(),
209+
$queueittoken, $configText, $customerID, $secretKey);
210+
211+
if($result->doRedirect())
212+
{
213+
//Adding no cache headers to prevent browsers to cache requests
214+
header("Expires:Fri, 01 Jan 1990 00:00:00 GMT");
215+
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
216+
header("Pragma: no-cache");
217+
//end
218+
219+
if(!$result->isAjaxResult)
220+
{
221+
//Send the user to the queue - either becuase hash was missing or becuase is was invalid
222+
header('Location: ' . $result->redirectUrl);
223+
}
224+
else
225+
{
226+
header('HTTP/1.0: 200');
227+
header($result->getAjaxQueueRedirectHeaderKey() . ': '. $result->getAjaxRedirectUrl());
228+
}
229+
230+
die();
231+
}
232+
if(!empty($queueittoken))
233+
{
234+
//Request can continue - we remove queueittoken from querystring parameter to avoid sharing of user specific token
235+
header('Location: '.str_replace("?queueittoken=".$queueittoken,"", getFullRequestUri()));
236+
die();
237+
}
238+
}
239+
catch(\Exception $e)
240+
{
241+
//log the exception
242+
}
243+
```

0 commit comments

Comments
 (0)