KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > authenticate > FileSystemAuthenticator


1 /*
2  * Created on Jul 10, 2006
3  */

4 package com.openedit.users.authenticate;
5
6 import com.openedit.OpenEditException;
7 import com.openedit.users.Authenticator;
8 import com.openedit.users.StringEncrypter;
9 import com.openedit.users.User;
10 import com.openedit.users.UserManagerException;
11
12
13 public class FileSystemAuthenticator implements Authenticator
14 {
15     public boolean authenticate(User inUser, String JavaDoc inPassword) throws UserManagerException
16     {
17         String JavaDoc password = inUser.getPassword();
18         if ( password != null)
19         {
20             //Decrypt their stored password
21
if( password.startsWith("DES:"))
22             {
23                 if ( inPassword.startsWith("DES:"))
24                 {
25                     return inPassword.equals(password); //there are both encrypted so just compare
26
}
27                 else
28                 {
29                     String JavaDoc decryptedString = decrypt(password);
30                         if ( decryptedString.equals(inPassword))
31                         {
32                             return true;
33                         }
34                 }
35             }
36             else if ( password.equals(inPassword))
37             {
38                 return true;
39             }
40         }
41         return false;
42     }
43
44     protected String JavaDoc decrypt(String JavaDoc inPassword) throws UserManagerException
45     {
46         long encryptionKey = 7939805759879765L; //TODO: Move this to properties file
47
encryptionKey++;
48         try
49         {
50             StringEncrypter encrypter = new StringEncrypter( StringEncrypter.DES_ENCRYPTION_SCHEME, encryptionKey + "42" + encryptionKey );
51             String JavaDoc code = inPassword.substring(4,inPassword.length()); //take off the DES:
52
String JavaDoc decryptedString = encrypter.decrypt( code );
53             return decryptedString;
54         } catch ( Exception JavaDoc ex)
55         {
56             throw new UserManagerException(ex);
57         }
58     }
59     
60     public String JavaDoc encrypt(String JavaDoc inPassword) throws UserManagerException
61     {
62         try
63         {
64             long encryptionKey = 7939805759879765L; encryptionKey++;
65             StringEncrypter encrypter = new StringEncrypter( StringEncrypter.DES_ENCRYPTION_SCHEME, encryptionKey + "42" + encryptionKey );
66             String JavaDoc decryptedString = encrypter.encrypt( inPassword );
67             return decryptedString;
68         } catch ( OpenEditException ex)
69         {
70             throw new UserManagerException(ex);
71         }
72     }
73
74     
75 }
76
Popular Tags