cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: rev 37164 - in cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication: components context user
Date Sat, 28 Aug 2004 13:30:14 GMT
Author: cziegeler
Date: Sat Aug 28 06:30:14 2004
New Revision: 37164

Added:
   cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/ServletAuthenticator.java
  (contents, props changed)
Modified:
   cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/PipelineAuthenticator.java
   cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java
   cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/user/UserHandler.java
Log:
Add first version of role support and servlet auth support

Modified: cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/PipelineAuthenticator.java
==============================================================================
--- cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/PipelineAuthenticator.java
(original)
+++ cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/PipelineAuthenticator.java
Sat Aug 28 06:30:14 2004
@@ -43,10 +43,9 @@
 
 /**
  * Verify if a user can be authenticated.
- * This is a helper class that could be made pluggable if required.
  *
  * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version CVS $Id: PipelineAuthenticator.java,v 1.15 2004/06/18 14:28:55 vgritsenko Exp
$
+ * @version CVS $Id$
 */
 public class PipelineAuthenticator
     extends AbstractLogEnabled

Added: cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/ServletAuthenticator.java
==============================================================================
--- (empty file)
+++ cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/ServletAuthenticator.java
Sat Aug 28 06:30:14 2004
@@ -0,0 +1,133 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.webapps.authentication.components;
+
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration;
+import org.apache.cocoon.webapps.authentication.user.UserHandler;
+import org.apache.excalibur.source.SourceParameters;
+import org.apache.excalibur.xml.dom.DOMParser;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+/**
+ * Verify if a user can be authenticated.
+ * This is a very simple authenticator that checks if the user is authenticated
+ * using the servlet authentication mechanisms.
+ *
+ * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
+ * @version CVS $Id: PipelineAuthenticator.java 30932 2004-07-29 17:35:38Z vgritsenko $
+*/
+public class ServletAuthenticator
+    extends AbstractLogEnabled
+    implements Contextualizable, ThreadSafe, Serviceable, Authenticator {
+
+    protected Context context;
+    protected ServiceManager manager;
+    
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
+     */
+    public void contextualize(Context context) throws ContextException {
+        this.context = context;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+     */
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+    
+    /** 
+     * Fill the authentication context.
+     * This method can be overwritten to add any application specific data
+     * to the user.
+     * (Don't forget to call this implementation via super as well as it
+     * adds the ID).
+     * @param context The context. This document has already the authentication
+     *                             root node.
+     */
+    protected void fillContext(Document contextDoc) {
+        final Request req = ContextHelper.getRequest(this.context);
+        final Element root = contextDoc.getDocumentElement();
+        
+        // append the ID
+        final Element id = contextDoc.createElement("ID");
+        id.appendChild(contextDoc.createTextNode(req.getRemoteUser()));
+        root.appendChild(id);        
+    }
+    
+    /* (non-Javadoc)
+     * @see org.apache.cocoon.webapps.authentication.components.Authenticator#authenticate(org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration,
org.apache.excalibur.source.SourceParameters)
+     */
+    public AuthenticationResult authenticate(HandlerConfiguration configuration,
+                                             SourceParameters parameters)
+    throws ProcessingException {
+        if (this.getLogger().isDebugEnabled() ) {
+            this.getLogger().debug("start authenticator using handler " + configuration.getName());
+        }
+
+        final Request req = ContextHelper.getRequest(this.context);
+        AuthenticationResult result = null;
+        if ( req.getRemoteUser() != null ) {
+            DOMParser parser = null;
+            try {
+                parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
+                final Document doc = parser.createDocument();
+                final Element root = doc.createElement("authentication");
+                doc.appendChild(root);
+                this.fillContext(doc);
+
+                result = new AuthenticationResult(true, doc);
+            } catch (SAXException se) {
+                throw new ProcessingException("Unable to create document.", se);
+            } catch (ServiceException se) {
+                throw new ProcessingException("Unable to lookup dom parser.", se);
+            } finally {
+                this.manager.release(parser);
+            }
+        }
+
+        if (this.getLogger().isDebugEnabled() ) {
+            this.getLogger().debug("end authenticator: " + result);
+        }
+
+        return result;
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.cocoon.webapps.authentication.components.Authenticator#logout(UserHandler)
+     */
+    public void logout(UserHandler handler) {
+        if (this.getLogger().isDebugEnabled() ) {
+            this.getLogger().debug("logout using handler " + handler.getHandlerName());
+        }
+        // TODO what can we do here?
+    }
+}

Modified: cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java
==============================================================================
--- cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java
(original)
+++ cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java
Sat Aug 28 06:30:14 2004
@@ -16,14 +16,19 @@
 package org.apache.cocoon.webapps.authentication.context;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.StringTokenizer;
 
 import org.apache.avalon.framework.CascadingRuntimeException;
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.components.source.SourceUtil;
+import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.webapps.authentication.AuthenticationConstants;
 import org.apache.cocoon.webapps.authentication.components.DefaultAuthenticationManager;
 import org.apache.cocoon.webapps.authentication.configuration.ApplicationConfiguration;
@@ -48,7 +53,7 @@
  * This is the implementation for the authentication context
  * 
  * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version CVS $Id: AuthenticationContext.java,v 1.18 2004/04/06 14:48:56 cziegeler Exp
$
+ * @version CVS $Id$
 */
 public class AuthenticationContext
 implements SessionContext {
@@ -61,6 +66,8 @@
     protected Context         context;
     protected XPathProcessor  xpathProcessor;
     protected SourceResolver  resolver;
+    /** A list of roles the user is in */
+    protected List            roles;
     
     /** Constructor */
     public AuthenticationContext(Context context, XPathProcessor processor, SourceResolver
resolver) {
@@ -801,4 +808,27 @@
         this.handler.setApplicationIsLoaded(appConf);
     }
 
+    /**
+     * Test if the user has a role
+     * @since 2.1.6
+     */
+    public boolean isUserInRole(String role) {
+        if ( this.roles == null ) {
+            this.roles = new ArrayList();
+            try {
+                final String allRoles = (String)this.getContextInfo().get("roles");
+                final StringTokenizer st = new StringTokenizer( allRoles, ",");
+                while ( st.hasMoreElements() ) {
+                    this.roles.add(st.nextElement());
+                }
+            } catch (ProcessingException pe) {
+                // we ignore this
+            }
+        }
+        if ( this.roles.contains( role ) ) {
+            return true;
+        }
+        final Request req = ContextHelper.getRequest(this.context);
+        return req.isUserInRole(role);
+    }
 }

Modified: cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/user/UserHandler.java
==============================================================================
--- cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/user/UserHandler.java
(original)
+++ cocoon/trunk/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/user/UserHandler.java
Sat Aug 28 06:30:14 2004
@@ -27,7 +27,7 @@
  * The authentication Handler.
  *
  * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version CVS $Id: UserHandler.java,v 1.11 2004/03/19 13:59:21 cziegeler Exp $
+ * @version CVS $Id$
 */
 public final class UserHandler
 implements java.io.Serializable {
@@ -65,9 +65,8 @@
     public boolean getApplicationsLoaded() {
         if ( this.handler.getApplications().isEmpty() ) {
             return true;
-        } else {
-            return this.appsLoaded;
         }
+        return this.appsLoaded;
     }
     
     /**
@@ -118,6 +117,14 @@
             }
         }
         return this.userID;
+    }
+    
+    /**
+     * Test if the user has a role
+     * @since 2.1.6
+     */
+    public boolean isUserInRole(String role) {
+        return this.context.isUserInRole(role);    
     }
     
     public void addApplicationContext(String name) {

Mime
View raw message