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

1   package org.apache.archiva.redback.policy.encoders;
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.policy.PasswordEncoder;
20  import org.apache.archiva.redback.policy.PasswordEncodingException;
21  import org.apache.archiva.redback.users.Messages;
22  import org.apache.commons.codec.binary.Base64;
23  import org.apache.commons.lang.StringUtils;
24  
25  import java.io.UnsupportedEncodingException;
26  import java.security.MessageDigest;
27  import java.security.NoSuchAlgorithmException;
28  
29  /**
30   * Abstract Password Encoder that uses the {@link MessageDigest} from JAAS.
31   *
32   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
33   *
34   */
35  public class AbstractJAASPasswordEncoder
36      implements PasswordEncoder
37  {
38      private String algorithm;
39  
40      private Object systemSalt;
41  
42      public AbstractJAASPasswordEncoder( String algorithm )
43      {
44          this.algorithm = algorithm;
45      }
46  
47      public void setSystemSalt( Object salt )
48      {
49          this.systemSalt = salt;
50      }
51  
52      public String encodePassword( String rawPass, Object salt )
53      {
54          if ( rawPass == null )
55          {
56              throw new IllegalArgumentException( "rawPass parameter cannot be null." );
57          }
58  
59          MessageDigest md = null;
60          try
61          {
62              md = MessageDigest.getInstance( this.algorithm );
63              String precode = rawPass;
64  
65              // Only checking for null, not using StringUtils.isNotEmpty() as
66              // whitespace can make up a valid salt. 
67              if ( salt != null )
68              {
69                  // Conforming to acegi password encoding standards for compatibility
70                  precode += "{" + salt + "}";
71              }
72              md.update( precode.getBytes( "UTF-8" ) ); //$NON-NLS-1$
73  
74              byte raw[] = md.digest();
75              Base64 base64 = new Base64( 0, new byte[0] );
76              return ( base64.encodeToString( raw ) );
77          }
78          catch ( NoSuchAlgorithmException e )
79          {
80              throw new PasswordEncodingException(
81                  Messages.getString( "password.encoder.no.such.algoritm", this.algorithm ), e ); //$NON-NLS-1$
82          }
83          catch ( UnsupportedEncodingException e )
84          {
85              throw new PasswordEncodingException( Messages.getString( "password.encoder.unsupported.encoding" ),
86                                                   e ); //$NON-NLS-1$
87          }
88      }
89  
90      public boolean isPasswordValid( String encPass, String rawPass, Object salt )
91      {
92          if ( StringUtils.isEmpty( encPass ) )
93          {
94              // TODO: Throw exception?
95              return false;
96          }
97  
98          // PLXREDBACK-36 Commented out because a user with an empty password can't login due to the checking.
99          // Empty password checking can also be achieve by turning on MustHavePasswordRule.
100         //if ( StringUtils.isEmpty( rawPass ) )
101         //{
102         //    TODO: Throw exception?
103         //    return false;
104         //}
105 
106         String testPass = encodePassword( rawPass, salt );
107         return ( encPass.equals( testPass ) );
108     }
109 
110     public String encodePassword( String rawPass )
111     {
112         return encodePassword( rawPass, this.systemSalt );
113     }
114 
115     public boolean isPasswordValid( String encPass, String rawPass )
116     {
117         return isPasswordValid( encPass, rawPass, this.systemSalt );
118     }
119 
120 }