KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > auth > MD5Token


1 package org.jgroups.auth;
2
3 import org.jgroups.Message;
4 import org.jgroups.util.Util;
5
6 import java.io.DataInputStream JavaDoc;
7 import java.io.DataOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 /**
12  * <p>
13  * This is an example of using a preshared token that is encrypted using an MD5/SHA hash for authentication purposes. All members of the group have to have the same string value in the JGroups config.
14  *</p>
15  * <p>
16  * Configuration parameters for this example are shown below:
17  * </p>
18  * <ul>
19  * <li>token_hash (required) = MD5(default)/SHA</li>
20  * <li>auth_value (required) = the string to encrypt</li>
21  * </ul>
22  * @see org.jgroups.auth.AuthToken
23  * @author Chris Mills
24  */

25 public class MD5Token extends AuthToken {
26
27     public static final String JavaDoc TOKEN_ATTR = "auth_value";
28     public static final String JavaDoc TOKEN_TYPE = "token_hash";
29
30     private String JavaDoc token = null;
31     private String JavaDoc hash_type = "MD5";
32
33     public MD5Token(){
34         //need an empty constructor
35
}
36
37     public MD5Token(String JavaDoc token){
38         this.token = hash(token);
39     }
40
41     public MD5Token(String JavaDoc token, String JavaDoc hash_type){
42         this.token = hash(token);
43         this.hash_type = hash_type;
44     }
45
46     public void setValue(Properties JavaDoc properties){
47         this.token = hash((String JavaDoc)properties.get(MD5Token.TOKEN_ATTR));
48         properties.remove(MD5Token.TOKEN_ATTR);
49
50         if(properties.containsKey(MD5Token.TOKEN_TYPE)){
51             hash_type = (String JavaDoc)properties.get(MD5Token.TOKEN_TYPE);
52             properties.remove(MD5Token.TOKEN_TYPE);
53         }
54     }
55
56     public String JavaDoc getName(){
57         return "org.jgroups.auth.MD5Token";
58     }
59     /**
60      * Called during setup to hash the auth_value string in to an MD5/SHA hash
61      * @param token the string to hash
62      * @return the hashed version of the string
63      */

64     private String JavaDoc hash(String JavaDoc token){
65         //perform the hashing of the token key
66
String JavaDoc hashedToken = null;
67
68         if(hash_type.equalsIgnoreCase("SHA")){
69             hashedToken = Util.sha(token);
70         }else{
71             hashedToken = Util.md5(token);
72         }
73
74         if(hashedToken == null){
75             //failed to encrypt
76
if(log.isWarnEnabled()){
77                 log.warn("Failed to hash token - sending in clear text");
78             }
79             return token;
80         }
81         return hashedToken;
82     }
83
84     public boolean authenticate(AuthToken token, Message msg){
85
86         if((token != null) && (token instanceof MD5Token)){
87             //Found a valid Token to authenticate against
88
MD5Token serverToken = (MD5Token) token;
89
90             if((this.token != null) && (serverToken.token != null) && (this.token.equalsIgnoreCase(serverToken.token))){
91                 //validated
92
if(log.isDebugEnabled()){
93                     log.debug("MD5Token match");
94                 }
95                 return true;
96             }else{
97                 if(log.isWarnEnabled()){
98                     log.warn("Authentication failed on MD5Token");
99                 }
100                 return false;
101             }
102         }
103
104         if(log.isWarnEnabled()){
105             log.warn("Invalid AuthToken instance - wrong type or null");
106         }
107         return false;
108     }
109
110     public void writeTo(DataOutputStream JavaDoc out) throws IOException JavaDoc {
111         if(log.isDebugEnabled()){
112             log.debug("MD5Token writeTo()");
113         }
114         Util.writeString(this.token, out);
115     }
116
117     public void readFrom(DataInputStream JavaDoc in) throws IOException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
118         if(log.isDebugEnabled()){
119             log.debug("MD5Token readFrom()");
120         }
121         this.token = Util.readString(in);
122     }
123 }
124
Popular Tags