1 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * 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 /**
22 * <p>return false if thresold is lower than average time millis between any CaptchaChannelProcessorTemplate mapped
23 * urls requests and is human;<br>
24 * Default keyword : REQUIRES_CAPTCHA_BELOW_AVERAGE_TIME_IN_MILLIS_REQUESTS <br>
25 * Note : before first humanity check</p>
26 *
27 * @author Marc-Antoine Garrigue
28 * @version $Id: AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.java 1496 2006-05-23 13:38:33Z benalex $
29 */
30 public class AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor 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(CaptchaSecurityContext context) {
66 int req = context.getHumanRestrictedResourcesRequestsCount();
67 float thresold = getThresold();
68 float duration = System.currentTimeMillis() - context.getLastPassedCaptchaDateInMillis();
69 float average;
70
71 if (req == 0) {
72 average = thresold + 1;
73 } else {
74 average = duration / req;
75 }
76
77 if (context.isHuman() && (average > thresold)) {
78 logger.debug("context is valid : average time between requests < thresold && is human");
79
80 return true;
81 } else {
82 logger.debug("context is not valid : request count > thresold or is not human");
83
84 return false;
85 }
86 }
87 }