1 package org.jgroups.auth; 2 3 import org.jgroups.Message; 4 import org.jgroups.util.Util; 5 6 import java.io.DataInputStream ; 7 import java.io.DataOutputStream ; 8 import java.io.IOException ; 9 import java.util.Properties ; 10 11 25 public class MD5Token extends AuthToken { 26 27 public static final String TOKEN_ATTR = "auth_value"; 28 public static final String TOKEN_TYPE = "token_hash"; 29 30 private String token = null; 31 private String hash_type = "MD5"; 32 33 public MD5Token(){ 34 } 36 37 public MD5Token(String token){ 38 this.token = hash(token); 39 } 40 41 public MD5Token(String token, String hash_type){ 42 this.token = hash(token); 43 this.hash_type = hash_type; 44 } 45 46 public void setValue(Properties properties){ 47 this.token = hash((String )properties.get(MD5Token.TOKEN_ATTR)); 48 properties.remove(MD5Token.TOKEN_ATTR); 49 50 if(properties.containsKey(MD5Token.TOKEN_TYPE)){ 51 hash_type = (String )properties.get(MD5Token.TOKEN_TYPE); 52 properties.remove(MD5Token.TOKEN_TYPE); 53 } 54 } 55 56 public String getName(){ 57 return "org.jgroups.auth.MD5Token"; 58 } 59 64 private String hash(String token){ 65 String 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 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 MD5Token serverToken = (MD5Token) token; 89 90 if((this.token != null) && (serverToken.token != null) && (this.token.equalsIgnoreCase(serverToken.token))){ 91 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 out) throws IOException { 111 if(log.isDebugEnabled()){ 112 log.debug("MD5Token writeTo()"); 113 } 114 Util.writeString(this.token, out); 115 } 116 117 public void readFrom(DataInputStream in) throws IOException , IllegalAccessException , InstantiationException { 118 if(log.isDebugEnabled()){ 119 log.debug("MD5Token readFrom()"); 120 } 121 this.token = Util.readString(in); 122 } 123 } 124 | Popular Tags |