1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.captcha;
17
18 import org.springframework.util.Assert;
19
20
21 /***
22 * <p>
23 * return false if thresold is lower than average time millis between any
24 * CaptchaChannelProcessorTemplate mapped urls requests and is human;<br>
25 * Default keyword : REQUIRES_CAPTCHA_AFTER_THRESOLD_IN_MILLIS <br>
26 * Note : before first humanity check
27 * </p>
28 *
29 * @author Marc-Antoine Garrigue
30 * @version $Id: AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.java,v 1.2 2005/11/17 00:55:49 benalex Exp $
31 */
32 public class AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
33 extends CaptchaChannelProcessorTemplate {
34
35
36 /*** Keyword for this channelProcessor */
37 public static final String DEFAULT_KEYWORD = "REQUIRES_CAPTCHA_BELOW_AVERAGE_TIME_IN_MILLIS_REQUESTS";
38
39
40
41 /***
42 * Constructor
43 */
44 public AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor() {
45 super();
46 this.setKeyword(DEFAULT_KEYWORD);
47 }
48
49
50
51 /***
52 * Verify if thresold is > 0
53 *
54 * @throws Exception if false
55 */
56 public void afterPropertiesSet() throws Exception {
57 super.afterPropertiesSet();
58 Assert.isTrue(getThresold() > 0, "thresold must be > 0");
59 }
60
61 /***
62 * Verify wheter the context is valid concerning humanity
63 *
64 * @param context
65 *
66 * @return true if valid, false otherwise
67 */
68 boolean isContextValidConcerningHumanity(CaptchaSecurityContext context) {
69 int req = context.getHumanRestrictedResourcesRequestsCount();
70 float thresold = getThresold();
71 float duration = System.currentTimeMillis()
72 - context.getLastPassedCaptchaDateInMillis();
73 float average;
74
75 if (req == 0) {
76 average = thresold + 1;
77 } else {
78 average = duration / req;
79 }
80
81 if (context.isHuman() && (average > thresold)) {
82 logger.debug(
83 "context is valid : average time between requests < thresold && is human");
84
85 return true;
86 } else {
87 logger.debug(
88 "context is not valid : request count > thresold or is not human");
89
90 return false;
91 }
92 }
93 }