1 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 ; 21 22 import org.apache.xpath.XPathAPI; 23 24 import org.mmbase.util.logging.Logger; 25 import org.mmbase.util.logging.Logging; 26 27 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 41 public static final String PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_0 = "-//MMBase//DTD security context config 1.0//EN"; 42 public static final String PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_1 = "-//MMBase//DTD security context config 1.1//EN"; 43 public static final String PUBLIC_ID_SECURITY_CONTEXT_CONFIG_1_2 = "-//MMBase//DTD security context config 1.2//EN"; 44 45 46 public static final String DTD_SECURITY_CONTEXT_CONFIG_1_0 = "securitycontextconfig_1_0.dtd"; 47 public static final String DTD_SECURITY_CONTEXT_CONFIG_1_1 = "securitycontextconfig_1_1.dtd"; 48 public static final String 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 in = MMBaseCopConfig.securityLoader.getInputSource(configResource); 66 document = org.mmbase.util.XMLBasicReader.getDocumentBuilder(this.getClass()).parse(in); 67 } catch(org.xml.sax.SAXException se) { 68 log.error("error parsing file :"+configResource); 69 String message = "error loading configfile :'" + configResource + "'("+se + "->"+se.getMessage()+"("+se.getMessage()+"))"; 70 log.error(message); 71 log.error(Logging.stackTrace(se)); 72 throw new SecurityException (message); 73 } catch(java.io.IOException ioe) { 74 log.error("error parsing file :"+configResource); 75 log.error(Logging.stackTrace(ioe)); 76 throw new SecurityException ("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 String 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 te) { 90 log.error("error executing query: '" + xpath + "' "); 91 log.error( Logging.stackTrace(te)); 92 throw new SecurityException ("error executing query: '"+xpath+"' "); 93 } 94 for(Node contains = found.nextNode(); contains != null; contains = found.nextNode()) { 96 NamedNodeMap nnm = contains.getAttributes(); 97 String moduleName = nnm.getNamedItem("name").getNodeValue(); 98 String 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 moduleClass = Class.forName(className); 104 module = (ContextLoginModule) moduleClass.newInstance(); 105 } catch(Exception e) { 106 String 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 (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 moduleName, Map loginInfo, Object [] parameters) throws SecurityException { 128 if(!loginModules.containsKey(moduleName)) { 130 throw new UnknownAuthenticationMethodException("could not load module with name: '" + moduleName + "'"); 131 } 132 ContextLoginModule module = (ContextLoginModule) loginModules.get(moduleName); 133 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 150 public boolean isValid(UserContext userContext) throws SecurityException { 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 [] getTypes() { 157 return (String []) loginModules.keySet().toArray(new String [] {}); 158 } 159 } 160 | Popular Tags |