View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.auth;
17  
18  import java.security.InvalidAlgorithmParameterException;
19  import java.security.InvalidKeyException;
20  import java.security.NoSuchAlgorithmException;
21  
22  import javax.crypto.BadPaddingException;
23  import javax.crypto.IllegalBlockSizeException;
24  import javax.crypto.NoSuchPaddingException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.juddi.cryptor.Cryptor;
29  import org.apache.juddi.cryptor.CryptorFactory;
30  import org.apache.juddi.error.RegistryException;
31  import org.apache.juddi.error.UnknownUserException;
32  
33  /***
34   * @author Anou Manavalan
35   */
36  public class CryptedXMLDocAuthenticator extends XMLDocAuthenticator
37  {
38    // private reference to the jUDDI logger
39    private static Log log = LogFactory.getLog(CryptedXMLDocAuthenticator.class);
40  
41    /***
42     *
43     */
44    public CryptedXMLDocAuthenticator()
45    {
46      super();
47    }
48  
49    /***
50     *
51     */
52    public String authenticate(String userID,String credential)
53      throws RegistryException
54    {
55      preProcess(userID,credential);
56      String encryptedCredential = encrypt(credential);
57      return postProcess(userID, encryptedCredential);
58    }
59  
60    /***
61     *
62     */
63    private String encrypt(String str) 
64      throws RegistryException
65    {
66      try
67      {
68        Cryptor cryptor = (Cryptor)CryptorFactory.getCryptor();
69    
70        return cryptor.encrypt(str);
71      }
72      catch (InvalidKeyException e)
73      {
74        log.error("Invalid Key Exception in crypting the password",e);
75        throw new RegistryException(e.getMessage());
76      }
77      catch (NoSuchPaddingException e)
78      {
79        log.error("Padding Exception in crypting the password",e);
80        throw new RegistryException(e.getMessage());
81      }
82      catch (NoSuchAlgorithmException e)
83      {
84        log.error("Algorithm Exception in crypting the password",e);
85        throw new RegistryException(e.getMessage());
86      }
87      catch (InvalidAlgorithmParameterException e)
88      {
89        log.error("Algorithm parameter Exception in crypting the password",e);
90        throw new RegistryException(e.getMessage());
91      }
92      catch (IllegalBlockSizeException e)
93      {
94        log.error("Block size Exception in crypting the password",e);
95        throw new RegistryException(e.getMessage());
96      }
97      catch (BadPaddingException e)
98      {
99        log.error("Bad Padding Exception in crypting the password",e);
100       throw new RegistryException(e.getMessage());
101     }
102   }
103 
104   /***
105    * @param userID
106    * @param credential
107    * @throws RegistryException
108    */
109   private void preProcess(String userID, String credential) 
110     throws RegistryException
111   {
112     // a userID must be specified.
113     if (userID == null)
114       //throw new UnknownUserException("Invalid user ID: "+userID);
115       throw new UnknownUserException("Invalid user ID = "+userID);
116 
117     // credential (password) must be specified.
118     if (credential == null)
119       //throw new UnknownUserException("Invalid credentials");
120       throw new UnknownUserException("Invalid credentials");
121   }
122 
123   /***
124    * @param userID
125    * @param encryptedCredential
126    * @return
127    * @throws RegistryException
128    */
129   private String postProcess(String userID, String encryptedCredential) 
130     throws RegistryException
131   {
132     if (userTable.containsKey(userID))
133     {
134       UserInfo userInfo = (UserInfo)userTable.get(userID);
135       if ((userInfo.password == null) || (!encryptedCredential.equals(userInfo.password)))
136       throw new UnknownUserException("Invalid credentials");
137     }
138     else
139       throw new UnknownUserException("Invalid user ID: "+userID);
140 
141     return userID;
142   }
143 
144 
145   /****************************************************************************/
146   /****************************** TEST DRIVER *********************************/
147   /****************************************************************************/
148 
149 
150   public static void main(String[] args)
151     throws Exception
152   {
153     Authenticator auth = new CryptedXMLDocAuthenticator();
154 
155     try {
156       System.out.print("anou_mana/password: ");
157       auth.authenticate("anou_mana","password");
158       System.out.println("successfully authenticated");
159     }
160     catch(Exception ex) {
161       System.out.println(ex.getMessage());
162     }
163 
164     try {
165       System.out.print("anou_mana/badpass: ");
166       auth.authenticate("anou_mana","badpass");
167       System.out.println("successfully authenticated");
168     }
169     catch(Exception ex) {
170       System.out.println(ex.getMessage());
171     }
172 
173     try {
174       System.out.print("bozo/clown: ");
175       auth.authenticate("bozo","clown");
176       System.out.println("successfully authenticated");
177     }
178     catch(Exception ex) {
179       System.out.println(ex.getMessage());
180     }
181 
182     try {
183       System.out.print("sviens/password: ");
184       auth.authenticate("sviens","password");
185       System.out.println("successfully authenticated");
186     }
187     catch(Exception ex) {
188       System.out.println(ex.getMessage());
189     }
190   }
191 }