forked from queueit/KnownUser.V3.PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueITHelpers.php
170 lines (146 loc) · 5.57 KB
/
QueueITHelpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace QueueIT\KnownUserV3\SDK;
class Utils
{
public static function isNullOrEmptyString($value)
{
return (!isset($value) || trim($value) === '');
}
}
class QueueUrlParams
{
const TimeStampKey = "ts";
const ExtendableCookieKey = "ce";
const CookieValidityMinutesKey = "cv";
const HashKey = "h";
const EventIdKey = "e";
const QueueIdKey = "q";
const RedirectTypeKey = "rt";
const KeyValueSeparatorChar = '_';
const KeyValueSeparatorGroupChar = '~';
public $timeStamp = 0;
public $eventId = "";
public $hashCode = "";
public $extendableCookie = false;
public $cookieValidityMinutes = null;
public $queueITToken = "";
public $queueITTokenWithoutHash = "";
public $queueId = "";
public $redirectType = "";
public static function extractQueueParams($queueitToken)
{
if (Utils::isNullOrEmptyString($queueitToken)) {
return null;
}
$result = new QueueUrlParams();
$result->queueITToken = $queueitToken;
$paramsNameValueList = explode(QueueUrlParams::KeyValueSeparatorGroupChar, $result->queueITToken);
foreach ($paramsNameValueList as $pNameValue) {
$paramNameValueArr = explode(QueueUrlParams::KeyValueSeparatorChar, $pNameValue);
if (count($paramNameValueArr) != 2) {
continue;
}
switch ($paramNameValueArr[0]) {
case QueueUrlParams::TimeStampKey: {
if (is_numeric($paramNameValueArr[1])) {
$result->timeStamp = intval($paramNameValueArr[1]);
} else {
$result->timeStamp = 0;
}
break;
}
case QueueUrlParams::CookieValidityMinutesKey: {
if (is_numeric($paramNameValueArr[1])) {
$result->cookieValidityMinutes = intval($paramNameValueArr[1]);
}
break;
}
case QueueUrlParams::EventIdKey: {
$result->eventId = $paramNameValueArr[1];
break;
}
case QueueUrlParams::ExtendableCookieKey: {
$result->extendableCookie = $paramNameValueArr[1] === 'True' || $paramNameValueArr[1] === 'true';
break;
}
case QueueUrlParams::HashKey: {
$result->hashCode = $paramNameValueArr[1];
break;
}
case QueueUrlParams::QueueIdKey: {
$result->queueId = $paramNameValueArr[1];
break;
}
case QueueUrlParams::RedirectTypeKey: {
$result->redirectType = $paramNameValueArr[1];
break;
}
}
}
$result->queueITTokenWithoutHash = str_replace(
QueueUrlParams::KeyValueSeparatorGroupChar
. QueueUrlParams::HashKey
. QueueUrlParams::KeyValueSeparatorChar
. $result->hashCode,
"",
$result->queueITToken
);
return $result;
}
}
class ConnectorDiagnostics
{
public $isEnabled = false;
public $hasError = false;
public $validationResult = null;
private function setStateWithSetupError()
{
$this->hasError = true;
$this->validationResult = new RequestValidationResult(
"ConnectorDiagnosticsRedirect",
null,
null,
"https://api2.queue-it.net/diagnostics/connector/error/?code=setup",
null,
null
);
}
private function setStateWithTokenError($customerId, $errorCode)
{
$this->hasError = true;
$this->validationResult = new RequestValidationResult(
"ConnectorDiagnosticsRedirect",
null,
null,
"https://" . $customerId . ".api2.queue-it.net/" . $customerId . "/diagnostics/connector/error/?code=" . $errorCode,
null,
null
);
}
public static function verify($customerId, $secretKey, $queueitToken)
{
$diagnostics = new ConnectorDiagnostics();
$queueParams = QueueUrlParams::extractQueueParams($queueitToken);
if ($queueParams == null)
return $diagnostics;
if (Utils::isNullOrEmptyString($queueParams->redirectType))
return $diagnostics;
if (strtolower($queueParams->redirectType) != "debug")
return $diagnostics;
if (Utils::isNullOrEmptyString($customerId) || Utils::isNullOrEmptyString($secretKey)) {
$diagnostics->setStateWithSetupError();
return $diagnostics;
}
$calculatedHash = hash_hmac('sha256', $queueParams->queueITTokenWithoutHash, $secretKey);
if (strtoupper($calculatedHash) != strtoupper($queueParams->hashCode)) {
$diagnostics->setStateWithTokenError($customerId, "hash");
return $diagnostics;
}
if ($queueParams->timeStamp < time()) {
$diagnostics->setStateWithTokenError($customerId, "timestamp");
return $diagnostics;
}
$diagnostics->isEnabled = true;
return $diagnostics;
}
}