View Javadoc

1   /* Copyright 2004, 2005 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>
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      //~ Static fields/initializers =============================================
35  
36      /*** Keyword for this channelProcessor */
37      public static final String DEFAULT_KEYWORD = "REQUIRES_CAPTCHA_BELOW_AVERAGE_TIME_IN_MILLIS_REQUESTS";
38  
39      //~ Constructors ===========================================================
40  
41      /***
42       * Constructor
43       */
44      public AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor() {
45          super();
46          this.setKeyword(DEFAULT_KEYWORD);
47      }
48  
49      //~ Methods ================================================================
50  
51      /***
52       * Verify if thresold is &gt; 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  }