11package queueit .security ;
22
3- import java .io .InputStream ;
43import java .io .UnsupportedEncodingException ;
54import java .net .URI ;
65import java .security .MessageDigest ;
76import java .security .NoSuchAlgorithmException ;
7+ import java .text .DateFormat ;
8+ import java .text .SimpleDateFormat ;
9+ import java .util .Collection ;
810import java .util .Date ;
911import java .util .Properties ;
12+ import java .util .TimeZone ;
1013import java .util .UUID ;
11- import java .util .concurrent .Callable ;
1214import javax .servlet .http .*;
1315import javax .xml .bind .DatatypeConverter ;
1416
1517public class CookieValidateResultRepository extends ValidateResultRepositoryBase {
1618
1719 static String defaultCookieDomain ;
1820 static int defaultCookieExpiration = 1200 ;
21+ static int defaultIdleExpiration = 180 ;
22+ static int defaultDisabledExpiration = 180 ;
1923
2024 static {
2125 loadConfiguration ();
@@ -28,11 +32,25 @@ private static void loadConfiguration()
2832 Properties props = QueueitProperties .getProperties ("queueit.properties" );
2933 defaultCookieDomain = props .getProperty ("cookieDomain" , null );
3034 defaultCookieExpiration = Integer .parseInt (props .getProperty ("cookieExpiration" , "1200" ));
35+ defaultIdleExpiration = Integer .parseInt (props .getProperty ("idleExpiration" , "180" ));
36+ defaultDisabledExpiration = Integer .parseInt (props .getProperty ("disabledExpiration" , "180" ));
3137 } catch (Exception e ) {
3238 // no need to handle exception
3339 }
3440 }
3541
42+ public static void configure (String cookieDomain , Integer cookieExpiration , Integer idleExpiration , Integer disabledExpiration )
43+ {
44+ if (cookieDomain != null )
45+ defaultCookieDomain = cookieDomain ;
46+ if (cookieExpiration != null )
47+ defaultCookieExpiration = cookieExpiration ;
48+ if (idleExpiration != null )
49+ defaultIdleExpiration = idleExpiration ;
50+ if (disabledExpiration != null )
51+ defaultDisabledExpiration = disabledExpiration ;
52+ }
53+
3654 @ Override
3755 public IValidateResult getValidationResult (IQueue queue ) {
3856
@@ -42,7 +60,7 @@ public IValidateResult getValidationResult(IQueue queue) {
4260 String queueId = null ;
4361 String originalUrl = null ;
4462 String encryptedPlaceInQueue = null ;
45- String redirectType = null ;
63+ RedirectType redirectType = null ;
4664 String timeStamp = null ;
4765 String actualHash = null ;
4866 Integer placeInQueue = 0 ;
@@ -57,7 +75,7 @@ public IValidateResult getValidationResult(IQueue queue) {
5775 if (cookies [i ].getName ().equals (key + "-PlaceInQueue" ))
5876 encryptedPlaceInQueue = cookies [i ].getValue ();
5977 if (cookies [i ].getName ().equals (key + "-RedirectType" ))
60- redirectType = cookies [i ].getValue ();
78+ redirectType = RedirectType . fromString ( cookies [i ].getValue () );
6179 if (cookies [i ].getName ().equals (key + "-TimeStamp" ))
6280 timeStamp = cookies [i ].getValue ();
6381 if (cookies [i ].getName ().equals (key + "-Hash" ))
@@ -74,12 +92,13 @@ public IValidateResult getValidationResult(IQueue queue) {
7492 return null ;
7593 }
7694
77- String expectedHash = generateHash (queueId , originalUrl , placeInQueue , redirectType , timeStamp );
95+ String expectedHash = generateHash (queueId , originalUrl , placeInQueue , redirectType . toString () , timeStamp );
7896
7997 if (!expectedHash .equals (actualHash ))
8098 return null ;
8199
82- setCookie (queue , queueId , originalUrl , placeInQueue , redirectType , timeStamp , actualHash );
100+ if (redirectType != RedirectType .Disabled && redirectType != RedirectType .Idle )
101+ setCookie (queue , queueId , originalUrl , placeInQueue , redirectType , timeStamp , actualHash , null );
83102
84103 return new AcceptedConfirmedResult (
85104 queue ,
@@ -89,13 +108,18 @@ public IValidateResult getValidationResult(IQueue queue) {
89108 new Date (Integer .parseInt (timeStamp )),
90109 queue .getCustomerId (),
91110 queue .getEventId (),
92- RedirectType . valueOf ( redirectType ) ,
111+ redirectType ,
93112 URI .create (originalUrl )),
94113 false );
95114 }
96115
97116 @ Override
98117 public void setValidationResult (IQueue queue , IValidateResult validationResult ) {
118+ this .setValidationResult (queue , validationResult , null );
119+ }
120+
121+ @ Override
122+ public void setValidationResult (IQueue queue , IValidateResult validationResult , Date expirationTime ) {
99123
100124 if (validationResult instanceof AcceptedConfirmedResult )
101125 {
@@ -104,37 +128,86 @@ public void setValidationResult(IQueue queue, IValidateResult validationResult)
104128 String queueId = confirmedResult .getKnownUser ().getQueueId ().toString ();
105129 String originalUrl = confirmedResult .getKnownUser ().getOriginalUrl ().toString ();
106130 Integer placeInQueue = confirmedResult .getKnownUser ().getPlaceInQueue ();
107- String redirectType = confirmedResult .getKnownUser ().getRedirectType (). toString ();
131+ RedirectType redirectType = confirmedResult .getKnownUser ().getRedirectType ();
108132 Long timeStamp = confirmedResult .getKnownUser ().getTimeStamp ().getTime () / 1000 ;
109133
110- String hash = generateHash (queueId , originalUrl , placeInQueue , redirectType , timeStamp .toString ());
134+ String hash = generateHash (queueId , originalUrl , placeInQueue , redirectType . toString () , timeStamp .toString ());
111135
112- setCookie (queue , queueId , originalUrl , placeInQueue , redirectType , timeStamp .toString (), hash );
136+ setCookie (queue , queueId , originalUrl , placeInQueue , redirectType , timeStamp .toString (), hash , expirationTime );
113137 }
114138 }
115139
116- private void setCookie (IQueue queue , String queueId , String originalUrl , Integer placeInQueue , String redirectType , String timeStamp , String hash )
140+ private void setCookie (
141+ IQueue queue ,
142+ String queueId ,
143+ String originalUrl ,
144+ Integer placeInQueue ,
145+ RedirectType redirectType ,
146+ String timeStamp ,
147+ String hash ,
148+ Date expirationTime )
117149 {
118150 String key = generateKey (queue .getCustomerId (), queue .getEventId ());
119151 HttpServletResponse response = RequestContext .getCurrentInstance ().getResponse ();
120152
121- addCookie (new Cookie (key + "-QueueId" , queueId ), response );
122- addCookie (new Cookie (key + "-OriginalUrl" , originalUrl ), response );
123- addCookie (new Cookie (key + "-PlaceInQueue" , Hashing .encryptPlaceInQueue (placeInQueue )), response );
124- addCookie (new Cookie (key + "-RedirectType" , redirectType ), response );
125- addCookie (new Cookie (key + "-TimeStamp" , timeStamp .toString ()), response );
126- addCookie (new Cookie (key + "-Hash" , hash ), response );
153+ int expiration ;
154+ Date now = new Date ();
155+
156+ if (expirationTime != null )
157+ expiration = (int )(expirationTime .getTime () - now .getTime ());
158+ else if (redirectType == RedirectType .Disabled )
159+ expiration = defaultDisabledExpiration ;
160+ else if (redirectType == RedirectType .Idle )
161+ expiration = defaultIdleExpiration ;
162+ else
163+ expiration = defaultCookieExpiration ;
164+
165+ this .clearExistingCookies (response , key , expiration );
166+
167+ addCookie (new Cookie (key + "-QueueId" , queueId ), response , expiration );
168+ addCookie (new Cookie (key + "-OriginalUrl" , originalUrl ), response , expiration );
169+ addCookie (new Cookie (key + "-PlaceInQueue" , Hashing .encryptPlaceInQueue (placeInQueue )), response , expiration );
170+ addCookie (new Cookie (key + "-RedirectType" , redirectType .toString ()), response , expiration );
171+ addCookie (new Cookie (key + "-TimeStamp" , timeStamp .toString ()), response , expiration );
172+ addCookie (new Cookie (key + "-Hash" , hash ), response , expiration );
127173 }
128174
129- private void addCookie (Cookie cookie , HttpServletResponse response )
130- {
175+ private void addCookie (Cookie cookie , HttpServletResponse response , int maxAge )
176+ {
131177 cookie .setHttpOnly (true );
132- cookie .setMaxAge (defaultCookieExpiration );
178+ cookie .setMaxAge (maxAge < 0 ? 0 : maxAge );
133179 if (defaultCookieDomain != null )
134180 cookie .setDomain (defaultCookieDomain );
135-
181+
136182 response .addCookie (cookie );
137183 }
184+
185+ private void clearExistingCookies (HttpServletResponse response , String key , int maxAge )
186+ {
187+ Collection <String > cookieHeaders = response .getHeaders ("Set-Cookie" );
188+
189+ if (maxAge > 0 )
190+ {
191+ Date expdate = new Date ();
192+ expdate .setTime (expdate .getTime () + (maxAge * 1000 ));
193+ DateFormat df = new SimpleDateFormat ("dd-MMM-yyyy kk:mm:ss z" );
194+ df .setTimeZone (TimeZone .getTimeZone ("GMT" ));
195+
196+ response .setHeader ("Set-Cookie" , key + "=true; Expires=" + df .format (expdate ) + ";" );
197+ }
198+ else
199+ {
200+ response .setHeader ("Set-Cookie" , key + "=true; Expires=Thu, 11-Sep-2014 11:27:49 GMT;" );
201+ }
202+
203+ for (String cookieHeader : cookieHeaders )
204+ {
205+ if (!cookieHeader .contains (key ))
206+ {
207+ response .addHeader ("Set-Cookie" , cookieHeader );
208+ }
209+ }
210+ }
138211
139212 private String generateHash (String queueId , String originalUrl , Integer placeInQueue , String redirectType , String timeStamp ) {
140213 try {
@@ -154,4 +227,9 @@ private String generateHash(String queueId, String originalUrl, Integer placeInQ
154227 return null ;
155228 }
156229 }
230+
231+ @ Override
232+ public void cancel (IQueue queue , IValidateResult validationResult ) {
233+ this .setValidationResult (queue , validationResult , new Date (System .currentTimeMillis ()-24 *60 *60 *1000 ));
234+ }
157235}
0 commit comments