1 14 15 package org.quickserver.net.qsadmin; 16 17 import org.quickserver.net.server.*; 18 import org.quickserver.net.AppException; 19 import java.io.*; 20 import java.util.*; 21 22 30 public class Authenticator extends QuickAuthenticationHandler { 31 32 public AuthStatus askAuthentication(ClientHandler handler) 33 throws IOException, AppException { 34 Data data = (Data) handler.getClientData(); 35 data.setLastAsked("U"); 36 handler.sendClientMsg("+OK Username required"); 37 return null; 38 } 39 40 public AuthStatus handleAuthentication(ClientHandler handler, String command) 41 throws IOException, AppException { 42 Data data = (Data)handler.getClientData(); 43 44 if(data.getLastAsked().equals("U")) { 45 data.setUsername(command); 46 data.setLastAsked("P"); 47 handler.sendClientMsg("+OK Password required"); 48 } else if(data.getLastAsked().equals("P")) { 49 data.setPassword(command.getBytes()); 50 51 if(Authenticator.validate(data.getUsername(), data.getPassword())) { 52 handler.sendClientMsg("+OK Logged in"); 53 data.setPassword(null); 54 return AuthStatus.SUCCESS; 55 } else { 56 handler.sendClientMsg("-ERR Authorisation Failed"); 57 data.setPassword(null); 58 return AuthStatus.FAILURE; 59 } 60 } else { 61 throw new AppException("Unknown LastAsked!"); 62 } 63 64 return null; 65 } 66 67 71 protected static boolean validate(String username, byte[] password) { 72 return username.equals("Admin") && 73 Arrays.equals(password,"QsAdm1n".getBytes()); 74 } 75 76 } 77 | Popular Tags |