KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > implementation > context > ContextAuthentication


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.context;
11
12 import org.mmbase.security.*;
13 import org.mmbase.security.SecurityException;
14
15 import java.util.*;
16
17 import org.w3c.dom.*;
18 import org.w3c.dom.traversal.NodeIterator;
19
20 import org.xml.sax.InputSource JavaDoc;
21
22 import org.apache.xpath.XPathAPI;
23
24 import org.mmbase.util.logging.Logger;
25 import org.mmbase.util.logging.Logging;
26
27 /**
28  * Authentication based on a XML-configuration file. The XML file contains besides users, groups and
29  * contexts (used for ContextAuthorization).
30  *
31  * @author Eduard Witteveen
32  * @version $Id: ContextAuthentication.java,v 1.22 2006/01/17 21:25:28 michiel Exp $
33  * @see ContextAuthorization
34  */

35 public class ContextAuthentication extends Authentication {
36     private static final Logger log = Logging.getLoggerInstance(ContextAuthentication.class);
37     private Map loginModules = new LinkedHashMap();
38     private Document document;
39
40     /** Public ID of the Builder DTD version 1.0 */
41     public static final String JavaDoc PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_0 = "-//MMBase//DTD security context config 1.0//EN";
42     public static final String JavaDoc PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_1 = "-//MMBase//DTD security context config 1.1//EN";
43     public static final String JavaDoc PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_2 = "-//MMBase//DTD security context config 1.2//EN";
44
45     /** DTD resource filename of the Builder DTD version 1.0 */
46     public static final String JavaDoc DTD_SECURITY_CONTEXT_CONFIG_1_0 = "securitycontextconfig_1_0.dtd";
47     public static final String JavaDoc DTD_SECURITY_CONTEXT_CONFIG_1_1 = "securitycontextconfig_1_1.dtd";
48     public static final String JavaDoc DTD_SECURITY_CONTEXT_CONFIG_1_2 = "securitycontextconfig_1_2.dtd";
49
50     static {
51         org.mmbase.util.XMLEntityResolver.registerPublicID(PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_0, DTD_SECURITY_CONTEXT_CONFIG_1_0, MMBaseCopConfig.class);
52         org.mmbase.util.XMLEntityResolver.registerPublicID(PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_1, DTD_SECURITY_CONTEXT_CONFIG_1_1, MMBaseCopConfig.class);
53         org.mmbase.util.XMLEntityResolver.registerPublicID(PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_2, DTD_SECURITY_CONTEXT_CONFIG_1_2, MMBaseCopConfig.class);
54     }
55
56     public ContextAuthentication() {
57     }
58
59     protected void load() {
60         if (log.isDebugEnabled()) {
61             log.debug("using: '" + configResource + "' as config file for context-authentication");
62         }
63
64         try {
65             InputSource JavaDoc in = MMBaseCopConfig.securityLoader.getInputSource(configResource);
66             document = org.mmbase.util.XMLBasicReader.getDocumentBuilder(this.getClass()).parse(in);
67         } catch(org.xml.sax.SAXException JavaDoc se) {
68             log.error("error parsing file :"+configResource);
69             String JavaDoc message = "error loading configfile :'" + configResource + "'("+se + "->"+se.getMessage()+"("+se.getMessage()+"))";
70             log.error(message);
71             log.error(Logging.stackTrace(se));
72             throw new SecurityException JavaDoc(message);
73         } catch(java.io.IOException JavaDoc ioe) {
74             log.error("error parsing file :"+configResource);
75             log.error(Logging.stackTrace(ioe));
76             throw new SecurityException JavaDoc("error loading configfile :'"+configResource+"'("+ioe+")" );
77         }
78         if (log.isDebugEnabled()) {
79             log.debug("loaded: '" + configResource + "' as config file for authentication");
80             log.debug("going to load the modules...");
81         }
82
83         // do the xpath query...
84
String JavaDoc xpath = "/contextconfig/loginmodules/module";
85         if (log.isDebugEnabled()) log.debug("going to execute the query:" + xpath );
86         NodeIterator found;
87         try {
88             found = XPathAPI.selectNodeIterator(document, xpath);
89         } catch(javax.xml.transform.TransformerException JavaDoc te) {
90             log.error("error executing query: '" + xpath + "' ");
91             log.error( Logging.stackTrace(te));
92             throw new SecurityException JavaDoc("error executing query: '"+xpath+"' ");
93         }
94         // we now have a list of login modules.. process them all, and load them...
95
for(Node contains = found.nextNode(); contains != null; contains = found.nextNode()) {
96             NamedNodeMap nnm = contains.getAttributes();
97             String JavaDoc moduleName = nnm.getNamedItem("name").getNodeValue();
98             String JavaDoc className = nnm.getNamedItem("class").getNodeValue();
99
100             log.debug("going to try to load module with the name '" + moduleName + "' with class: " + className);
101             ContextLoginModule module;
102             try {
103                 Class JavaDoc moduleClass = Class.forName(className);
104                 module = (ContextLoginModule) moduleClass.newInstance();
105             } catch(Exception JavaDoc e) {
106                 String JavaDoc msg = "could not load module with the name: '" + moduleName + "' with class: " + className;
107                 log.error(msg);
108                 log.error( Logging.stackTrace(e));
109                 throw new SecurityException JavaDoc(msg);
110             }
111             module.load(document, getKey(), moduleName, manager);
112             log.info("loaded module with the name: '" + moduleName + "' with class: " + className);
113             loginModules.put(moduleName, module);
114         }
115
116         if (!loginModules.containsKey("class")) {
117             ContextLoginModule classModule = new ClassLogin();
118             log.info("The class login module was not configured. It is needed sometimes. Now loading module with the name 'class' with class: " + classModule.getClass());
119             classModule.load(document, getKey(), "class", manager);
120             loginModules.put("class", classModule);
121         }
122
123         log.debug("done loading the modules...");
124     }
125
126
127     public UserContext login(String JavaDoc moduleName, Map loginInfo, Object JavaDoc[] parameters) throws SecurityException JavaDoc {
128         // look if we can find our login module...
129
if(!loginModules.containsKey(moduleName)) {
130             throw new UnknownAuthenticationMethodException("could not load module with name: '" + moduleName + "'");
131         }
132         ContextLoginModule module = (ContextLoginModule) loginModules.get(moduleName);
133         // and we do the login...
134
UserContext user = module.login(loginInfo, parameters);
135         if (log.isServiceEnabled()) {
136             if(user == null) {
137                 log.debug("login on module with name '" + moduleName + "' failed");
138             } else {
139                 if (user.getRank().getInt() > Rank.ANONYMOUS_INT) {
140                     log.debug("login on module with name '" + moduleName + "' was succesfull for user with id: '" + user.getIdentifier() + "'");
141                 }
142             }
143         }
144         return user;
145     }
146
147     /**
148      * this method does nothing..
149      */

150     public boolean isValid(UserContext userContext) throws SecurityException JavaDoc {
151         if ( getKey() == ((ContextUserContext)userContext).getKey()) return true;
152         log.debug("not valid because " + getKey () + " != " + ((ContextUserContext) userContext).getKey());
153         return false;
154     }
155
156     public String JavaDoc[] getTypes() {
157         return (String JavaDoc[]) loginModules.keySet().toArray(new String JavaDoc[] {});
158     }
159 }
160
Popular Tags