-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUserInQueueStateCookieRepository.lua
More file actions
190 lines (159 loc) · 5.89 KB
/
UserInQueueStateCookieRepository.lua
File metadata and controls
190 lines (159 loc) · 5.89 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
local utils = require("Utils")
local iHelpers = require("KnownUserImplementationHelpers")
-- Private functions
local function generateHash(eventId, queueId, fixedCookieValidityMinutes, redirectType, issueTime, secretKey)
local message = eventId .. queueId .. fixedCookieValidityMinutes .. redirectType .. issueTime
return iHelpers.hash.hmac_sha256_encode(message, secretKey)
end
local function createCookieValue(eventId, queueId, fixedCookieValidityMinutes, redirectType, secretKey)
local issueTime = os.time()
local hashValue = generateHash(eventId, queueId, fixedCookieValidityMinutes, redirectType, issueTime, secretKey)
local fixedCookieValidityMinutesPart = ""
if (fixedCookieValidityMinutes ~= "") then
fixedCookieValidityMinutesPart = "&FixedValidityMins=" .. fixedCookieValidityMinutes
end
local cookieValue = "EventId=" .. eventId ..
"&QueueId=" .. queueId .. fixedCookieValidityMinutesPart ..
"&RedirectType=" .. redirectType .. "&IssueTime=" .. issueTime .. "&Hash=" .. hashValue
return cookieValue
end
local function getCookieNameValueMap(cookieValue)
local result = { }
local cookieNameValues = utils.explode("&", cookieValue)
for _, cookieNameValue in pairs(cookieNameValues) do
local arr = utils.explode("=", cookieNameValue)
if(arr[1] ~= nil and arr[2] ~= nil) then
result[arr[1]] = arr[2]
end
end
return result
end
local function isCookieValid(secretKey, cookieNameValueMap, eventId, cookieValidityMinutes, validateTime)
if (cookieNameValueMap["EventId"] == nil) then
return false
end
if (cookieNameValueMap["QueueId"] == nil) then
return false
end
if (cookieNameValueMap["RedirectType"] == nil) then
return false
end
if (cookieNameValueMap["IssueTime"] == nil) then
return false
end
if (cookieNameValueMap["Hash"] == nil) then
return false
end
local fixedCookieValidityMinutes = ""
if (cookieNameValueMap["FixedValidityMins"] ~= nil) then
fixedCookieValidityMinutes = cookieNameValueMap["FixedValidityMins"]
end
local hashValue = generateHash(
cookieNameValueMap["EventId"],
cookieNameValueMap["QueueId"],
fixedCookieValidityMinutes,
cookieNameValueMap["RedirectType"],
cookieNameValueMap["IssueTime"],
secretKey)
if (hashValue ~= cookieNameValueMap["Hash"]) then
return false
end
if (string.lower(eventId) ~= string.lower(cookieNameValueMap["EventId"])) then
return false
end
if (validateTime) then
local validity = cookieValidityMinutes
if (utils.toString(fixedCookieValidityMinutes) ~= "") then
validity = tonumber(fixedCookieValidityMinutes)
end
local expirationTime = cookieNameValueMap["IssueTime"] + (validity*60)
if (expirationTime < os.time()) then
return false
end
end
return true
end
-- END Private functions
local repo = {
StateInfo = {
create = function(isFound, isValid, queueId, fixedCookieValidityMinutes, redirectType)
local model = {
isFound = isFound,
isValid = isValid,
queueId = queueId,
fixedCookieValidityMinutes = fixedCookieValidityMinutes,
redirectType = redirectType,
isStateExtendable = function(self)
return self.isValid and self.fixedCookieValidityMinutes == nil
end
}
return model
end
}
}
repo.getCookieKey = function(eventId)
return "QueueITAccepted-SDFrts345E-V3_" .. eventId
end
repo.cancelQueueCookie = function(eventId, cookieDomain, isCookieHttpOnly, isCookieSecure)
local cookieKey = repo.getCookieKey(eventId)
iHelpers.response.setCookie(cookieKey, "deleted", 1, cookieDomain, isCookieHttpOnly, isCookieSecure)
end
repo.getState = function(eventId, cookieValidityMinutes, secretKey, validateTime)
local pcall_status, pcall_result = pcall(function()
local cookieKey = repo.getCookieKey(eventId)
if (iHelpers.request.getUnescapedCookieValue(cookieKey) == nil) then
return repo.StateInfo.create(false, false, nil, nil, nil)
end
local cookieNameValueMap = getCookieNameValueMap(iHelpers.request.getUnescapedCookieValue(cookieKey))
if (isCookieValid(secretKey, cookieNameValueMap, eventId, cookieValidityMinutes, validateTime) == false) then
return repo.StateInfo.create(true, false, nil, nil, nil)
end
local fixedCookieValidityMinutes = nil
if (cookieNameValueMap["FixedValidityMins"] ~= nil) then
fixedCookieValidityMinutes = tonumber(cookieNameValueMap["FixedValidityMins"])
end
return repo.StateInfo.create(
true,
true,
cookieNameValueMap["QueueId"],
fixedCookieValidityMinutes,
cookieNameValueMap["RedirectType"]
)
end)
if (pcall_status) then
return pcall_result
end
return repo.StateInfo.create(true, false, nil, nil, nil)
end
repo.reissueQueueCookie = function(
eventId, cookieValidityMinutes, cookieDomain, isCookieHttpOnly, isCookieSecure, secretKey)
local cookieKey = repo.getCookieKey(eventId)
if (iHelpers.request.getUnescapedCookieValue(cookieKey) == nil) then
return
end
local cookieNameValueMap = getCookieNameValueMap(iHelpers.request.getUnescapedCookieValue(cookieKey))
if (isCookieValid(secretKey, cookieNameValueMap, eventId, cookieValidityMinutes, true) == false) then
return
end
local fixedCookieValidityMinutes = ""
if (cookieNameValueMap["FixedValidityMins"] ~= nil) then
fixedCookieValidityMinutes = cookieNameValueMap["FixedValidityMins"]
end
local cookieValue = createCookieValue(
eventId,
cookieNameValueMap["QueueId"],
fixedCookieValidityMinutes,
cookieNameValueMap["RedirectType"],
secretKey)
iHelpers.response.setCookie(
cookieKey, cookieValue, os.time() + (24 * 60 * 60), cookieDomain, isCookieHttpOnly, isCookieSecure)
end
repo.store = function(
eventId, queueId, fixedCookieValidityMinutes, cookieDomain, isCookieHttpOnly, isCookieSecure, redirectType, secretKey)
local cookieKey = repo.getCookieKey(eventId)
local cookieValue = createCookieValue(
eventId, queueId, utils.toString(fixedCookieValidityMinutes), redirectType, secretKey)
iHelpers.response.setCookie(
cookieKey, cookieValue, os.time() + (24 * 60 * 60), cookieDomain, isCookieHttpOnly, isCookieSecure)
end
return repo