KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > qsadmin > Authenticator


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
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, String JavaDoc 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     /**
68      * This function is used to validate username and password.
69      * May be overridden to change username and/or password.
70      */

71     protected static boolean validate(String JavaDoc username, byte[] password) {
72         return username.equals("Admin") &&
73             Arrays.equals(password,"QsAdm1n".getBytes());
74     }
75     
76 }
77
Popular Tags