1 19 20 package org.lucane.server.auth; 21 22 import java.util.StringTokenizer ; 23 24 import org.lucane.common.Logging; 25 import org.lucane.common.Message; 26 import org.lucane.common.net.ObjectConnection; 27 import org.lucane.server.Server; 28 import org.lucane.server.ServerConfig; 29 30 public abstract class Authenticator 31 { 32 public static Authenticator newInstance(ServerConfig config) 33 throws ClassNotFoundException , InstantiationException , IllegalAccessException 34 { 35 Logging.getLogger().info("Authenticator used : " + config.getAuthenticatorClass()); 36 37 Class authClass = Class.forName(config.getAuthenticatorClass()); 38 return (Authenticator)authClass.newInstance(); 39 } 40 41 public abstract AuthResponse authenticate(AuthRequest request); 42 43 private boolean loginDisabled = false; 44 45 public void disableLogin() 46 { 47 this.loginDisabled = true; 48 Logging.getLogger().info("Login disabled"); 49 } 50 51 public void authenticate(ObjectConnection oc, Message message, String data) 53 { 54 if(loginDisabled) 56 { 57 try { 58 oc.write("ACCESS_DENIED"); 59 } catch(Exception e) {} 60 return; 61 } 62 63 String passwd = ""; 64 StringTokenizer stk = new StringTokenizer (data, " "); 65 try { 66 passwd = stk.nextToken(); 67 } catch (Exception ex) {} 68 69 AuthRequest request = new AuthRequest(message.getSender(), passwd); 71 AuthResponse response = authenticate(request); 72 73 if(response.getValue() == AuthResponse.AUTH_ACCEPTED) 75 { 76 try { 77 oc.write("AUTH_ACCEPTED"); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 } 81 82 Server.getInstance().sendUserListToEveryone(); 84 } 85 86 else if(response.getValue() == AuthResponse.BAD_CREDENTIALS) 88 { 89 try { 90 oc.write("NOT_VALID_USER"); 91 } catch(Exception e) {} 92 } 93 94 else 96 { 97 try { 98 oc.write("ACCESS_DENIED"); 99 } catch(Exception e) {} 100 } 101 102 } 103 } | Popular Tags |