01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.captcha;
17:
18: import org.springframework.util.Assert;
19:
20: /**
21: * <p>return false if thresold is lower than average time millis between any CaptchaChannelProcessorTemplate mapped
22: * urls requests and is human;<br>
23: * Default keyword : REQUIRES_CAPTCHA_BELOW_AVERAGE_TIME_IN_MILLIS_REQUESTS <br>
24: * Note : before first humanity check</p>
25: *
26: * @author Marc-Antoine Garrigue
27: * @version $Id: AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.java 1496 2006-05-23 13:38:33Z benalex $
28: */
29: public class AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
30: extends CaptchaChannelProcessorTemplate {
31: //~ Static fields/initializers =====================================================================================
32:
33: /** Keyword for this channelProcessor */
34: public static final String DEFAULT_KEYWORD = "REQUIRES_CAPTCHA_BELOW_AVERAGE_TIME_IN_MILLIS_REQUESTS";
35:
36: //~ Constructors ===================================================================================================
37:
38: /**
39: * Constructor
40: */
41: public AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor() {
42: super ();
43: this .setKeyword(DEFAULT_KEYWORD);
44: }
45:
46: //~ Methods ========================================================================================================
47:
48: /**
49: * Verify if thresold is > 0
50: *
51: * @throws Exception if false
52: */
53: public void afterPropertiesSet() throws Exception {
54: super .afterPropertiesSet();
55: Assert.isTrue(getThresold() > 0, "thresold must be > 0");
56: }
57:
58: /**
59: * Verify wheter the context is valid concerning humanity
60: *
61: * @param context
62: *
63: * @return true if valid, false otherwise
64: */
65: boolean isContextValidConcerningHumanity(
66: CaptchaSecurityContext context) {
67: int req = context.getHumanRestrictedResourcesRequestsCount();
68: float thresold = getThresold();
69: float duration = System.currentTimeMillis()
70: - context.getLastPassedCaptchaDateInMillis();
71: float average;
72:
73: if (req == 0) {
74: average = thresold + 1;
75: } else {
76: average = duration / req;
77: }
78:
79: if (context.isHuman() && (average > thresold)) {
80: logger
81: .debug("context is valid : average time between requests < thresold && is human");
82:
83: return true;
84: } else {
85: logger
86: .debug("context is not valid : request count > thresold or is not human");
87:
88: return false;
89: }
90: }
91: }
|