33
44module QueueIt
55 class UserInQueueService
6- SDK_VERSION_NO = "3.6.0 "
6+ SDK_VERSION_NO = "3.6.1 "
77 SDK_VERSION = "v3-ruby-" + SDK_VERSION_NO
88
99 def initialize ( userInQueueStateRepository )
@@ -27,11 +27,27 @@ def validateQueueRequest(targetUrl, queueitToken, config, customerId, secretKey)
2727
2828 queueParams = QueueUrlParams ::extractQueueParams ( queueitToken )
2929
30- if ( !queueParams . nil? )
31- return getQueueITTokenValidationResult ( targetUrl , config , queueParams , customerId , secretKey )
30+ requestValidationResult = nil
31+ isTokenValid = false
32+
33+ if ( !queueParams . nil? )
34+ tokenValidationResult = validateToken ( config , queueParams , secretKey )
35+ isTokenValid = tokenValidationResult . isValid
36+
37+ if ( isTokenValid )
38+ requestValidationResult = getValidTokenResult ( config , queueParams , secretKey )
39+ else
40+ requestValidationResult = getErrorResult ( customerId , targetUrl , config , queueParams , tokenValidationResult . errorCode )
41+ end
3242 else
33- return cancelQueueCookieReturnQueueResult ( targetUrl , config , customerId )
43+ requestValidationResult = getQueueResult ( targetUrl , config , customerId )
44+ end
45+
46+ if ( state . isFound && !isTokenValid )
47+ @userInQueueStateRepository . cancelQueueCookie ( config . eventId , config . cookieDomain ) ;
3448 end
49+
50+ return requestValidationResult ;
3551 end
3652
3753 def validateCancelRequest ( targetUrl , cancelConfig , customerId , secretKey )
@@ -49,31 +65,19 @@ def validateCancelRequest(targetUrl, cancelConfig, customerId, secretKey)
4965 end
5066 end
5167
52- def getQueueITTokenValidationResult ( targetUrl , config , queueParams , customerId , secretKey )
53- calculatedHash = OpenSSL ::HMAC . hexdigest ( 'sha256' , secretKey , queueParams . queueITTokenWithoutHash )
54- if ( calculatedHash . upcase ( ) != queueParams . hashCode . upcase ( ) )
55- return cancelQueueCookieReturnErrorResult ( customerId , targetUrl , config , queueParams , "hash" )
56- end
57- if ( queueParams . eventId . upcase ( ) != config . eventId . upcase ( ) )
58- return cancelQueueCookieReturnErrorResult ( customerId , targetUrl , config , queueParams , "eventid" )
59- end
60- if ( queueParams . timeStamp < Time . now . getutc . tv_sec )
61- return cancelQueueCookieReturnErrorResult ( customerId , targetUrl , config , queueParams , "timestamp" )
62- end
63-
68+ def getValidTokenResult ( config , queueParams , secretKey )
6469 @userInQueueStateRepository . store (
6570 config . eventId ,
6671 queueParams . queueId ,
6772 queueParams . cookieValidityMinutes ,
6873 !Utils ::isNilOrEmpty ( config . cookieDomain ) ? config . cookieDomain : '' ,
6974 queueParams . redirectType ,
7075 secretKey )
76+
7177 return RequestValidationResult . new ( ActionTypes ::QUEUE , config . eventId , queueParams . queueId , nil , queueParams . redirectType , config . actionName )
7278 end
7379
74- def cancelQueueCookieReturnErrorResult ( customerId , targetUrl , config , qParams , errorCode )
75- @userInQueueStateRepository . cancelQueueCookie ( config . eventId , config . cookieDomain )
76-
80+ def getErrorResult ( customerId , targetUrl , config , qParams , errorCode )
7781 query = getQueryString ( customerId , config . eventId , config . version , config . actionName , config . culture , config . layoutName ) +
7882 "&queueittoken=" + qParams . queueITToken +
7983 "&ts=" + Time . now . getutc . tv_sec . to_s +
@@ -84,9 +88,7 @@ def cancelQueueCookieReturnErrorResult(customerId, targetUrl, config, qParams, e
8488 return RequestValidationResult . new ( ActionTypes ::QUEUE , config . eventId , nil , redirectUrl , nil , config . actionName )
8589 end
8690
87- def cancelQueueCookieReturnQueueResult ( targetUrl , config , customerId )
88- @userInQueueStateRepository . cancelQueueCookie ( config . eventId , config . cookieDomain )
89-
91+ def getQueueResult ( targetUrl , config , customerId )
9092 query = getQueryString ( customerId , config . eventId , config . version , config . actionName , config . culture , config . layoutName ) +
9193 ( !Utils ::isNilOrEmpty ( targetUrl ) ? "&t=" + Utils . urlEncode ( targetUrl ) : "" )
9294
@@ -126,5 +128,30 @@ def extendQueueCookie(eventId, cookieValidityMinutes, cookieDomain, secretKey)
126128 def getIgnoreActionResult ( actionName )
127129 return RequestValidationResult . new ( ActionTypes ::IGNORE , nil , nil , nil , nil , actionName )
128130 end
131+
132+ def validateToken ( config , queueParams , secretKey )
133+ calculatedHash = OpenSSL ::HMAC . hexdigest ( 'sha256' , secretKey , queueParams . queueITTokenWithoutHash )
134+ if ( calculatedHash . upcase ( ) != queueParams . hashCode . upcase ( ) )
135+ return TokenValidationResult . new ( false , "hash" )
136+ end
137+ if ( queueParams . eventId . upcase ( ) != config . eventId . upcase ( ) )
138+ return TokenValidationResult . new ( false , "eventid" )
139+ end
140+ if ( queueParams . timeStamp < Time . now . getutc . tv_sec )
141+ return TokenValidationResult . new ( false , "timestamp" )
142+ end
143+
144+ return TokenValidationResult . new ( true , nil )
145+ end
146+
147+ class TokenValidationResult
148+ attr_reader :isValid
149+ attr_reader :errorCode
150+
151+ def initialize ( isValid , errorCode )
152+ @isValid = isValid
153+ @errorCode = errorCode
154+ end
155+ end
129156 end
130157end
0 commit comments