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,119 +1,168 @@
1
- require 'open-uri'
2
- require 'cgi'
3
-
4
- module QueueIt
5
- class UserInQueueService
6
- SDK_VERSION = "3.5.1"
7
-
8
- def initialize(userInQueueStateRepository)
9
- @userInQueueStateRepository = userInQueueStateRepository
10
- end
11
-
12
- def validateQueueRequest(targetUrl, queueitToken, config, customerId, secretKey)
13
- state = @userInQueueStateRepository.getState(config.eventId, config.cookieValidityMinute, secretKey, true)
14
- if (state.isValid)
15
- if (state.isStateExtendable && config.extendCookieValidity)
16
- @userInQueueStateRepository.store(
17
- config.eventId,
18
- state.queueId,
19
- nil,
20
- !Utils::isNilOrEmpty(config.cookieDomain) ? config.cookieDomain : '',
21
- state.redirectType,
22
- secretKey)
23
- end
24
- return RequestValidationResult.new(ActionTypes::QUEUE, config.eventId, state.queueId, nil, state.redirectType)
25
- end
26
-
27
- queueParams = QueueUrlParams::extractQueueParams(queueitToken)
28
- if(!queueParams.nil?)
29
- return getQueueITTokenValidationResult(targetUrl, config.eventId, config, queueParams, customerId, secretKey)
30
- else
31
- return getInQueueRedirectResult(targetUrl, config, customerId)
32
- end
33
- end
34
-
35
- def validateCancelRequest(targetUrl, cancelConfig, customerId, secretKey)
36
- state = @userInQueueStateRepository.getState(cancelConfig.eventId, -1, secretKey, false)
37
- if (state.isValid)
38
- @userInQueueStateRepository.cancelQueueCookie(cancelConfig.eventId, cancelConfig.cookieDomain)
39
- query = getQueryString(customerId, cancelConfig.eventId, cancelConfig.version, nil, nil) + ( !Utils::isNilOrEmpty(targetUrl) ? ("&r=" + CGI.escape(targetUrl)) : "" )
40
-
41
- domainAlias = cancelConfig.queueDomain
42
- if (!domainAlias.end_with?("/") )
43
- domainAlias = domainAlias + "/"
44
- end
45
-
46
- redirectUrl = "https://" + domainAlias + "cancel/" + customerId + "/" + cancelConfig.eventId + "/?" + query
47
- return RequestValidationResult.new(ActionTypes::CANCEL, cancelConfig.eventId, state.queueId, redirectUrl, state.redirectType)
48
- else
49
- return RequestValidationResult.new(ActionTypes::CANCEL, cancelConfig.eventId, nil, nil, nil)
50
- end
51
- end
52
-
53
- def getQueueITTokenValidationResult(targetUrl, eventId, config, queueParams,customerId, secretKey)
54
- calculatedHash = OpenSSL::HMAC.hexdigest('sha256', secretKey, queueParams.queueITTokenWithoutHash)
55
- if (calculatedHash.upcase() != queueParams.hashCode.upcase())
56
- return getVaidationErrorResult(customerId, targetUrl, config, queueParams, "hash")
57
- end
58
- if (queueParams.eventId.upcase() != eventId.upcase())
59
- return getVaidationErrorResult(customerId, targetUrl, config, queueParams, "eventid")
60
- end
61
- if (queueParams.timeStamp < Time.now.getutc.tv_sec)
62
- return getVaidationErrorResult(customerId, targetUrl, config, queueParams, "timestamp")
63
- end
64
-
65
- @userInQueueStateRepository.store(
66
- config.eventId,
67
- queueParams.queueId,
68
- queueParams.cookieValidityMinutes,
69
- !Utils::isNilOrEmpty(config.cookieDomain) ? config.cookieDomain : '',
70
- queueParams.redirectType,
71
- secretKey)
72
- return RequestValidationResult.new(ActionTypes::QUEUE, config.eventId, queueParams.queueId, nil, queueParams.redirectType)
73
- end
74
-
75
- def getVaidationErrorResult(customerId, targetUrl, config, qParams, errorCode)
76
- query = getQueryString(customerId, config.eventId, config.version, config.culture, config.layoutName) +
77
- "&queueittoken=" + qParams.queueITToken +
78
- "&ts=" + Time.now.getutc.tv_sec.to_s +
79
- (!Utils::isNilOrEmpty(targetUrl) ? ("&t=" + CGI.escape(targetUrl)) : "")
80
- domainAlias = config.queueDomain
81
- if (!domainAlias.end_with?("/") )
82
- domainAlias = domainAlias + "/"
83
- end
84
- redirectUrl = "https://" + domainAlias + "error/" + errorCode + "/?" + query
85
- return RequestValidationResult.new(ActionTypes::QUEUE, config.eventId, nil, redirectUrl, nil)
86
- end
87
-
88
- def getInQueueRedirectResult(targetUrl, config, customerId)
89
- redirectUrl = "https://" + config.queueDomain +
90
- "?" + getQueryString(customerId, config.eventId, config.version, config.culture, config.layoutName) +
91
- (!Utils::isNilOrEmpty(targetUrl) ? "&t=" +
92
- CGI.escape( targetUrl) : "")
93
- return RequestValidationResult.new(ActionTypes::QUEUE, config.eventId, nil, redirectUrl, nil)
94
- end
95
-
96
- def getQueryString(customerId, eventId, configVersion, culture, layoutName)
97
- queryStringList = Array.new
98
- queryStringList.push("c=" + CGI.escape(customerId))
99
- queryStringList.push("e=" + CGI.escape(eventId))
100
- queryStringList.push("ver=v3-ruby-" + SDK_VERSION)
101
- queryStringList.push("cver=" + (!configVersion.nil? ? configVersion.to_s : '-1'))
102
- if (!Utils::isNilOrEmpty(culture))
103
- queryStringList.push("cid=" + CGI.escape(culture))
104
- end
105
- if (!Utils::isNilOrEmpty(layoutName))
106
- queryStringList.push("l=" + CGI.escape(layoutName))
107
- end
108
- return queryStringList.join("&")
109
- end
110
-
111
- def extendQueueCookie(eventId, cookieValidityMinutes, cookieDomain, secretKey)
112
- @userInQueueStateRepository.reissueQueueCookie(eventId, cookieValidityMinutes, cookieDomain, secretKey)
113
- end
114
-
115
- def getIgnoreActionResult()
116
- return RequestValidationResult.new(ActionTypes::IGNORE, nil, nil, nil, nil)
117
- end
118
- end
119
- end
1
+ require 'open-uri'
2
+ require 'cgi'
3
+
4
+ module QueueIt
5
+ class UserInQueueService
6
+ SDK_VERSION_NO = "3.7.1"
7
+ SDK_VERSION = "v3-ruby-" + SDK_VERSION_NO
8
+
9
+ def initialize(userInQueueStateRepository)
10
+ @userInQueueStateRepository = userInQueueStateRepository
11
+ end
12
+
13
+ def validateQueueRequest(targetUrl, queueitToken, config, customerId, secretKey)
14
+ state = @userInQueueStateRepository.getState(config.eventId, config.cookieValidityMinute, secretKey, true)
15
+ if (state.isValid)
16
+ if (state.isStateExtendable && config.extendCookieValidity)
17
+ @userInQueueStateRepository.store(
18
+ config.eventId,
19
+ state.queueId,
20
+ nil,
21
+ !Utils::isNilOrEmpty(config.cookieDomain) ? config.cookieDomain : '',
22
+ config.isCookieHttpOnly,
23
+ config.isCookieSecure,
24
+ state.redirectType,
25
+ secretKey)
26
+ end
27
+ return RequestValidationResult.new(ActionTypes::QUEUE, config.eventId, state.queueId, nil, state.redirectType, config.actionName)
28
+ end
29
+
30
+ queueParams = QueueUrlParams::extractQueueParams(queueitToken)
31
+
32
+ requestValidationResult = nil
33
+ isTokenValid = false
34
+
35
+ if (!queueParams.nil?)
36
+ tokenValidationResult = validateToken(config, queueParams, secretKey)
37
+ isTokenValid = tokenValidationResult.isValid
38
+
39
+ if (isTokenValid)
40
+ requestValidationResult = getValidTokenResult(config, queueParams, secretKey)
41
+ else
42
+ requestValidationResult = getErrorResult(customerId, targetUrl, config, queueParams, tokenValidationResult.errorCode)
43
+ end
44
+ else
45
+ requestValidationResult = getQueueResult(targetUrl, config, customerId)
46
+ end
47
+
48
+ if (state.isFound && !isTokenValid)
49
+ @userInQueueStateRepository.cancelQueueCookie(config.eventId, config.cookieDomain, config.isCookieHttpOnly, config.isCookieSecure);
50
+ end
51
+
52
+ return requestValidationResult;
53
+ end
54
+
55
+ def validateCancelRequest(targetUrl, cancelConfig, customerId, secretKey)
56
+ state = @userInQueueStateRepository.getState(cancelConfig.eventId, -1, secretKey, false)
57
+ if (state.isValid)
58
+ @userInQueueStateRepository.cancelQueueCookie(cancelConfig.eventId, cancelConfig.cookieDomain, cancelConfig.isCookieHttpOnly, cancelConfig.isCookieSecure)
59
+
60
+ query = getQueryString(customerId, cancelConfig.eventId, cancelConfig.version, cancelConfig.actionName, nil, nil) +
61
+ (!Utils::isNilOrEmpty(targetUrl) ? ("&r=" + Utils.urlEncode(targetUrl)) : "" )
62
+
63
+ uriPath = "cancel/" + customerId + "/" + cancelConfig.eventId
64
+
65
+ if(!Utils::isNilOrEmpty(state.queueId))
66
+ uriPath = uriPath + "/" + state.queueId
67
+ end
68
+
69
+ redirectUrl = generateRedirectUrl(cancelConfig.queueDomain, uriPath, query)
70
+
71
+ return RequestValidationResult.new(ActionTypes::CANCEL, cancelConfig.eventId, state.queueId, redirectUrl, state.redirectType, cancelConfig.actionName)
72
+ else
73
+ return RequestValidationResult.new(ActionTypes::CANCEL, cancelConfig.eventId, nil, nil, nil, cancelConfig.actionName)
74
+ end
75
+ end
76
+
77
+ def getValidTokenResult(config, queueParams, secretKey)
78
+ @userInQueueStateRepository.store(
79
+ config.eventId,
80
+ queueParams.queueId,
81
+ queueParams.cookieValidityMinutes,
82
+ !Utils::isNilOrEmpty(config.cookieDomain) ? config.cookieDomain : '',
83
+ config.isCookieHttpOnly,
84
+ config.isCookieSecure,
85
+ queueParams.redirectType,
86
+ secretKey)
87
+
88
+ return RequestValidationResult.new(ActionTypes::QUEUE, config.eventId, queueParams.queueId, nil, queueParams.redirectType, config.actionName)
89
+ end
90
+
91
+ def getErrorResult(customerId, targetUrl, config, qParams, errorCode)
92
+ query = getQueryString(customerId, config.eventId, config.version, config.actionName, config.culture, config.layoutName) +
93
+ "&queueittoken=" + qParams.queueITToken +
94
+ "&ts=" + Time.now.getutc.tv_sec.to_s +
95
+ (!Utils::isNilOrEmpty(targetUrl) ? ("&t=" + Utils.urlEncode(targetUrl)) : "")
96
+
97
+ redirectUrl = generateRedirectUrl(config.queueDomain, "error/" + errorCode + "/", query)
98
+
99
+ return RequestValidationResult.new(ActionTypes::QUEUE, config.eventId, nil, redirectUrl, nil, config.actionName)
100
+ end
101
+
102
+ def getQueueResult(targetUrl, config, customerId)
103
+ query = getQueryString(customerId, config.eventId, config.version, config.actionName, config.culture, config.layoutName) +
104
+ (!Utils::isNilOrEmpty(targetUrl) ? "&t=" + Utils.urlEncode( targetUrl) : "")
105
+
106
+ redirectUrl = generateRedirectUrl(config.queueDomain, "", query)
107
+
108
+ return RequestValidationResult.new(ActionTypes::QUEUE, config.eventId, nil, redirectUrl, nil, config.actionName)
109
+ end
110
+
111
+ def getQueryString(customerId, eventId, configVersion, actionName, culture, layoutName)
112
+ queryStringList = Array.new
113
+ queryStringList.push("c=" + Utils.urlEncode(customerId))
114
+ queryStringList.push("e=" + Utils.urlEncode(eventId))
115
+ queryStringList.push("ver=" + SDK_VERSION)
116
+ queryStringList.push("cver=" + (!configVersion.nil? ? configVersion.to_s : '-1'))
117
+ queryStringList.push("man=" + Utils.urlEncode(actionName))
118
+
119
+ if (!Utils::isNilOrEmpty(culture))
120
+ queryStringList.push("cid=" + Utils.urlEncode(culture))
121
+ end
122
+ if (!Utils::isNilOrEmpty(layoutName))
123
+ queryStringList.push("l=" + Utils.urlEncode(layoutName))
124
+ end
125
+ return queryStringList.join("&")
126
+ end
127
+
128
+ def generateRedirectUrl(queueDomain, uriPath, query)
129
+ if (!queueDomain.end_with?("/") )
130
+ queueDomain = queueDomain + "/"
131
+ end
132
+ return "https://" + queueDomain + uriPath + "?" + query
133
+ end
134
+
135
+ def extendQueueCookie(eventId, cookieValidityMinutes, cookieDomain, isCookieHttpOnly, isCookieSecure, secretKey)
136
+ @userInQueueStateRepository.reissueQueueCookie(eventId, cookieValidityMinutes, cookieDomain, isCookieHttpOnly, isCookieSecure, secretKey)
137
+ end
138
+
139
+ def getIgnoreActionResult(actionName)
140
+ return RequestValidationResult.new(ActionTypes::IGNORE, nil, nil, nil, nil, actionName)
141
+ end
142
+
143
+ def validateToken(config, queueParams, secretKey)
144
+ calculatedHash = OpenSSL::HMAC.hexdigest('sha256', secretKey, queueParams.queueITTokenWithoutHash)
145
+ if (calculatedHash.upcase() != queueParams.hashCode.upcase())
146
+ return TokenValidationResult.new(false, "hash")
147
+ end
148
+ if (queueParams.eventId.upcase() != config.eventId.upcase())
149
+ return TokenValidationResult.new(false, "eventid")
150
+ end
151
+ if (queueParams.timeStamp < Time.now.getutc.tv_sec)
152
+ return TokenValidationResult.new(false, "timestamp")
153
+ end
154
+
155
+ return TokenValidationResult.new(true, nil)
156
+ end
157
+
158
+ class TokenValidationResult
159
+ attr_reader :isValid
160
+ attr_reader :errorCode
161
+
162
+ def initialize(isValid, errorCode)
163
+ @isValid = isValid
164
+ @errorCode = errorCode
165
+ end
166
+ end
167
+ end
168
+ end