01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 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 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: /**
23: * Default QSAdminServer ServerAuthenticator.
24: * <p>
25: * Username : Admin<br>
26: * Password : QsAdm1n
27: * </p>
28: * @since 1.1
29: */
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,
41: String command) 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
52: .getPassword())) {
53: handler.sendClientMsg("+OK Logged in");
54: data.setPassword(null);
55: return AuthStatus.SUCCESS;
56: } else {
57: handler.sendClientMsg("-ERR Authorisation Failed");
58: data.setPassword(null);
59: return AuthStatus.FAILURE;
60: }
61: } else {
62: throw new AppException("Unknown LastAsked!");
63: }
64:
65: return null;
66: }
67:
68: /**
69: * This function is used to validate username and password.
70: * May be overridden to change username and/or password.
71: */
72: protected static boolean validate(String username, byte[] password) {
73: return username.equals("Admin")
74: && Arrays.equals(password, "QsAdm1n".getBytes());
75: }
76:
77: }
|