1 package org.jgroups.protocols; 2 3 4 import org.jgroups.Address; 5 import org.jgroups.Event; 6 import org.jgroups.Message; 7 import org.jgroups.auth.AuthToken; 8 import org.jgroups.protocols.pbcast.GMS; 9 import org.jgroups.protocols.pbcast.JoinRsp; 10 import org.jgroups.stack.Protocol; 11 import java.util.Properties ; 12 13 14 18 public class AUTH extends Protocol{ 19 20 static final String NAME = "AUTH"; 21 22 25 private AuthToken serverSideToken = null; 26 27 public AUTH(){ 28 } 29 30 public boolean setProperties(Properties props) { 31 32 String authClassString = props.getProperty("auth_class"); 33 34 if(authClassString != null){ 35 props.remove("auth_class"); 36 37 try{ 38 Object obj = Class.forName(authClassString).newInstance(); 39 serverSideToken = (AuthToken) obj; 40 serverSideToken.setValue(props); 41 }catch(Exception e){ 42 if(log.isFatalEnabled()){ 43 log.fatal("Failed to create server side token (" + authClassString + ")"); 44 log.fatal(e); 45 } 46 return false; 47 } 48 } 49 50 if(!props.isEmpty()) { 51 if(log.isErrorEnabled()){ 53 log.error("AUTH.setProperties(): the following properties are not recognized: " + props); 54 } 55 return false; 56 } 57 return true; 58 } 59 60 public final String getName() { 61 return AUTH.NAME; 62 } 63 69 private Event createFailureEvent(Address joiner, String message){ 70 Message msg = new Message(joiner, null, null); 71 72 if(log.isDebugEnabled()){ 73 log.debug("Creating JoinRsp with failure message - " + message); 74 } 75 JoinRsp joinRes = new JoinRsp(message); 76 78 GMS.GmsHeader gmsHeader = new GMS.GmsHeader(GMS.GmsHeader.JOIN_RSP, joinRes); 79 msg.putHeader(GMS.name, gmsHeader); 80 81 if(log.isDebugEnabled()){ 82 log.debug("GMSHeader created for failure JOIN_RSP"); 83 } 84 85 return new Event(Event.MSG, msg); 86 } 87 88 97 public Object up(Event evt) { 98 GMS.GmsHeader hdr = isJoinMessage(evt); 99 if((hdr != null) && (hdr.getType() == GMS.GmsHeader.JOIN_REQ)){ 100 if(log.isDebugEnabled()){ 101 log.debug("AUTH got up event"); 102 } 103 Message msg = (Message)evt.getArg(); 105 106 if((msg.getHeader(AUTH.NAME) != null) && (msg.getHeader(AUTH.NAME) instanceof AuthHeader)){ 107 AuthHeader authHeader = (AuthHeader)msg.getHeader(AUTH.NAME); 108 109 if(authHeader != null){ 110 if(this.serverSideToken.authenticate(authHeader.getToken(), msg)){ 112 if(log.isDebugEnabled()){ 114 log.debug("AUTH passing up event"); 115 } 116 up_prot.up(evt); 117 }else{ 118 if(log.isWarnEnabled()){ 120 log.warn("AUTH failed to validate AuthHeader token"); 121 } 122 down_prot.down(createFailureEvent(msg.getSrc(), "Authentication failed")); 123 } 124 }else{ 125 if(log.isWarnEnabled()){ 127 log.warn("AUTH failed to get valid AuthHeader from Message"); 128 } 129 down_prot.down(createFailureEvent(msg.getSrc(), "Failed to find valid AuthHeader in Message")); 130 } 131 }else{ 132 if(log.isDebugEnabled()){ 133 log.debug("No AUTH Header Found"); 134 } 135 down_prot.down(createFailureEvent(msg.getSrc(), "Failed to find an AuthHeader in Message")); 137 } 138 }else{ 139 if(log.isDebugEnabled()){ 141 log.debug("Message not a JOIN_REQ - ignoring it"); 142 } 143 return up_prot.up(evt); 144 } 145 return null; 146 } 147 148 156 public Object down(Event evt) { 157 GMS.GmsHeader hdr = isJoinMessage(evt); 158 if((hdr != null) && (hdr.getType() == GMS.GmsHeader.JOIN_REQ)){ 159 if(log.isDebugEnabled()){ 160 log.debug("AUTH got down event"); 161 } 162 Message msg = (Message)evt.getArg(); 164 AuthHeader authHeader = new AuthHeader(); 165 authHeader.setToken(this.serverSideToken); 166 msg.putHeader(AUTH.NAME, authHeader); 167 168 if(log.isDebugEnabled()){ 169 log.debug("AUTH passing down event"); 170 } 171 } 172 173 if((hdr != null) && (hdr.getType() == GMS.GmsHeader.JOIN_RSP)){ 174 if(log.isDebugEnabled()){ 175 log.debug(hdr.toString()); 176 } 177 } 178 179 return down_prot.down(evt); 180 } 181 182 187 private static GMS.GmsHeader isJoinMessage(Event evt){ 188 Message msg; 189 switch(evt.getType()){ 190 case Event.MSG: 191 msg = (Message)evt.getArg(); 192 Object obj = msg.getHeader("GMS"); 193 if(obj == null || !(obj instanceof GMS.GmsHeader)){ 194 return null; 195 } 196 return (GMS.GmsHeader)obj; 197 } 198 return null; 199 } 200 } 201 | Popular Tags |