1 18 package freecs.auth; 19 20 import java.net.MalformedURLException ; 21 import java.sql.Timestamp ; 22 import java.util.Date ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Properties ; 27 import java.util.Vector ; 28 29 import org.apache.xmlrpc.XmlRpcClient; 30 import org.apache.xmlrpc.XmlRpcException; 31 32 import freecs.Server; 33 import freecs.core.User; 34 import freecs.interfaces.IRequest; 35 36 39 public class XmlRpcAuthenticator extends AbstractAuthenticator { 40 41 XmlRpcClient xmlrpc = null; 42 43 public XmlRpcAuthenticator() { 44 } 45 46 47 public void init(Properties allProps, String additionalPrefix) { 48 super.init(allProps, additionalPrefix); 49 Server.log (this, "parsing config", Server.MSG_STATE, Server.LVL_MINOR); 50 synchronized (this) { 51 try { 52 xmlrpc = new XmlRpcClient(props.getProperty("server")); 53 } catch (MalformedURLException wrongurl) { 54 Server.log(this, "can't construct xmlrpc-client because of wrong url: " + wrongurl.toString(), Server.MSG_CONFIG, Server.LVL_MAJOR); 55 } 56 } 57 } 58 59 62 public void shutdown() throws Exception { 63 xmlrpc = null; 64 } 65 66 70 public User loginUser(String usr, String pwd, String userCookie, IRequest request) 71 throws Exception { 72 73 String cookie = props.getProperty("cookie"); 75 String checkCookie = (cookie != null && !cookie.trim().equals("")) ? request.getProperty("c_" + cookie) : userCookie; 76 77 pwd = (pwd == null) ? "" : pwd; 80 checkCookie = (checkCookie == null) ? "" : checkCookie; 81 82 Vector params = new Vector (); 83 if ("session".equalsIgnoreCase(props.getProperty("check"))) { 84 params.add(checkCookie); 85 } else { 86 params.add(usr); 87 params.add(pwd); 88 } 89 Object result = null; 90 try { 91 result = xmlrpc.execute(props.getProperty("loginMethod"), params); 92 } catch (XmlRpcException xmlrpcEx) { 93 Server.debug (this, props.getProperty("server") + "/" + props.getProperty("loginMethod") + " reports an xmlrpc-error: ", xmlrpcEx, Server.MSG_AUTH, Server.LVL_MAJOR); 95 return null; 96 } catch (Exception ex) { 97 Server.debug(this, "xmlrpc-request to " + props.getProperty("server") + "/" + props.getProperty("loginMethod") + " failed: ", ex, Server.MSG_AUTH, Server.LVL_MAJOR); 99 return null; 100 } 101 102 if (result == null || result == Boolean.FALSE) { 103 return null; 105 } 106 107 if (!(result instanceof Hashtable )) { 108 if (!"1".equals(result) 109 && !"true".equals(result) 110 && !Boolean.TRUE.equals(result)) 111 return null; 112 return new User (usr, userCookie); 113 } 114 Hashtable userdata = (Hashtable ) result; 115 116 if (userdata.containsKey("errorcode")) { 117 int errorCode = ((Integer )userdata.get("errorcode")).intValue(); 119 if (errorCode>0) { 120 Server.log(this, "login failed as " + props.getProperty("server") + " returned code " + errorCode, Server.MSG_AUTH, Server.LVL_VERBOSE); 121 return null; 122 } 123 } 124 String remoteUsername = (String ) userdata.get("username"); 125 if (!usr.equalsIgnoreCase(remoteUsername)) { 126 return null; 128 } 129 130 User u = new User (usr, userCookie); 131 u.isUnregistered = false; 132 133 if (userdata.containsKey("color")) { 134 u.setColCode((String )userdata.get("color")); 135 } 136 if (userdata.containsKey("chattime")) { 137 u.setProperty("chattime", Long.valueOf((String ) 138 userdata.get("chattime"))); 139 } 140 if (userdata.containsKey("id")) { 141 Object obj = userdata.get("id"); 142 if (obj instanceof Number ) { 143 u.setID(((Number )obj).toString()); 144 } else if (obj instanceof String ) { 145 u.setID((String )obj); 146 } 147 } 148 if (userdata.containsKey("lastlogin")) { 149 Object obj = userdata.get("lastlogin"); 150 if (obj instanceof Number ) { 151 u.setProperty("lastlogin", new Timestamp (((Number )obj).longValue())); 152 } else if (obj instanceof Date ) { 153 u.setProperty("lastlogin", new Timestamp (((Date )obj).getTime())); 154 } 155 } 156 if (userdata.containsKey("friendslist")) { 157 List users = parseUserList((String )userdata.get("friendslist")); 158 for (Iterator i = users.iterator(); i.hasNext(); ) { 159 u.addFriend((String ) i.next()); 160 } 161 } 162 if (parseBoolean(userdata.get("blocked"))==true) { 163 u.blocked = true; 164 } 165 if (parseBoolean(userdata.get("activated"))==true){ 166 u.activated = true; 167 } 168 return (u); 169 170 } 171 172 public User loginUser(User u, String username, String password, IRequest request) throws Exception { 173 return u; 174 } 175 176 177 180 public void logoutUser(User u) throws Exception { 181 String logoutMethod = props.getProperty("logoutMethod"); 182 if (logoutMethod == null 183 || logoutMethod.length() <1) 184 return; 185 Hashtable userdata = new Hashtable (); 186 userdata.put("username", u.getName()); 187 userdata.put("chattime", new Integer ((new Long (u.getChattime()).intValue())) ); 188 userdata.put("color", u.getColCode()); 189 userdata.put("cookie", u.getCookie()); 190 if (props.containsKey("cookie")) { 191 String cookieName = props.getProperty("cookie"); 192 userdata.put("c_" + cookieName, u.getProperty("c_" + cookieName)); 193 } 194 195 Vector params = new Vector (1); 196 params.add(userdata); 197 try { 198 Object result = xmlrpc.execute(logoutMethod, params); 199 } catch (XmlRpcException xmlrpcEx) { 200 Server.debug(this, props.getProperty("server") + "/" + props.getProperty("logoutMethod") + " reports an xmlrpc-error: ", xmlrpcEx, Server.MSG_AUTH, Server.LVL_MAJOR); 202 } catch (Exception ex) { 203 Server.debug(this, "xmlrpc-request to " + props.getProperty("server") + "/" + props.getProperty("logoutMethod") + " failed: ", ex, Server.MSG_AUTH, Server.LVL_MAJOR); 205 } 206 } 207 208 209 } | Popular Tags |