1package org.apache.archiva.redback.policy.rules;
23/*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 at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819import org.apache.archiva.redback.configuration.UserConfigurationKeys;
20import org.apache.archiva.redback.policy.PasswordRuleViolations;
21import org.apache.archiva.redback.policy.UserSecurityPolicy;
22import org.apache.archiva.redback.users.User;
23import org.apache.commons.lang.StringUtils;
24import org.springframework.stereotype.Service;
2526import javax.annotation.PostConstruct;
2728/**29 * Basic Password Rule, Checks for non-empty passwords that have at least {@link #setMinimumCount(int)} of30 * alpha characters contained within.31 *32 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>33 */34 @Service( "passwordRule#alpha-count" )
35publicclassAlphaPasswordRule36extendsAbstractPasswordRule37 {
3839privateint minimumCount;
4041privateint countAlphaCharacters( String password )
42 {
43int count = 0;
4445if ( StringUtils.isEmpty( password ) )
46 {
47return count;
48 }
4950/* TODO: Eventually upgrade to the JDK 1.5 Technique51 * 52 * // Doing this via iteration of code points to take in account localized passwords.53 * for ( int i = 0; i < password.length(); i++ )54 * {55 * int codepoint = password.codePointAt( i );56 * if ( Character.isLetter( codepoint ) )57 * {58 * count++;59 * }60 * }61 */6263// JDK 1.4 Technique - NOT LOCALIZED.64for ( int i = 0; i < password.length(); i++ )
65 {
66char c = password.charAt( i );
67if ( Character.isLetter( c ) )
68 {
69 count++;
70 }
71 }
7273return count;
74 }
7576publicint getMinimumCount()
77 {
78return minimumCount;
79 }
8081publicvoid setMinimumCount( int minimumCount )
82 {
83this.minimumCount = minimumCount;
84 }
8586publicvoid setUserSecurityPolicy( UserSecurityPolicy policy )
87 {
88// Ignore, policy not needed in this rule.89 }
9091publicvoid testPassword( PasswordRuleViolations violations, User user )
92 {
93if ( countAlphaCharacters( user.getPassword() ) < this.minimumCount )
94 {
95 violations.addViolation( UserConfigurationKeys.ALPHA_COUNT_VIOLATION,
96new String[]{ String.valueOf( minimumCount ) } ); //$NON-NLS-1$97 }
98 }
99100 @PostConstruct
101publicvoid initialize()
102 {
103 enabled = config.getBoolean( UserConfigurationKeys.POLICY_PASSWORD_RULE_ALPHACOUNT_ENABLED );
104this.minimumCount = config.getInt( UserConfigurationKeys.ALPHA_COUNT_MIN );
105 }
106 }