KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.lucane.common.*;
23 import org.lucane.common.concepts.UserConcept;
24 import org.lucane.common.net.ObjectConnection;
25 import org.lucane.server.ConnectInfoManager;
26 import org.lucane.server.Server;
27 import org.lucane.server.store.Store;
28
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32 public class DefaultAuthenticator extends Authenticator
33 {
34     public AuthResponse authenticate(AuthRequest request)
35     {
36         Store store = Server.getInstance().getStore();
37         ConnectInfo userInfo = request.getUserInfo();
38         
39         //get the user concept
40
UserConcept user = null;
41         try {
42             user = store.getUserStore().getUser(userInfo.getName());
43         } catch(Exception JavaDoc e) {
44             e.printStackTrace();
45         }
46         
47         if(user == null)
48             return new AuthResponse(AuthResponse.BAD_CREDENTIALS);
49         
50         if(user.isLocked())
51             return new AuthResponse(AuthResponse.USER_LOCKED);
52         
53         if(!store.getUserStore().checkUserPassword(user, request.getMd5Passwd()))
54             return new AuthResponse(AuthResponse.BAD_CREDENTIALS);
55         
56         //disconnect already connected user
57
if(userInfo.isClient() && ConnectInfoManager.getInstance().isConnected(userInfo))
58         {
59             ConnectInfo oldUser = ConnectInfoManager.getInstance().
60                 getCompleteConnectInfo(request.getUserInfo());
61             
62             try {
63                 Map JavaDoc action = new HashMap JavaDoc();
64                 action.put("command", "DISCONNECT");
65                 ObjectConnection oc = Server.getInstance().sendMessageTo(oldUser, "Client", action);
66                 oc.close();
67             } catch (Exception JavaDoc e) {
68                 //we can't do much here, the client might have crashed
69
}
70             ConnectInfoManager.getInstance().removeConnectInfo(oldUser);
71         }
72         
73         //add the connect info
74
ConnectInfoManager.getInstance().addConnectInfo(userInfo);
75         
76         return new AuthResponse(AuthResponse.AUTH_ACCEPTED);
77     }
78 }
Popular Tags