001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package chatserver;
016:
017: import org.quickserver.net.server.*;
018: import java.io.*;
019: import java.util.*;
020: import org.quickserver.net.*;
021:
022: /**
023: *
024: * @author Akshathkumar Shetty
025: */
026: public class ChatAuthenticator extends QuickAuthenticationHandler {
027: public AuthStatus askAuthentication(ClientHandler handler)
028: throws IOException, AppException {
029: ChatData data = (ChatData) handler.getClientData();
030: data.setLastAsked("U");
031: handler.sendClientMsg("{system.data} Username");
032: return null;
033: }
034:
035: public AuthStatus handleAuthentication(ClientHandler handler,
036: String command) throws IOException, AppException {
037: ChatData data = (ChatData) handler.getClientData();
038:
039: if (data.getLastAsked().equals("U")) {
040: data.setUsername(command);
041: data.setLastAsked("P");
042: handler.sendClientMsg("{system.data} Password");
043: return null;
044: }
045:
046: if (data.getLastAsked().equals("P")) {
047: data.setPassword(command.getBytes());
048: data.setLastAsked("R");
049: handler.sendClientMsg("{system.data} Room");
050: return null;
051: }
052:
053: if (data.getLastAsked().equals("R")) {
054: if (data.registerUsername(data.getUsername()) == false) {
055: handler
056: .sendClientMsg("{system.error} AuthFailed. Username taken!");
057: data.setUsername(null);
058: return AuthStatus.FAILURE;
059: }
060:
061: if (validate(handler, data.getUsername(), data
062: .getPassword())) {
063: data.setRoom(command);
064: handler.sendClientMsg("{system.ok} Auth Ok");
065: handler
066: .sendClientMsg("{system.msg} Current Chat Room: "
067: + data.getRoom());
068: data.setPassword(null);
069:
070: ChatMessaging.sendInfoMessage2Room(handler, data
071: .getRoom(), "LoggedIn");
072: ChatMessaging.printHelp(handler, null);
073:
074: return AuthStatus.SUCCESS;
075: } else {
076: handler.sendClientMsg("{system.error} AuthFailed");
077: data.deregisterUsername(data.getUsername());
078: data.setPassword(null);
079: return AuthStatus.FAILURE;
080: }
081: } else {
082: throw new AppException("Unknown LastAsked!");
083: }
084: }
085:
086: /**
087: * This function is used to validate username and password.
088: * May be overridden to change username and/or password.
089: */
090: protected static boolean validate(ClientHandler handler,
091: String username, byte[] password) {
092: return Arrays.equals(password, username.getBytes());
093: }
094:
095: /*
096: //simpler edition of ChatAuthenticator, that would extend QuickAuthenticator
097: public boolean askAuthorisation(ClientHandler handler)
098: throws IOException {
099: String username = askStringInput(handler, "{system.data} Username");
100: String password = askStringInput(handler, "{system.data} Password");
101: String room = askStringInput(handler, "{system.data} Room");
102:
103: //no need to check username or username for null : done by QuickAuthenticator
104:
105: if(username.equals(password)) {
106: ChatData cd = (ChatData)handler.getClientData();
107: try {
108: cd.setUsername(username);
109: } catch(IllegalArgumentException iae) {
110: sendString(handler, "{system.error} AuthFailed. "+iae.getMessage());
111: return false;
112: }
113:
114: if(room.length()==0) room = "home";
115:
116: cd.setRoom(room); //"home"
117:
118: sendString(handler, "{system.ok} Auth Ok");
119: sendString(handler, "{system.msg} Current Chat Room: "+cd.getRoom());
120:
121: ChatMessaging.sendInfoMessage2Room(handler, room, "LoggedIn");
122:
123: ChatMessaging.printHelp(handler, null);
124:
125: return true;
126: } else {
127: sendString(handler, "{system.error} AuthFailed");
128: return false;
129: }
130: }
131: */
132: }
|