Class: QueueIt::UserInQueueStateCookieRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb

Constant Summary collapse

QUEUEIT_DATA_KEY =
"QueueITAccepted-SDFrts345E-V3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookieManager) ⇒ UserInQueueStateCookieRepository

Returns a new instance of UserInQueueStateCookieRepository.



9
10
11
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 9

def initialize(cookieManager)
	@cookieManager = cookieManager
end

Class Method Details

.getCookieKey(eventId) ⇒ Object



26
27
28
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 26

def self.getCookieKey(eventId)
	 return QUEUEIT_DATA_KEY + '_' + eventId
end

Instance Method Details

#cancelQueueCookie(eventId, cookieDomain) ⇒ Object



13
14
15
16
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 13

def cancelQueueCookie(eventId, cookieDomain) 
	cookieKey = self.class.getCookieKey(eventId)
	@cookieManager.setCookie(cookieKey, nil, -1, cookieDomain)		
end

#createCookieValue(queueId, isStateExtendable, expirationTime, secretKey) ⇒ Object



30
31
32
33
34
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 30

def createCookieValue(queueId, isStateExtendable, expirationTime, secretKey) 
	hashValue = OpenSSL::HMAC.hexdigest('sha256', secretKey, queueId + isStateExtendable + expirationTime) 
	cookieValue = "QueueId=" + queueId + "&IsCookieExtendable=" + isStateExtendable + "&Expires=" + expirationTime + "&Hash=" + hashValue        
	return cookieValue
end

#extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, secretKey) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 79

def extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, secretKey)       
	cookieKey = self.class.getCookieKey(eventId)
	cookieValue = @cookieManager.getCookie(cookieKey)
	if (cookieValue.nil?) 
		return
	end
     
	cookieNameValueMap = getCookieNameValueMap(cookieValue)
	if (!isCookieValid(cookieNameValueMap, secretKey)) 
		return 
	end
	expirationTime = (Time.now.getutc.tv_sec + (cookieValidityMinute * 60)).to_s
	cookieValue = createCookieValue(cookieNameValueMap["QueueId"], cookieNameValueMap["IsCookieExtendable"], expirationTime, secretKey)
	@cookieManager.setCookie(cookieKey, cookieValue, Time.now + (24*60*60), cookieDomain)
end

#getCookieNameValueMap(cookieValue) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 36

def getCookieNameValueMap(cookieValue) 
	result = Hash.new 
	cookieNameValues = cookieValue.split("&")
	if (cookieNameValues.length != 4) 
		return result
	end

	cookieNameValues.each do |item|
		arr = item.split("=")
		if(arr.length == 2)
			result[arr[0]] = arr[1]
		end
	end
	return result
end

#getState(eventId, secretKey) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 95

def getState(eventId, secretKey) 
	cookieKey = cookieKey = self.class.getCookieKey(eventId)
	if (@cookieManager.getCookie(cookieKey).nil?) 
		return StateInfo.new(false, nil, false, 0)
	end
	cookieNameValueMap = getCookieNameValueMap(@cookieManager.getCookie(cookieKey))
	if (!isCookieValid(cookieNameValueMap, secretKey))
		return StateInfo.new(false, nil, false,0)
	end
	return StateInfo.new(
		true, 
		cookieNameValueMap["QueueId"], 
		cookieNameValueMap["IsCookieExtendable"] == 'true',
		Integer(cookieNameValueMap["Expires"]))
end

#isCookieValid(cookieNameValueMap, secretKey) ⇒ Object



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
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 52

def isCookieValid(cookieNameValueMap, secretKey) 
	begin
		if (!cookieNameValueMap.key?("IsCookieExtendable")) 
			return false
		end
		if (!cookieNameValueMap.key?("Expires")) 
			return false
		end
		if (!cookieNameValueMap.key?("Hash")) 
			return false
		end
		if (!cookieNameValueMap.key?("QueueId")) 
			return false
		end
		hashValue = OpenSSL::HMAC.hexdigest('sha256', secretKey, cookieNameValueMap["QueueId"] + cookieNameValueMap["IsCookieExtendable"] + cookieNameValueMap["Expires"]) 
		if (hashValue != cookieNameValueMap["Hash"]) 
			return false
		end		
		if(Integer(cookieNameValueMap["Expires"]) < Time.now.getutc.tv_sec) 
			return false
		end
		return true
	rescue
		return false
	end
end

#store(eventId, queueId, isStateExtendable, cookieValidityMinute, cookieDomain, secretKey) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/queueit_knownuserv3/user_in_queue_state_cookie_repository.rb', line 18

def store(eventId, queueId, isStateExtendable, cookieValidityMinute, cookieDomain, secretKey)
	cookieKey = self.class.getCookieKey(eventId)
	expirationTime = (Time.now.getutc.tv_sec + (cookieValidityMinute * 60)).to_s
	isStateExtendableString = (isStateExtendable) ? 'true' : 'false'
	cookieValue = createCookieValue(queueId, isStateExtendableString, expirationTime, secretKey)
	@cookieManager.setCookie(cookieKey, cookieValue, Time.now + (24*60*60), cookieDomain)
end