1 16 package org.apache.cocoon.webapps.authentication.components; 17 18 import java.io.IOException ; 19 20 import org.apache.avalon.framework.activity.Disposable; 21 import org.apache.avalon.framework.logger.AbstractLogEnabled; 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.avalon.framework.service.Serviceable; 25 import org.apache.avalon.framework.thread.ThreadSafe; 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.components.source.SourceUtil; 28 import org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration; 29 import org.apache.cocoon.webapps.authentication.user.UserHandler; 30 import org.apache.cocoon.webapps.session.MediaManager; 31 import org.apache.cocoon.xml.XMLUtils; 32 import org.apache.cocoon.xml.dom.DOMUtil; 33 import org.apache.excalibur.source.Source; 34 import org.apache.excalibur.source.SourceException; 35 import org.apache.excalibur.source.SourceParameters; 36 import org.apache.excalibur.source.SourceResolver; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Node ; 40 import org.w3c.dom.NodeList ; 41 import org.w3c.dom.Text ; 42 import org.xml.sax.SAXException ; 43 44 50 public class PipelineAuthenticator 51 extends AbstractLogEnabled 52 implements Serviceable, ThreadSafe, Disposable, Authenticator { 53 54 55 protected ServiceManager manager; 56 57 58 protected SourceResolver resolver; 59 60 63 private boolean isValidAuthenticationFragment(Document authenticationFragment) 64 throws ProcessingException { 65 if (getLogger().isDebugEnabled() ) { 67 getLogger().debug("BEGIN isValidAuthenticationFragment fragment=" + 68 XMLUtils.serializeNode(authenticationFragment, XMLUtils.createPropertiesForXML(false))); 69 } 70 boolean isValid = false; 71 72 if (authenticationFragment.hasChildNodes() == true 75 && authenticationFragment.getChildNodes().getLength() == 1) { 76 Node child = authenticationFragment.getFirstChild(); 77 78 if (child.getNodeType() == Node.ELEMENT_NODE 79 && child.getNodeName().equals("authentication") == true) { 80 81 if (child.hasChildNodes() == true) { 83 NodeList children = child.getChildNodes(); 84 boolean found = false; 85 int i = 0; 86 int l = children.getLength(); 87 88 while (found == false && i < l) { 89 child = children.item(i); 90 if (child.getNodeType() == Node.ELEMENT_NODE 91 && child.getNodeName().equals("ID") == true) { 92 found = true; 93 } else { 94 i++; 95 } 96 } 97 98 if (found == true) { 100 child.normalize(); if (child.hasChildNodes() == true && 102 child.getChildNodes().getLength() == 1 && 103 child.getChildNodes().item(0).getNodeType() == Node.TEXT_NODE) { 104 String value = child.getChildNodes().item(0).getNodeValue().trim(); 105 if (value.length() > 0) isValid = true; 106 } 107 } 108 } 109 110 } 111 } 112 if (this.getLogger().isDebugEnabled()) { 113 this.getLogger().debug("END isValidAuthenticationFragment valid=" + isValid); 114 } 115 return isValid; 116 } 117 118 121 public AuthenticationResult authenticate(HandlerConfiguration configuration, 122 SourceParameters parameters) 123 throws ProcessingException { 124 if (this.getLogger().isDebugEnabled() ) { 125 this.getLogger().debug("start authenticator using handler " + configuration.getName()); 126 } 127 128 final String authenticationResourceName = configuration.getAuthenticationResource(); 129 final SourceParameters authenticationParameters = configuration.getAuthenticationResourceParameters(); 130 if (parameters != null) { 131 parameters.add(authenticationParameters); 132 } else { 133 parameters = authenticationParameters; 134 } 135 136 Document doc = null; 137 String exceptionMsg = null; 138 139 try { 141 Source source = null; 142 try { 143 source = SourceUtil.getSource(authenticationResourceName, null, 144 parameters, this.resolver); 145 doc = SourceUtil.toDOM(source); 146 } catch (SAXException se) { 147 throw new ProcessingException(se); 148 } catch (SourceException se) { 149 throw SourceUtil.handle(se); 150 } catch (IOException e) { 151 throw new ProcessingException(e); 152 } finally { 153 this.resolver.release(source); 154 } 155 } catch (ProcessingException local) { 156 this.getLogger().error("authenticator: " + local.getMessage(), local); 157 exceptionMsg = local.getMessage(); 158 } 159 160 boolean isValid = false; 162 AuthenticationResult result = null; 163 if (doc != null) { 164 isValid = this.isValidAuthenticationFragment( doc ); 165 166 if ( isValid ) { 167 if (this.getLogger().isInfoEnabled() ) { 168 this.getLogger().info("Authenticator: User authenticated using handler '" 169 + configuration.getName() + "'"); 170 } 171 172 MediaManager mediaManager = null; 173 String mediaType; 174 try { 175 mediaManager = (MediaManager)this.manager.lookup( MediaManager.ROLE ); 176 mediaType = mediaManager.getMediaType(); 177 } catch (ServiceException se) { 178 throw new ProcessingException("Unable to lookup media manager.", se); 179 } finally { 180 this.manager.release( mediaManager ); 181 } 182 synchronized (configuration) { 183 Element specialElement; 186 Text specialValue; 187 Element authNode; 188 189 authNode = (Element )doc.getFirstChild(); 190 191 specialElement = doc.createElementNS(null, "type"); 192 specialValue = doc.createTextNode("cocoon.authentication"); 193 specialElement.appendChild(specialValue); 194 authNode.appendChild(specialElement); 195 196 specialElement = doc.createElementNS(null, "media"); 197 specialValue = doc.createTextNode(mediaType); 198 specialElement.appendChild(specialValue); 199 authNode.appendChild(specialElement); 200 201 result = new AuthenticationResult(true, doc); 202 203 } } 205 } 206 207 if ( !isValid ) { 208 if (this.getLogger().isInfoEnabled() ) { 209 this.getLogger().info("Authenticator: Failed authentication using handler '" 210 + configuration.getName()+ "'"); 211 } 212 Node data = null; 214 215 if (doc != null) { 216 data = DOMUtil.getFirstNodeFromPath(doc, 217 new String [] {"authentication","data"}, 218 false); 219 } 220 doc = DOMUtil.createDocument(); 221 222 final Element root = doc.createElementNS(null, "root"); 230 doc.appendChild(root); 231 Element element = doc.createElementNS(null, "failed"); 232 root.appendChild(element); 233 234 if (exceptionMsg != null) { 235 Text text = doc.createTextNode(exceptionMsg); 236 element.appendChild(text); 237 } 238 239 if (data == null) { 240 element = doc.createElementNS(null, "data"); 241 root.appendChild(element); 242 Text text = doc.createTextNode("No information available"); 243 element.appendChild(text); 244 } else { 245 root.appendChild(doc.importNode(data, true)); 246 } 247 248 result = new AuthenticationResult(false, doc); 249 } 250 251 if (this.getLogger().isDebugEnabled() ) { 252 this.getLogger().debug("end authenticator"); 253 } 254 255 return result; 256 } 257 258 259 262 public void service(ServiceManager manager) throws ServiceException { 263 this.manager = manager; 264 this.resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 265 } 266 267 270 public void dispose() { 271 if ( this.manager != null ){ 272 this.manager.release( this.resolver ); 273 this.manager = null; 274 this.resolver = null; 275 } 276 } 277 278 281 public void logout(UserHandler handler) { 282 if (this.getLogger().isDebugEnabled() ) { 283 this.getLogger().debug("logout using handler " + handler.getHandlerName()); 284 } 285 286 final HandlerConfiguration configuration = handler.getHandlerConfiguration(); 287 final String logoutResourceName = configuration.getLogoutResource(); 288 if (logoutResourceName != null) { 289 final SourceParameters parameters = configuration.getAuthenticationResourceParameters(); 290 291 Source source = null; 293 try { 294 source = SourceUtil.getSource(logoutResourceName, null, parameters, this.resolver); 297 SourceUtil.toDOM(source); 298 } catch (Exception ignore) { 299 this.getLogger().error("logout: " + ignore.getMessage(), ignore); 300 } finally { 301 this.resolver.release(source); 302 } 303 } 304 } 305 } 306 | Popular Tags |