This project has retired. For details please refer to its Attic page.
CharacterLengthPasswordRule xref
View Javadoc

1   package org.apache.archiva.redback.policy.rules;
2   
3   /*
4    * Copyright 2001-2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.archiva.redback.configuration.UserConfigurationKeys;
20  import org.apache.archiva.redback.policy.PasswordRuleViolations;
21  import org.apache.archiva.redback.policy.UserSecurityPolicy;
22  import org.apache.archiva.redback.users.User;
23  import org.apache.commons.lang.StringUtils;
24  import org.springframework.stereotype.Service;
25  
26  import javax.annotation.PostConstruct;
27  
28  /**
29   * Basic Password Rule, Checks for non-empty passwords that have between {@link #setMinimumCharacters(int)} and
30   * {@link #setMaximumCharacters(int)} characters in length.
31   *
32   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
33   */
34  @Service( "passwordRule#character-length" )
35  public class CharacterLengthPasswordRule
36      extends AbstractPasswordRule
37  {
38  
39      public static final int DEFAULT_CHARACTER_LENGTH_MAX = 8;
40  
41      private int minimumCharacters;
42  
43      private int maximumCharacters;
44  
45      public int getMaximumCharacters()
46      {
47          return maximumCharacters;
48      }
49  
50      public int getMinimumCharacters()
51      {
52          return minimumCharacters;
53      }
54  
55      public void setMaximumCharacters( int maximumCharacters )
56      {
57          this.maximumCharacters = maximumCharacters;
58      }
59  
60      public void setMinimumCharacters( int minimumCharacters )
61      {
62          this.minimumCharacters = minimumCharacters;
63      }
64  
65      public void setUserSecurityPolicy( UserSecurityPolicy policy )
66      {
67          // Ignore, policy not needed in this rule.
68      }
69  
70      public void testPassword( PasswordRuleViolations violations, User user )
71      {
72          if ( minimumCharacters > maximumCharacters )
73          {
74              /* this should caught up front during the configuration of the component */
75              // TODO: Throw runtime exception instead?
76              violations.addViolation( UserConfigurationKeys.CHARACTER_LENGTH_MISCONFIGURED_VIOLATION,
77                                       new String[]{ String.valueOf( minimumCharacters ),
78                                           String.valueOf( maximumCharacters ) } ); //$NON-NLS-1$
79          }
80  
81          String password = user.getPassword();
82  
83          if ( StringUtils.isEmpty( password ) || password.length() < minimumCharacters ||
84              password.length() > maximumCharacters )
85          {
86              violations.addViolation( UserConfigurationKeys.CHARACTER_LENGTH_VIOLATION,
87                                       new String[]{ String.valueOf( minimumCharacters ),
88                                           String.valueOf( maximumCharacters ) } ); //$NON-NLS-1$
89          }
90      }
91  
92      @PostConstruct
93      public void initialize()
94      {
95          enabled = config.getBoolean( UserConfigurationKeys.POLICY_PASSWORD_RULE_CHARACTERLENGTH_ENABLED );
96          this.minimumCharacters = config.getInt( UserConfigurationKeys.CHARACTER_LENGTH_MIN );
97          this.maximumCharacters = config.getInt( UserConfigurationKeys.CHARACTER_LENGTH_MAX );
98      }
99  }