KickJava   Java API By Example, From Geeks To Geeks.

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


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.GroupConcept;
24 import org.lucane.common.concepts.UserConcept;
25 import org.lucane.common.net.ObjectConnection;
26 import org.lucane.server.ConnectInfoManager;
27 import org.lucane.server.Server;
28 import org.lucane.server.store.Store;
29
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32
33 /**
34  * This authenticator logs users as the default one,
35  * but also create accounts for non existing users trying to log in.
36  */

37 public class DemoAuthenticator extends Authenticator
38 {
39     public AuthResponse authenticate(AuthRequest request)
40     {
41         Store store = Server.getInstance().getStore();
42         ConnectInfo userInfo = request.getUserInfo();
43         
44         //get the user concept
45
UserConcept user = null;
46         try {
47             user = store.getUserStore().getUser(userInfo.getName());
48         } catch(Exception JavaDoc e) {
49             e.printStackTrace();
50         }
51         
52         //user does not exist yet
53
if(user == null)
54         {
55             try {
56                 user = new UserConcept(userInfo.getName(), request.getMd5Passwd(),
57                         userInfo.getName(), "", "en", false, "org.lucane.applications.quicklaunch");
58                 store.getUserStore().storeUser(user);
59                 GroupConcept allUsers = store.getGroupStore().getGroup("AllUsers");
60                 allUsers.addUser(user);
61                 store.getGroupStore().updateGroup(allUsers);
62                 Logging.getLogger().info("Created user '" + userInfo.getName() + "'.");
63             } catch(Exception JavaDoc e) {
64                 Logging.getLogger().warning("Unable to create user '" + userInfo.getName() + "' : " + e);
65                 return new AuthResponse(AuthResponse.LOGIN_DISABLED);
66             }
67         }
68         
69         if(user.isLocked())
70             return new AuthResponse(AuthResponse.USER_LOCKED);
71         
72         if(!store.getUserStore().checkUserPassword(user, request.getMd5Passwd()))
73             return new AuthResponse(AuthResponse.BAD_CREDENTIALS);
74         
75         //disconnect already connected user
76
if(userInfo.isClient() && ConnectInfoManager.getInstance().isConnected(userInfo))
77         {
78             ConnectInfo oldUser = ConnectInfoManager.getInstance().
79                 getCompleteConnectInfo(request.getUserInfo());
80             
81             try {
82                 Map JavaDoc action = new HashMap JavaDoc();
83                 action.put("command", "DISCONNECT");
84                 ObjectConnection oc = Server.getInstance().sendMessageTo(oldUser, "Client", action);
85                 oc.close();
86             } catch (Exception JavaDoc e) {
87                 //we can't do much here, the client might have crashed
88
}
89             ConnectInfoManager.getInstance().removeConnectInfo(oldUser);
90         }
91         
92         //add the connect info
93
ConnectInfoManager.getInstance().addConnectInfo(userInfo);
94         
95         return new AuthResponse(AuthResponse.AUTH_ACCEPTED);
96     }
97 }
Popular Tags