01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
10: * For questions, suggestions, bug-reports, enhancement-requests etc.
11: * visit http://www.quickserver.org
12: *
13: */
14:
15: package echoserver;
16:
17: import org.quickserver.net.server.*;
18: import java.io.*;
19: import java.util.*;
20: import org.quickserver.net.AppException;
21:
22: /**
23: * Needs ClientData
24: */
25: public class EchoAuthenticatorHandler extends
26: QuickAuthenticationHandler {
27: public AuthStatus askAuthentication(ClientHandler handler)
28: throws IOException, AppException {
29: Data data = (Data) handler.getClientData();
30: data.setLastAsked("U");
31: handler.sendClientMsg("User Name :");
32: return null;//no AuthStatus yet
33: }
34:
35: public AuthStatus handleAuthentication(ClientHandler handler,
36: String command) throws IOException, AppException {
37: Data data = (Data) handler.getClientData();
38:
39: if (data.getLastAsked().equals("U")) {
40: data.setUsername(command);
41: data.setLastAsked("P");
42: handler.sendClientMsg("Password :");
43: } else if (data.getLastAsked().equals("P")) {
44: data.setPassword(command.getBytes());
45:
46: if (validate(handler, data.getUsername(), data
47: .getPassword())) {
48: handler.sendClientMsg("Auth OK");
49: data.setPassword(null);
50: return AuthStatus.SUCCESS;
51: } else {
52: handler.sendClientMsg("Auth Failed");
53: data.setPassword(null);
54: return AuthStatus.FAILURE;
55: }
56: } else {
57: throw new AppException("Unknown LastAsked!");
58: }
59:
60: return null;//no AuthStatus yet
61: }
62:
63: /**
64: * This function is used to validate username and password.
65: * May be overridden to change username and/or password.
66: */
67: protected static boolean validate(ClientHandler handler,
68: String username, byte[] password) {
69: return Arrays.equals(password, username.getBytes());
70: }
71: }
|