KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > auth > Authenticator


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.server.auth;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.lucane.common.Logging;
25 import org.lucane.common.Message;
26 import org.lucane.common.net.ObjectConnection;
27 import org.lucane.server.Server;
28 import org.lucane.server.ServerConfig;
29
30 public abstract class Authenticator
31 {
32     public static Authenticator newInstance(ServerConfig config)
33     throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
34     {
35         Logging.getLogger().info("Authenticator used : " + config.getAuthenticatorClass());
36         
37         Class JavaDoc authClass = Class.forName(config.getAuthenticatorClass());
38         return (Authenticator)authClass.newInstance();
39     }
40     
41     public abstract AuthResponse authenticate(AuthRequest request);
42     
43     private boolean loginDisabled = false;
44     
45     public void disableLogin()
46     {
47         this.loginDisabled = true;
48         Logging.getLogger().info("Login disabled");
49     }
50     
51     // for compatibility with the current implementation
52
public void authenticate(ObjectConnection oc, Message message, String JavaDoc data)
53     {
54         // check is login is still allowed
55
if(loginDisabled)
56         {
57             try {
58                 oc.write("ACCESS_DENIED");
59             } catch(Exception JavaDoc e) {}
60             return;
61         }
62         
63         String JavaDoc passwd = "";
64         StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(data, " ");
65         try {
66             passwd = stk.nextToken();
67         } catch (Exception JavaDoc ex) {}
68         
69         //call the new api
70
AuthRequest request = new AuthRequest(message.getSender(), passwd);
71         AuthResponse response = authenticate(request);
72         
73         //accepted
74
if(response.getValue() == AuthResponse.AUTH_ACCEPTED)
75         {
76             try {
77                 oc.write("AUTH_ACCEPTED");
78             } catch (Exception JavaDoc e) {
79                 e.printStackTrace();
80             }
81             
82             //send the user list to everyone
83
Server.getInstance().sendUserListToEveryone();
84         }
85         
86         //bad user or password
87
else if(response.getValue() == AuthResponse.BAD_CREDENTIALS)
88         {
89             try {
90                 oc.write("NOT_VALID_USER");
91             } catch(Exception JavaDoc e) {}
92         }
93         
94         //login disabled
95
else
96         {
97             try {
98                 oc.write("ACCESS_DENIED");
99             } catch(Exception JavaDoc e) {}
100         }
101         
102     }
103 }
Popular Tags