queueit_knownuserv3 3.5.1 → 3.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,371 +1,410 @@
1
- require 'cgi'
2
- require 'json'
3
-
4
- module QueueIt
5
- class KnownUser
6
- QUEUEIT_TOKEN_KEY = "queueittoken"
7
- QUEUEIT_DEBUG_KEY = "queueitdebug"
8
- QUEUEIT_AJAX_HEADER_KEY = "x-queueit-ajaxpageurl"
9
-
10
- @@userInQueueService = nil
11
- def self.getUserInQueueService(cookieJar)
12
- if (@@userInQueueService == nil)
13
- return UserInQueueService.new(UserInQueueStateCookieRepository.new(CookieManager.new(cookieJar)))
14
- end
15
-
16
- return @@userInQueueService
17
- end
18
- private_class_method :getUserInQueueService
19
-
20
- def self.isQueueAjaxCall(request)
21
- return request.headers[QUEUEIT_AJAX_HEADER_KEY] != nil
22
- end
23
- private_class_method :isQueueAjaxCall
24
-
25
- def self.generateTargetUrl(originalTargetUrl, request)
26
- unless isQueueAjaxCall(request)
27
- return originalTargetUrl
28
- end
29
- return CGI::unescape(request.headers[QUEUEIT_AJAX_HEADER_KEY])
30
- end
31
- private_class_method :generateTargetUrl
32
-
33
- def self.convertToInt(value)
34
- begin
35
- converted = Integer(value)
36
- rescue
37
- converted = 0
38
- end
39
- return converted
40
- end
41
- private_class_method :convertToInt
42
-
43
- def self.logMoreRequestDetails(debugEntries, request)
44
- debugEntries["ServerUtcTime"] = Time.now.utc.iso8601
45
- debugEntries["RequestIP"] = request.remote_ip
46
- debugEntries["RequestHttpHeader_Via"] = request.headers["via"]
47
- debugEntries["RequestHttpHeader_Forwarded"] = request.headers["forwarded"]
48
- debugEntries["RequestHttpHeader_XForwardedFor"] = request.headers["x-forwarded-for"]
49
- debugEntries["RequestHttpHeader_XForwardedHost"] = request.headers["x-forwarded-host"]
50
- debugEntries["RequestHttpHeader_XForwardedProto"] = request.headers["x-forwarded-proto"]
51
- end
52
- private_class_method :logMoreRequestDetails
53
-
54
- def self.getIsDebug(queueitToken, secretKey)
55
- qParams = QueueUrlParams.extractQueueParams(queueitToken)
56
- if(qParams == nil)
57
- return false
58
- end
59
-
60
- redirectType = qParams.redirectType
61
- if(redirectType == nil)
62
- return false
63
- end
64
-
65
- if (redirectType.upcase.eql?("DEBUG"))
66
- calculatedHash = OpenSSL::HMAC.hexdigest('sha256', secretKey, qParams.queueITTokenWithoutHash)
67
- valid = qParams.hashCode.eql?(calculatedHash)
68
- return valid
69
- end
70
- return false
71
- end
72
- private_class_method :getIsDebug
73
-
74
- def self.setDebugCookie(debugEntries, cookieJar)
75
- if(debugEntries == nil || debugEntries.length == 0)
76
- return
77
- end
78
-
79
- cookieManager = CookieManager.new(cookieJar)
80
- cookieValue = ''
81
- debugEntries.each do |entry|
82
- cookieValue << (entry[0].to_s + '=' + entry[1].to_s + '|')
83
- end
84
- cookieValue = cookieValue.chop # remove trailing char
85
- cookieManager.setCookie(QUEUEIT_DEBUG_KEY, cookieValue, nil, nil)
86
- end
87
- private_class_method :setDebugCookie
88
-
89
- def self._resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, secretKey, request, debugEntries)
90
- isDebug = getIsDebug(queueitToken, secretKey)
91
- if(isDebug)
92
- debugEntries["TargetUrl"] = targetUrl
93
- debugEntries["QueueitToken"] = queueitToken
94
- debugEntries["OriginalUrl"] = getRealOriginalUrl(request)
95
- if(queueConfig == nil)
96
- debugEntries["QueueConfig"] = "NULL"
97
- else
98
- debugEntries["QueueConfig"] = queueConfig.toString()
99
- end
100
- logMoreRequestDetails(debugEntries, request)
101
- end
102
-
103
- if(Utils.isNilOrEmpty(customerId))
104
- raise KnownUserError, "customerId can not be nil or empty."
105
- end
106
-
107
- if(Utils.isNilOrEmpty(secretKey))
108
- raise KnownUserError, "secretKey can not be nil or empty."
109
- end
110
-
111
- if(queueConfig == nil)
112
- raise KnownUserError, "queueConfig can not be nil."
113
- end
114
-
115
- if(Utils.isNilOrEmpty(queueConfig.eventId))
116
- raise KnownUserError, "queueConfig.eventId can not be nil or empty."
117
- end
118
-
119
- if(Utils.isNilOrEmpty(queueConfig.queueDomain))
120
- raise KnownUserError, "queueConfig.queueDomain can not be nil or empty."
121
- end
122
-
123
- minutes = convertToInt(queueConfig.cookieValidityMinute)
124
- if(minutes <= 0)
125
- raise KnownUserError, "queueConfig.cookieValidityMinute should be integer greater than 0."
126
- end
127
-
128
- if(![true, false].include? queueConfig.extendCookieValidity)
129
- raise KnownUserError, "queueConfig.extendCookieValidity should be valid boolean."
130
- end
131
-
132
- userInQueueService = getUserInQueueService(request.cookie_jar)
133
- result = userInQueueService.validateQueueRequest(targetUrl, queueitToken, queueConfig, customerId, secretKey)
134
- result.isAjaxResult = isQueueAjaxCall(request)
135
-
136
- return result
137
- end
138
- private_class_method :_resolveQueueRequestByLocalConfig
139
-
140
- def self._cancelRequestByLocalConfig(targetUrl, queueitToken, cancelConfig, customerId, secretKey, request, debugEntries)
141
- targetUrl = generateTargetUrl(targetUrl, request)
142
- isDebug = getIsDebug(queueitToken, secretKey)
143
- if(isDebug)
144
- debugEntries["TargetUrl"] = targetUrl
145
- debugEntries["QueueitToken"] = queueitToken
146
- debugEntries["OriginalUrl"] = getRealOriginalUrl(request)
147
- if(cancelConfig == nil)
148
- debugEntries["CancelConfig"] = "NULL"
149
- else
150
- debugEntries["CancelConfig"] = cancelConfig.toString()
151
- end
152
- logMoreRequestDetails(debugEntries, request)
153
- end
154
-
155
- if(Utils.isNilOrEmpty(targetUrl))
156
- raise KnownUserError, "targetUrl can not be nil or empty."
157
- end
158
-
159
- if(Utils.isNilOrEmpty(customerId))
160
- raise KnownUserError, "customerId can not be nil or empty."
161
- end
162
-
163
- if(Utils.isNilOrEmpty(secretKey))
164
- raise KnownUserError, "secretKey can not be nil or empty."
165
- end
166
-
167
- if(cancelConfig == nil)
168
- raise KnownUserError, "cancelConfig can not be nil."
169
- end
170
-
171
- if(Utils.isNilOrEmpty(cancelConfig.eventId))
172
- raise KnownUserError, "cancelConfig.eventId can not be nil or empty."
173
- end
174
-
175
- if(Utils.isNilOrEmpty(cancelConfig.queueDomain))
176
- raise KnownUserError, "cancelConfig.queueDomain can not be nil or empty."
177
- end
178
-
179
- userInQueueService = getUserInQueueService(request.cookie_jar)
180
- result = userInQueueService.validateCancelRequest(targetUrl, cancelConfig, customerId, secretKey)
181
- result.isAjaxResult = isQueueAjaxCall(request)
182
-
183
- return result
184
- end
185
- private_class_method :_cancelRequestByLocalConfig
186
-
187
- def self.extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, secretKey, request)
188
- if(Utils.isNilOrEmpty(eventId))
189
- raise KnownUserError, "eventId can not be nil or empty."
190
- end
191
-
192
- if(Utils.isNilOrEmpty(secretKey))
193
- raise KnownUserError, "secretKey can not be nil or empty."
194
- end
195
-
196
- minutes = convertToInt(cookieValidityMinute)
197
- if(minutes <= 0)
198
- raise KnownUserError, "cookieValidityMinute should be integer greater than 0."
199
- end
200
-
201
- userInQueueService = getUserInQueueService(request.cookie_jar)
202
- userInQueueService.extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, secretKey)
203
- end
204
-
205
- def self.resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, secretKey, request)
206
- debugEntries = Hash.new
207
- begin
208
- targetUrl = generateTargetUrl(targetUrl, request)
209
- return _resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, secretKey, request, debugEntries)
210
- ensure
211
- setDebugCookie(debugEntries, request.cookie_jar)
212
- end
213
- end
214
-
215
- def self.validateRequestByIntegrationConfig(currentUrlWithoutQueueITToken, queueitToken, integrationsConfigString, customerId, secretKey, request)
216
- if(Utils.isNilOrEmpty(currentUrlWithoutQueueITToken))
217
- raise KnownUserError, "currentUrlWithoutQueueITToken can not be nil or empty."
218
- end
219
-
220
- if(Utils.isNilOrEmpty(integrationsConfigString))
221
- raise KnownUserError, "integrationsConfigString can not be nil or empty."
222
- end
223
-
224
- begin
225
- customerIntegration = JSON.parse(integrationsConfigString)
226
-
227
- debugEntries = Hash.new
228
- isDebug = getIsDebug(queueitToken, secretKey)
229
- if(isDebug)
230
- debugEntries["ConfigVersion"] = customerIntegration["Version"]
231
- debugEntries["PureUrl"] = currentUrlWithoutQueueITToken
232
- debugEntries["QueueitToken"] = queueitToken
233
- debugEntries["OriginalUrl"] = getRealOriginalUrl(request)
234
- logMoreRequestDetails(debugEntries, request)
235
- end
236
-
237
- integrationEvaluator = IntegrationEvaluator.new
238
- matchedConfig = integrationEvaluator.getMatchedIntegrationConfig(customerIntegration, currentUrlWithoutQueueITToken, request)
239
-
240
- if(isDebug)
241
- if(matchedConfig == nil)
242
- debugEntries["MatchedConfig"] = "NULL"
243
- else
244
- debugEntries["MatchedConfig"] = matchedConfig["Name"]
245
- end
246
- end
247
-
248
- if(matchedConfig == nil)
249
- return RequestValidationResult.new(nil, nil, nil, nil, nil)
250
- end
251
-
252
- # unspecified or 'Queue' specified
253
- if(!matchedConfig.key?("ActionType") || Utils.isNilOrEmpty(matchedConfig["ActionType"]) || matchedConfig["ActionType"].eql?(ActionTypes::QUEUE))
254
- return handleQueueAction(currentUrlWithoutQueueITToken, queueitToken, customerIntegration, customerId, secretKey, matchedConfig, request, debugEntries)
255
-
256
- elsif(matchedConfig["ActionType"].eql?(ActionTypes::CANCEL))
257
- return handleCancelAction(currentUrlWithoutQueueITToken, queueitToken, customerIntegration, customerId, secretKey, matchedConfig, request, debugEntries)
258
-
259
- # for all unknown types default to 'Ignore'
260
- else
261
- userInQueueService = getUserInQueueService(request.cookie_jar)
262
- result = userInQueueService.getIgnoreActionResult()
263
- result.isAjaxResult = isQueueAjaxCall(request)
264
-
265
- return result
266
- end
267
-
268
- rescue StandardError => stdErr
269
- raise KnownUserError, "integrationConfiguration text was not valid: " + stdErr.message
270
- ensure
271
- setDebugCookie(debugEntries, request.cookie_jar)
272
- end
273
- end
274
-
275
- def self.handleQueueAction(currentUrlWithoutQueueITToken, queueitToken, customerIntegration, customerId, secretKey, matchedConfig, request, debugEntries)
276
- queueConfig = QueueEventConfig.new
277
- queueConfig.eventId = matchedConfig["EventId"]
278
- queueConfig.queueDomain = matchedConfig["QueueDomain"]
279
- queueConfig.layoutName = matchedConfig["LayoutName"]
280
- queueConfig.culture = matchedConfig["Culture"]
281
- queueConfig.cookieDomain = matchedConfig["CookieDomain"]
282
- queueConfig.extendCookieValidity = matchedConfig["ExtendCookieValidity"]
283
- queueConfig.cookieValidityMinute = matchedConfig["CookieValidityMinute"]
284
- queueConfig.version = customerIntegration["Version"]
285
-
286
- case matchedConfig["RedirectLogic"]
287
- when "ForcedTargetUrl"
288
- targetUrl = matchedConfig["ForcedTargetUrl"]
289
- when "EventTargetUrl"
290
- targetUrl = ''
291
- else
292
- targetUrl = generateTargetUrl(currentUrlWithoutQueueITToken, request)
293
- end
294
-
295
- return _resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, secretKey, request, debugEntries)
296
- end
297
-
298
- def self.handleCancelAction(currentUrlWithoutQueueITToken, queueitToken, customerIntegration, customerId, secretKey, matchedConfig, request, debugEntries)
299
- cancelConfig = CancelEventConfig.new
300
- cancelConfig.eventId = matchedConfig["EventId"]
301
- cancelConfig.queueDomain = matchedConfig["QueueDomain"]
302
- cancelConfig.cookieDomain = matchedConfig["CookieDomain"]
303
- cancelConfig.version = customerIntegration["Version"]
304
-
305
- return _cancelRequestByLocalConfig(currentUrlWithoutQueueITToken, queueitToken, cancelConfig, customerId, secretKey, request, debugEntries)
306
- end
307
-
308
- def self.cancelRequestByLocalConfig(targetUrl, queueitToken, cancelConfig, customerId, secretKey, request)
309
- debugEntries = Hash.new
310
- begin
311
- return _cancelRequestByLocalConfig(targetUrl, queueitToken, cancelConfig, customerId, secretKey, request, debugEntries)
312
- ensure
313
- setDebugCookie(debugEntries, request.cookie_jar)
314
- end
315
- end
316
-
317
- def self.getRealOriginalUrl(request)
318
- # RoR could modify request.original_url if request contains x-forwarded-host/proto http headers.
319
- # Therefore we need this method to be able to access the 'real' original url.
320
- return request.env["rack.url_scheme"] + "://" + request.env["HTTP_HOST"] + request.original_fullpath
321
- end
322
- end
323
-
324
- class CookieManager
325
- @cookies = {}
326
-
327
- def initialize(cookieJar)
328
- @cookies = cookieJar
329
- end
330
-
331
- def getCookie(name)
332
- key = name.to_sym
333
- if(!Utils.isNilOrEmpty(@cookies[key]))
334
- return @cookies[key]
335
- end
336
- return nil
337
- end
338
-
339
- def setCookie(name, value, expire, domain)
340
- key = name.to_sym
341
- noDomain = Utils.isNilOrEmpty(domain)
342
- deleteCookie = Utils.isNilOrEmpty(value)
343
- noExpire = Utils.isNilOrEmpty(expire)
344
-
345
- if(noDomain)
346
- if(deleteCookie)
347
- @cookies.delete(key)
348
- else
349
- if(noExpire)
350
- @cookies[key] = { :value => value, :httponly => false }
351
- else
352
- @cookies[key] = { :value => value, :expires => expire, :httponly => false }
353
- end
354
- end
355
- else
356
- if(deleteCookie)
357
- @cookies.delete(key, :domain => domain)
358
- else
359
- if(noExpire)
360
- @cookies[key] = { :value => value, :domain => domain, :httponly => false }
361
- else
362
- @cookies[key] = { :value => value, :expires => expire, :domain => domain, :httponly => false }
363
- end
364
- end
365
- end
366
- end
367
- end
368
- end
369
-
370
-
371
-
1
+ require 'cgi'
2
+ require 'json'
3
+
4
+ module QueueIt
5
+
6
+ class KnownUser
7
+ QUEUEIT_TOKEN_KEY = "queueittoken"
8
+ QUEUEIT_DEBUG_KEY = "queueitdebug"
9
+ QUEUEIT_AJAX_HEADER_KEY = "x-queueit-ajaxpageurl"
10
+
11
+ @@userInQueueService = nil
12
+ def self.getUserInQueueService()
13
+ if (@@userInQueueService == nil)
14
+ return UserInQueueService.new(UserInQueueStateCookieRepository.new(HttpContextProvider.httpContext.cookieManager))
15
+ end
16
+
17
+ return @@userInQueueService
18
+ end
19
+ private_class_method :getUserInQueueService
20
+
21
+ def self.isQueueAjaxCall
22
+ headers = HttpContextProvider.httpContext.headers
23
+ return headers[QUEUEIT_AJAX_HEADER_KEY] != nil
24
+ end
25
+ private_class_method :isQueueAjaxCall
26
+
27
+ def self.generateTargetUrl(originalTargetUrl)
28
+ unless isQueueAjaxCall()
29
+ return originalTargetUrl
30
+ end
31
+ headers = HttpContextProvider.httpContext.headers
32
+ return CGI::unescape(headers[QUEUEIT_AJAX_HEADER_KEY])
33
+ end
34
+ private_class_method :generateTargetUrl
35
+
36
+ def self.convertToInt(value)
37
+ begin
38
+ converted = Integer(value)
39
+ rescue
40
+ converted = 0
41
+ end
42
+ return converted
43
+ end
44
+ private_class_method :convertToInt
45
+
46
+ def self.logMoreRequestDetails(debugEntries)
47
+ httpContext = HttpContextProvider.httpContext
48
+ headers = httpContext.headers
49
+
50
+ debugEntries["ServerUtcTime"] = Time.now.utc.iso8601
51
+ debugEntries["RequestIP"] = httpContext.userHostAddress
52
+ debugEntries["RequestHttpHeader_Via"] = headers["via"]
53
+ debugEntries["RequestHttpHeader_Forwarded"] = headers["forwarded"]
54
+ debugEntries["RequestHttpHeader_XForwardedFor"] = headers["x-forwarded-for"]
55
+ debugEntries["RequestHttpHeader_XForwardedHost"] = headers["x-forwarded-host"]
56
+ debugEntries["RequestHttpHeader_XForwardedProto"] = headers["x-forwarded-proto"]
57
+ end
58
+ private_class_method :logMoreRequestDetails
59
+
60
+ def self.setDebugCookie(debugEntries)
61
+ if(debugEntries == nil || debugEntries.length == 0)
62
+ return
63
+ end
64
+
65
+ cookieManager = HttpContextProvider.httpContext.cookieManager
66
+ cookieValue = ''
67
+ debugEntries.each do |entry|
68
+ cookieValue << (entry[0].to_s + '=' + entry[1].to_s + '|')
69
+ end
70
+ cookieValue = cookieValue.chop # remove trailing char
71
+ cookieManager.setCookie(QUEUEIT_DEBUG_KEY, cookieValue, nil, nil, false, false)
72
+ end
73
+ private_class_method :setDebugCookie
74
+
75
+ def self._resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, secretKey, debugEntries, isDebug)
76
+
77
+ if(isDebug)
78
+ debugEntries["SdkVersion"] = UserInQueueService::SDK_VERSION
79
+ debugEntries["Runtime"] = getRuntime()
80
+ debugEntries["TargetUrl"] = targetUrl
81
+ debugEntries["QueueitToken"] = queueitToken
82
+ debugEntries["OriginalUrl"] = getRealOriginalUrl()
83
+ if(queueConfig == nil)
84
+ debugEntries["QueueConfig"] = "NULL"
85
+ else
86
+ debugEntries["QueueConfig"] = queueConfig.toString()
87
+ end
88
+ logMoreRequestDetails(debugEntries)
89
+ end
90
+
91
+ if(Utils.isNilOrEmpty(customerId))
92
+ raise KnownUserError, "customerId can not be nil or empty."
93
+ end
94
+
95
+ if(Utils.isNilOrEmpty(secretKey))
96
+ raise KnownUserError, "secretKey can not be nil or empty."
97
+ end
98
+
99
+ if(queueConfig == nil)
100
+ raise KnownUserError, "queueConfig can not be nil."
101
+ end
102
+
103
+ if(Utils.isNilOrEmpty(queueConfig.eventId))
104
+ raise KnownUserError, "queueConfig.eventId can not be nil or empty."
105
+ end
106
+
107
+ if(Utils.isNilOrEmpty(queueConfig.queueDomain))
108
+ raise KnownUserError, "queueConfig.queueDomain can not be nil or empty."
109
+ end
110
+
111
+ minutes = convertToInt(queueConfig.cookieValidityMinute)
112
+ if(minutes <= 0)
113
+ raise KnownUserError, "queueConfig.cookieValidityMinute should be integer greater than 0."
114
+ end
115
+
116
+ if(![true, false].include? queueConfig.extendCookieValidity)
117
+ raise KnownUserError, "queueConfig.extendCookieValidity should be valid boolean."
118
+ end
119
+
120
+ userInQueueService = getUserInQueueService()
121
+ result = userInQueueService.validateQueueRequest(targetUrl, queueitToken, queueConfig, customerId, secretKey)
122
+ result.isAjaxResult = isQueueAjaxCall()
123
+
124
+ return result
125
+ end
126
+ private_class_method :_resolveQueueRequestByLocalConfig
127
+
128
+ def self._cancelRequestByLocalConfig(targetUrl, queueitToken, cancelConfig, customerId, secretKey, debugEntries, isDebug)
129
+ targetUrl = generateTargetUrl(targetUrl)
130
+
131
+ if(isDebug)
132
+ debugEntries["SdkVersion"] = UserInQueueService::SDK_VERSION
133
+ debugEntries["Runtime"] = getRuntime()
134
+ debugEntries["TargetUrl"] = targetUrl
135
+ debugEntries["QueueitToken"] = queueitToken
136
+ debugEntries["OriginalUrl"] = getRealOriginalUrl()
137
+ if(cancelConfig == nil)
138
+ debugEntries["CancelConfig"] = "NULL"
139
+ else
140
+ debugEntries["CancelConfig"] = cancelConfig.toString()
141
+ end
142
+ logMoreRequestDetails(debugEntries)
143
+ end
144
+
145
+ if(Utils.isNilOrEmpty(targetUrl))
146
+ raise KnownUserError, "targetUrl can not be nil or empty."
147
+ end
148
+
149
+ if(Utils.isNilOrEmpty(customerId))
150
+ raise KnownUserError, "customerId can not be nil or empty."
151
+ end
152
+
153
+ if(Utils.isNilOrEmpty(secretKey))
154
+ raise KnownUserError, "secretKey can not be nil or empty."
155
+ end
156
+
157
+ if(cancelConfig == nil)
158
+ raise KnownUserError, "cancelConfig can not be nil."
159
+ end
160
+
161
+ if(Utils.isNilOrEmpty(cancelConfig.eventId))
162
+ raise KnownUserError, "cancelConfig.eventId can not be nil or empty."
163
+ end
164
+
165
+ if(Utils.isNilOrEmpty(cancelConfig.queueDomain))
166
+ raise KnownUserError, "cancelConfig.queueDomain can not be nil or empty."
167
+ end
168
+
169
+ userInQueueService = getUserInQueueService()
170
+ result = userInQueueService.validateCancelRequest(targetUrl, cancelConfig, customerId, secretKey)
171
+ result.isAjaxResult = isQueueAjaxCall()
172
+
173
+ return result
174
+ end
175
+ private_class_method :_cancelRequestByLocalConfig
176
+
177
+ def self.extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, isCookieHttpOnly, isCookieSecure, secretKey)
178
+ if(Utils.isNilOrEmpty(eventId))
179
+ raise KnownUserError, "eventId can not be nil or empty."
180
+ end
181
+
182
+ if(Utils.isNilOrEmpty(secretKey))
183
+ raise KnownUserError, "secretKey can not be nil or empty."
184
+ end
185
+
186
+ minutes = convertToInt(cookieValidityMinute)
187
+ if(minutes <= 0)
188
+ raise KnownUserError, "cookieValidityMinute should be integer greater than 0."
189
+ end
190
+
191
+ userInQueueService = getUserInQueueService()
192
+ userInQueueService.extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, isCookieHttpOnly, isCookieSecure, secretKey)
193
+ end
194
+
195
+ def self.resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, secretKey)
196
+ debugEntries = Hash.new
197
+ connectorDiagnostics = ConnectorDiagnostics.verify(customerId, secretKey, queueitToken)
198
+
199
+ if(connectorDiagnostics.hasError)
200
+ return connectorDiagnostics.validationResult
201
+ end
202
+ begin
203
+ targetUrl = generateTargetUrl(targetUrl)
204
+ return _resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, secretKey, debugEntries, connectorDiagnostics.isEnabled)
205
+ rescue Exception => e
206
+ if(connectorDiagnostics.isEnabled)
207
+ debugEntries["Exception"] = e.message
208
+ end
209
+ raise e
210
+ ensure
211
+ setDebugCookie(debugEntries)
212
+ end
213
+ end
214
+
215
+ def self.validateRequestByIntegrationConfig(currentUrlWithoutQueueITToken, queueitToken, integrationConfigJson, customerId, secretKey)
216
+ debugEntries = Hash.new
217
+ customerIntegration = Hash.new
218
+ connectorDiagnostics = ConnectorDiagnostics.verify(customerId, secretKey, queueitToken)
219
+
220
+ if(connectorDiagnostics.hasError)
221
+ return connectorDiagnostics.validationResult
222
+ end
223
+ begin
224
+ if(connectorDiagnostics.isEnabled)
225
+ debugEntries["SdkVersion"] = UserInQueueService::SDK_VERSION
226
+ debugEntries["Runtime"] = getRuntime()
227
+ debugEntries["PureUrl"] = currentUrlWithoutQueueITToken
228
+ debugEntries["QueueitToken"] = queueitToken
229
+ debugEntries["OriginalUrl"] = getRealOriginalUrl()
230
+ logMoreRequestDetails(debugEntries)
231
+ end
232
+
233
+ customerIntegration = JSON.parse(integrationConfigJson)
234
+
235
+ if(connectorDiagnostics.isEnabled)
236
+ if(customerIntegration.length != 0 and customerIntegration["Version"] != nil)
237
+ debugEntries["ConfigVersion"] = customerIntegration["Version"]
238
+ else
239
+ debugEntries["ConfigVersion"] = "NULL"
240
+ end
241
+ end
242
+
243
+ if(Utils.isNilOrEmpty(currentUrlWithoutQueueITToken))
244
+ raise KnownUserError, "currentUrlWithoutQueueITToken can not be nil or empty."
245
+ end
246
+
247
+ if(customerIntegration.length == 0 || customerIntegration["Version"] == nil)
248
+ raise KnownUserError, "integrationConfigJson is not valid json."
249
+ end
250
+
251
+ integrationEvaluator = IntegrationEvaluator.new
252
+ matchedConfig = integrationEvaluator.getMatchedIntegrationConfig(customerIntegration, currentUrlWithoutQueueITToken, HttpContextProvider.httpContext)
253
+
254
+ if(connectorDiagnostics.isEnabled)
255
+ if(matchedConfig == nil)
256
+ debugEntries["MatchedConfig"] = "NULL"
257
+ else
258
+ debugEntries["MatchedConfig"] = matchedConfig["Name"]
259
+ end
260
+ end
261
+
262
+ if(matchedConfig == nil)
263
+ return RequestValidationResult.new(nil, nil, nil, nil, nil, nil)
264
+ end
265
+
266
+ # unspecified or 'Queue' specified
267
+ if(!matchedConfig.key?("ActionType") || Utils.isNilOrEmpty(matchedConfig["ActionType"]) || matchedConfig["ActionType"].eql?(ActionTypes::QUEUE))
268
+ return handleQueueAction(currentUrlWithoutQueueITToken, queueitToken, customerIntegration,
269
+ customerId, secretKey, matchedConfig, debugEntries, connectorDiagnostics.isEnabled)
270
+
271
+ elsif(matchedConfig["ActionType"].eql?(ActionTypes::CANCEL))
272
+ return handleCancelAction(currentUrlWithoutQueueITToken, queueitToken, customerIntegration,
273
+ customerId, secretKey, matchedConfig, debugEntries, connectorDiagnostics.isEnabled)
274
+
275
+ # for all unknown types default to 'Ignore'
276
+ else
277
+ userInQueueService = getUserInQueueService()
278
+ result = userInQueueService.getIgnoreActionResult(matchedConfig["Name"])
279
+ result.isAjaxResult = isQueueAjaxCall()
280
+
281
+ return result
282
+ end
283
+ rescue Exception => e
284
+ if(connectorDiagnostics.isEnabled)
285
+ debugEntries["Exception"] = e.message
286
+ end
287
+ raise e
288
+ ensure
289
+ setDebugCookie(debugEntries)
290
+ end
291
+ end
292
+
293
+ def self.handleQueueAction(currentUrlWithoutQueueITToken, queueitToken, customerIntegration, customerId, secretKey, matchedConfig, debugEntries, isDebug)
294
+ queueConfig = QueueEventConfig.new
295
+ queueConfig.eventId = matchedConfig["EventId"]
296
+ queueConfig.layoutName = matchedConfig["LayoutName"]
297
+ queueConfig.culture = matchedConfig["Culture"]
298
+ queueConfig.queueDomain = matchedConfig["QueueDomain"]
299
+ queueConfig.extendCookieValidity = matchedConfig["ExtendCookieValidity"]
300
+ queueConfig.cookieValidityMinute = matchedConfig["CookieValidityMinute"]
301
+ queueConfig.cookieDomain = matchedConfig["CookieDomain"]
302
+ queueConfig.isCookieHttpOnly = matchedConfig["IsCookieHttpOnly"] || false
303
+ queueConfig.isCookieSecure = matchedConfig["IsCookieSecure"] || false
304
+ queueConfig.version = customerIntegration["Version"]
305
+ queueConfig.actionName = matchedConfig["Name"]
306
+
307
+ case matchedConfig["RedirectLogic"]
308
+ when "ForcedTargetUrl"
309
+ targetUrl = matchedConfig["ForcedTargetUrl"]
310
+ when "EventTargetUrl"
311
+ targetUrl = ''
312
+ else
313
+ targetUrl = generateTargetUrl(currentUrlWithoutQueueITToken)
314
+ end
315
+
316
+ return _resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, secretKey, debugEntries, isDebug)
317
+ end
318
+
319
+ def self.handleCancelAction(currentUrlWithoutQueueITToken, queueitToken, customerIntegration, customerId, secretKey, matchedConfig, debugEntries, isDebug)
320
+ cancelConfig = CancelEventConfig.new
321
+ cancelConfig.eventId = matchedConfig["EventId"]
322
+ cancelConfig.queueDomain = matchedConfig["QueueDomain"]
323
+ cancelConfig.cookieDomain = matchedConfig["CookieDomain"]
324
+ cancelConfig.isCookieHttpOnly = matchedConfig["IsCookieHttpOnly"] || false
325
+ cancelConfig.isCookieSecure = matchedConfig["IsCookieSecure"] || false
326
+ cancelConfig.version = customerIntegration["Version"]
327
+ cancelConfig.actionName = matchedConfig["Name"]
328
+
329
+ return _cancelRequestByLocalConfig(currentUrlWithoutQueueITToken, queueitToken, cancelConfig, customerId, secretKey, debugEntries, isDebug)
330
+ end
331
+
332
+ def self.cancelRequestByLocalConfig(targetUrl, queueitToken, cancelConfig, customerId, secretKey)
333
+ debugEntries = Hash.new
334
+ connectorDiagnostics = ConnectorDiagnostics.verify(customerId, secretKey, queueitToken)
335
+
336
+ if(connectorDiagnostics.hasError)
337
+ return connectorDiagnostics.validationResult
338
+ end
339
+ begin
340
+ return _cancelRequestByLocalConfig(targetUrl, queueitToken, cancelConfig, customerId, secretKey, debugEntries, connectorDiagnostics.isEnabled)
341
+ rescue Exception => e
342
+ if(connectorDiagnostics.isEnabled)
343
+ debugEntries["Exception"] = e.message
344
+ end
345
+ raise e
346
+ ensure
347
+ setDebugCookie(debugEntries)
348
+ end
349
+ end
350
+
351
+ def self.getRealOriginalUrl()
352
+ return HttpContextProvider.httpContext.url
353
+ # RoR could modify request.original_url if request contains x-forwarded-host/proto http headers.
354
+ # Therefore we need this method to be able to access the 'real' original url.
355
+ #return request.env["rack.url_scheme"] + "://" + request.env["HTTP_HOST"] + request.original_fullpath
356
+ end
357
+
358
+ def self.getRuntime()
359
+ return RUBY_VERSION.to_s
360
+ end
361
+ end
362
+
363
+ class CookieManager
364
+ @cookies = {}
365
+
366
+ def initialize(cookieJar)
367
+ @cookies = cookieJar
368
+ end
369
+
370
+ def getCookie(name)
371
+ key = name.to_sym
372
+ if(!Utils.isNilOrEmpty(@cookies[key]))
373
+ return @cookies[key]
374
+ end
375
+ return nil
376
+ end
377
+
378
+ def setCookie(name, value, expire, domain, isHttpOnly, isSecure)
379
+ key = name.to_sym
380
+ noDomain = Utils.isNilOrEmpty(domain)
381
+ deleteCookie = Utils.isNilOrEmpty(value)
382
+ noExpire = Utils.isNilOrEmpty(expire)
383
+
384
+ if(noDomain)
385
+ if(deleteCookie)
386
+ @cookies.delete(key)
387
+ else
388
+ if(noExpire)
389
+ @cookies[key] = { :value => value, :httponly => isHttpOnly, :secure => isSecure }
390
+ else
391
+ @cookies[key] = { :value => value, :expires => expire, :httponly => isHttpOnly, :secure => isSecure }
392
+ end
393
+ end
394
+ else
395
+ if(deleteCookie)
396
+ @cookies.delete(key, :domain => domain)
397
+ else
398
+ if(noExpire)
399
+ @cookies[key] = { :value => value, :domain => domain, :httponly => isHttpOnly, :secure => isSecure }
400
+ else
401
+ @cookies[key] = { :value => value, :expires => expire, :domain => domain, :httponly => isHttpOnly, :secure => isSecure }
402
+ end
403
+ end
404
+ end
405
+ end
406
+ end
407
+ end
408
+
409
+
410
+