-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQueueITHelpers.lua
More file actions
162 lines (142 loc) · 4.34 KB
/
QueueITHelpers.lua
File metadata and controls
162 lines (142 loc) · 4.34 KB
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
local utils = require("Utils")
local models = require("Models")
local iHelpers = require("KnownUserImplementationHelpers")
local queueUrlParams = {
extractQueueParams = function(queueitToken)
if (utils.toString(queueitToken) == "") then
return nil
end
local TimeStampKey = "ts"
local ExtendableCookieKey = "ce"
local CookieValidityMinutesKey = "cv"
local HashCodeKey = "h"
local EventIdKey = "e"
local QueueIdKey = "q"
local RedirectTypeKey = "rt"
local KeyValueSeparatorChar = "_"
local KeyValueSeparatorGroupChar = "~"
-- Private functions
local function updateResult(paramNameValueArr, result)
if(paramNameValueArr[1] == TimeStampKey) then
local tsn = tonumber(paramNameValueArr[2])
if(tsn ~= nil) then
result.timeStamp = tsn
end
return
end
if(paramNameValueArr[1] == CookieValidityMinutesKey) then
local cvn = tonumber(paramNameValueArr[2])
if(cvn ~= nil) then
result.cookieValidityMinutes = cvn
end
return
end
if(paramNameValueArr[1] == EventIdKey) then
result.eventId = paramNameValueArr[2]
return
end
if(paramNameValueArr[1] == ExtendableCookieKey) then
if(paramNameValueArr[2] ~= nil) then
result.extendableCookie = string.lower(paramNameValueArr[2]) == "true"
else
result.extendableCookie = false
end
return
end
if(paramNameValueArr[1] == HashCodeKey) then
result.hashCode = paramNameValueArr[2]
return
end
if(paramNameValueArr[1] == QueueIdKey) then
result.queueId = paramNameValueArr[2]
return
end
if(paramNameValueArr[1] == RedirectTypeKey) then
result.redirectType = paramNameValueArr[2]
return
end
end
-- END Private functions
local result = {
timeStamp = 0,
eventId = "",
hashCode = "",
extendableCookie = false,
cookieValidityMinutes = nil,
queueITToken = queueitToken,
queueITTokenWithoutHash = "",
queueId = "",
redirectType = ""
}
local paramsNameValueList = utils.explode(KeyValueSeparatorGroupChar, result.queueITToken)
for _,pNameValue in pairs(paramsNameValueList) do
local paramNameValueArr = utils.explode(KeyValueSeparatorChar, pNameValue)
local c = utils.tableLength(paramNameValueArr)
if (c == 2) then
updateResult(paramNameValueArr, result)
end
end
local replacingHash =
KeyValueSeparatorGroupChar .. HashCodeKey ..
KeyValueSeparatorChar .. utils.escapeMagicChars(result.hashCode)
result.queueITTokenWithoutHash = result.queueITToken:gsub(replacingHash, "")
return result
end
}
local connectorDiagnostics = {
verify = function(customerId, secretKey, queueitToken)
local function setStateWithTokenError(_diagnostics, _customerId, _errorCode)
_diagnostics.hasError = true
_diagnostics.validationResult = models.RequestValidationResult.create(
"ConnectorDiagnosticsRedirect",
nil, nil,
"https://" .. _customerId ..
".api2.queue-it.net/" .. _customerId ..
"/diagnostics/connector/error/?code=" .. _errorCode,
nil, nil)
end
local function setStateWithSetupError(_diagnostics)
_diagnostics.hasError = true
_diagnostics.validationResult = models.RequestValidationResult.create(
"ConnectorDiagnosticsRedirect",
nil, nil,
"https://api2.queue-it.net/diagnostics/connector/error/?code=setup",
nil, nil)
end
local diagnostics = {
isEnabled = false,
hasError = false,
validationResult = nil
}
local qParams = queueUrlParams.extractQueueParams(queueitToken)
if (qParams == nil) then
return diagnostics
end
if (qParams.redirectType == nil) then
return diagnostics
end
if (string.lower(qParams.redirectType) ~= "debug") then
return diagnostics
end
if (utils.toString(customerId) == "" or utils.toString(secretKey) == "") then
setStateWithSetupError(diagnostics)
return diagnostics
end
local calculatedHash = iHelpers.hash.hmac_sha256_encode(qParams.queueITTokenWithoutHash, secretKey)
if (string.upper(calculatedHash) ~= string.upper(qParams.hashCode)) then
setStateWithTokenError(diagnostics, customerId, "hash")
return diagnostics
end
if (qParams.timeStamp < os.time()) then
setStateWithTokenError(diagnostics, customerId, "timestamp")
return diagnostics
end
diagnostics.isEnabled = true
return diagnostics
end
}
local qitHelpers = {
QueueUrlParams = queueUrlParams,
ConnectorDiagnostics = connectorDiagnostics
}
return qitHelpers