KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > implementation > cloud > Authenticate


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.security.implementation.cloud;
11
12 import org.mmbase.security.Rank;
13 import org.mmbase.security.UserContext;
14 import org.mmbase.security.BasicUser;
15 import org.mmbase.security.Authentication;
16
17 import java.util.Map JavaDoc;
18
19 import org.mmbase.util.logging.Logger;
20 import org.mmbase.util.logging.Logging;
21
22 /**
23  * Very simply security from within MMBase. You might want to look at Cloud Context Security which offers a much more powerfull implementation.
24  *
25  * @author Eduard Witteveen
26  * @author Michiel Meeuwissen
27  * @version $Id: Authenticate.java,v 1.11 2006/05/16 18:07:20 michiel Exp $
28  */

29
30 public class Authenticate extends Authentication {
31     private static final Logger log=Logging.getLoggerInstance(Authenticate.class);
32     private long validKey;
33     private static UserBuilder builder = null;
34
35     public Authenticate() {
36         validKey = System.currentTimeMillis();
37     }
38
39     /**
40      * {@inheritDoc}
41      * Implementation is empty here.
42      */

43     protected void load() {
44         //log.debug("using: '" + configFile + "' as config file for authentication");
45
}
46
47     /**
48      * Gets rank from user, whici sin
49      * @since MMBase-1.8
50      */

51     protected Rank getRank(String JavaDoc userName) {
52         if(userName.equals("admin")) {
53             log.debug("[admin login]");
54             return Rank.ADMIN;
55         } else {
56             return Rank.BASICUSER;
57         }
58     }
59
60     /**
61      * {@inheritDoc}
62      */

63     public UserContext login(String JavaDoc moduleName, Map JavaDoc loginInfo, Object JavaDoc[] parameters) throws org.mmbase.security.SecurityException {
64         if (log.isTraceEnabled()) {
65             log.trace("login-module: '" + moduleName + "'");
66         }
67         if("anonymous".equals(moduleName)) {
68             log.debug("[anonymous login]");
69             return new User("anonymous", Rank.ANONYMOUS, validKey, "anonymous");
70         } else if("name/password".equals(moduleName)) {
71             // look if the builder is good and such things....
72
checkBuilder();
73             if(builder==null) {
74                 throw new org.mmbase.security.SecurityException("builder wasnt loaded");
75             }
76             String JavaDoc userName = (String JavaDoc)loginInfo.get("username");
77             String JavaDoc password = (String JavaDoc)loginInfo.get("password");
78             log.trace("login-module: '" +moduleName + "' username: '" + userName + "' password: '" + password + "'");
79             if(userName == null) throw new org.mmbase.security.SecurityException("expected the property 'username' with login");
80             if(userName.equals("anonymous")) throw new org.mmbase.security.SecurityException("'anonymous' is not allowed to do a login");
81             if(password == null) throw new org.mmbase.security.SecurityException("expected the property 'password' with login");
82
83             if(builder.exists(userName, password)) {
84                 return new User(userName, getRank(userName), validKey, "name/password");
85             } else {
86                 // helaas, pindakaas...
87
return null;
88             }
89         } else if ("class".equals(moduleName)) {
90             org.mmbase.security.classsecurity.ClassAuthentication.Login li = org.mmbase.security.classsecurity.ClassAuthentication.classCheck("class");
91             if (li == null) {
92                 throw new SecurityException JavaDoc("Class authentication failed (class not authorized)");
93             }
94             String JavaDoc userName = (String JavaDoc) li.getMap().get("username");
95             if (userName == null && "administrator".equals(li.getMap().get("rank"))) userName = "admin";
96             if (userName == null) throw new org.mmbase.security.SecurityException("expected the property 'username' with login");
97             if (userName.equals("admin") || builder.exists(userName, null)) {
98                 return new User(userName, getRank(userName), validKey, "class");
99             } else {
100                 return null;
101             }
102
103         } else {
104             throw new org.mmbase.security.UnknownAuthenticationMethodException("login module with name '" + moduleName + "' not found, only know 'anonymous' and 'name/password' ");
105         }
106     }
107
108     /**
109      * {@inheritDoc}
110      */

111     public boolean isValid(UserContext usercontext) throws org.mmbase.security.SecurityException {
112         log.debug(usercontext);
113         return ((User)usercontext).getKey() == validKey;
114     }
115     
116     private void checkBuilder() throws org.mmbase.security.SecurityException {
117         if(builder == null) {
118             org.mmbase.module.core.MMBase mmb = (org.mmbase.module.core.MMBase) org.mmbase.module.Module.getModule("mmbaseroot");
119             builder = (UserBuilder) mmb.getMMObject("mmbaseusers");
120             if(builder == null) {
121                 String JavaDoc msg = "builder mmbaseusers not found";
122                 log.error(msg);
123                 throw new org.mmbase.security.SecurityException(msg);
124             } // optional check if the user admin does exist...
125
}
126     }
127
128
129     /**
130      * The user object for 'cloud' security.
131      */

132     private static class User extends BasicUser {
133         private String JavaDoc user;
134         private Rank rank;
135         private long key;
136         
137         User(String JavaDoc user, Rank rank, long key, String JavaDoc app) {
138             super(app);
139             this.user = user;
140             this.rank = rank;
141             this.key = key;
142         }
143         
144         public String JavaDoc getIdentifier() {
145             return user;
146         }
147         
148         public Rank getRank() throws org.mmbase.security.SecurityException {
149             return rank;
150         }
151         
152         public String JavaDoc toString() {
153             return user + "[" + rank + "]";
154         }
155         
156         long getKey() {
157             return key;
158         }
159     }
160
161 }
162
163
164
Popular Tags