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.users.User;
21 import org.apache.archiva.redback.policy.PasswordRuleViolations;
22 import org.apache.archiva.redback.policy.UserSecurityPolicy;
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 at least {@link #setMinimumCount(int)} of
30 * numerical characters contained within.
31 *
32 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
33 */
34 @Service( "passwordRule#numerical-count" )
35 public class NumericalPasswordRule
36 extends AbstractPasswordRule
37 {
38
39 private int minimumCount;
40
41 private int countDigitCharacters( String password )
42 {
43 int count = 0;
44
45 if ( StringUtils.isEmpty( password ) )
46 {
47 return count;
48 }
49
50 /* TODO: Eventually upgrade to the JDK 1.5 Technique
51 *
52 * // Doing this via iteration of code points to take in account localized numbers.
53 * for ( int i = 0; i < password.length(); i++ )
54 * {
55 * int codepoint = password.codePointAt( i );
56 * if ( Character.isDigit( codepoint ) )
57 * {
58 * count++;
59 * }
60 * }
61 */
62
63 // JDK 1.4 Technique - NOT LOCALIZED.
64 for ( int i = 0; i < password.length(); i++ )
65 {
66 char c = password.charAt( i );
67 if ( Character.isDigit( c ) )
68 {
69 count++;
70 }
71 }
72
73 return count;
74 }
75
76 public int getMinimumCount()
77 {
78 return minimumCount;
79 }
80
81 public void setMinimumCount( int minimumCount )
82 {
83 this.minimumCount = minimumCount;
84 }
85
86 public void setUserSecurityPolicy( UserSecurityPolicy policy )
87 {
88 // Ignore, policy not needed in this rule.
89 }
90
91 public void testPassword( PasswordRuleViolations violations, User user )
92 {
93 if ( countDigitCharacters( user.getPassword() ) < this.minimumCount )
94 {
95 violations.addViolation( UserConfigurationKeys.NUMERICAL_COUNT_VIOLATION,
96 new String[]{ String.valueOf( minimumCount ) } ); //$NON-NLS-1$
97 }
98 }
99
100 @PostConstruct
101 public void initialize()
102 {
103 enabled = config.getBoolean( UserConfigurationKeys.POLICY_PASSWORD_RULE_NUMERICALCOUNT_ENABLED );
104 this.minimumCount = config.getInt( UserConfigurationKeys.MINIMUM );
105 }
106 }