Author: cziegeler
Date: Sat Mar  6 19:28:25 2010
New Revision: 919825

URL: http://svn.apache.org/viewvc?rev=919825&view=rev
Log:
SLING-1419 : Remove dependency to JCR

Modified:
    
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java
    
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java
    
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java
    
sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java
    
sling/trunk/bundles/extensions/formauth/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java
    
sling/trunk/bundles/extensions/openidauth/src/main/java/org/apache/sling/openidauth/impl/OpenIDAuthenticationHandler.java

Modified: 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java?rev=919825&r1=919824&r2=919825&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java
 (original)
+++ 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java
 Sat Mar  6 19:28:25 2010
@@ -22,8 +22,10 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.Map;
 
+import javax.jcr.Credentials;
 import javax.jcr.LoginException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -521,6 +523,41 @@
     }
 
     /**
+     * Create a credentials object from the provided authentication info.
+     * If no map is provided, <code>null</code> is returned.
+     * If a map is provided and contains a credentials object, this object is
+     * returned.
+     * If a map is provided but does not contain a credentials object nor a
+     * user, <code>null</code> is returned.
+     * if a map is provided with a user name but without a credentials object
+     * a new credentials object is created and all values from the 
authentication
+     * info are added as attributes.
+     * @param authenticationInfo Optional authentication info
+     * @return A credentials object or <code>null</code>
+     */
+    private Credentials getCredentials(final Map<String, Object> 
authenticationInfo) {
+        if ( authenticationInfo == null ) {
+            return null;
+        }
+        Credentials credentials = (Credentials) 
authenticationInfo.get(AuthenticationInfo.CREDENTIALS);
+        if ( credentials == null ) {
+            // otherwise try to create SimpleCredentials if the userId is set
+            final String userId = (String) 
authenticationInfo.get(AuthenticationInfo.USER);
+            if (userId != null) {
+                final char[] password = (char[]) 
authenticationInfo.get(AuthenticationInfo.PASSWORD);
+                credentials = new SimpleCredentials(userId, (password == null 
? new char[0] : password));
+
+                // add attributes
+                final Iterator<Map.Entry<String, Object>> i = 
authenticationInfo.entrySet().iterator();
+                while  (i.hasNext() ) {
+                    final Map.Entry<String, Object> current = i.next();
+                    
((SimpleCredentials)credentials).setAttribute(current.getKey(), 
current.getValue());
+                }
+            }
+        }
+        return credentials;
+    }
+    /**
      * Try to acquire an Session as indicated by authInfo
      *
      * @return <code>true</code> if request processing should continue assuming
@@ -537,8 +574,8 @@
 
         // try to connect
         try {
-            Session session = repository.login(authInfo.getCredentials(),
-                authInfo.getWorkspaceName());
+            Session session = repository.login(getCredentials(authInfo),
+                (String)authInfo.get(AuthenticationInfo.WORKSPACE));
 
             // handle impersonation
             session = handleImpersonation(request, response, session);

Modified: 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java?rev=919825&r1=919824&r2=919825&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java
 (original)
+++ 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java
 Sat Mar  6 19:28:25 2010
@@ -70,8 +70,8 @@
 
         AuthenticationInfo info = new AuthenticationInfo(
             engineAuthInfo.getAuthType());
-        info.setCredentials(engineAuthInfo.getCredentials());
-        info.setWorkspaceName(engineAuthInfo.getWorkspaceName());
+        info.put(AuthenticationInfo.CREDENTIALS, 
engineAuthInfo.getCredentials());
+        info.put(AuthenticationInfo.WORKSPACE, 
engineAuthInfo.getWorkspaceName());
 
         return info;
     }

Modified: 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java?rev=919825&r1=919824&r2=919825&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java
 (original)
+++ 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java
 Sat Mar  6 19:28:25 2010
@@ -21,9 +21,6 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.jcr.Credentials;
-import javax.jcr.SimpleCredentials;
-
 /**
  * The <code>AuthenticationInfo</code> conveys any authentication credentials
  * and/or details extracted by the
@@ -250,59 +247,6 @@
     }
 
     /**
-     * @param workspaceName The name of the workspace to connect to. If this is
-     *            <code>null</code> the current workspace name is not replaced.
-     */
-    public final void setWorkspaceName(String workspaceName) {
-        putIfNotNull(WORKSPACE, workspaceName);
-    }
-
-    /**
-     * Returns the workspace name stored as the {...@link #WORKSPACE} property 
or
-     * <code>null</code> if the workspace name is not set in this map.
-     */
-    public final String getWorkspaceName() {
-        return (String) get(WORKSPACE);
-    }
-
-    /**
-     * @param credentials The <code>Credentials</code> to authenticate with. If
-     *            this is <code>null</code> the currently set credentials are
-     *            not replaced.
-     */
-    public final void setCredentials(Credentials credentials) {
-        putIfNotNull(CREDENTIALS, credentials);
-    }
-
-    /**
-     * Returns the JCR credentials stored as the {...@link #CREDENTIALS} 
property.
-     * If the {...@link #CREDENTIALS} object is not set but the user ID (
-     * {...@link #USER}) is set, <code>SimpleCredentials</code> object is 
returned
-     * based on that user ID and the (optional) {...@link #PASSWORD}. If the 
userID
-     * is not set, this method returns <code>null</code>.
-     */
-    public final Credentials getCredentials() {
-
-        // return credentials explicitly set
-        final Credentials creds = (Credentials) get(CREDENTIALS);
-        if (creds != null) {
-            return creds;
-        }
-
-        // otherwise try to create SimpleCredentials if the userId is set
-        final String userId = getUser();
-        if (userId != null) {
-            final char[] password = getPassword();
-            return new SimpleCredentials(userId, (password == null)
-                    ? new char[0]
-                    : password);
-        }
-
-        // finally, we cannot create credentials to return
-        return null;
-    }
-
-    /**
      * Sets or resets a property with the given <code>key</code> to a new
      * <code>value</code>. Some keys have special meanings and their values are
      * required to have predefined as listed in the following table:
@@ -359,7 +303,7 @@
                 + " property must be a char[]");
         }
 
-        if (CREDENTIALS.equals(key) && !(value instanceof Credentials)) {
+        if (CREDENTIALS.equals(key) && !isCredentialsObject(value)) {
             throw new IllegalArgumentException(CREDENTIALS
                 + " property must be a javax.jcr.Credentials instance");
         }
@@ -372,6 +316,24 @@
         return super.put(key, value);
     }
 
+    /** We do this check in order to avoid an import to the javax.jcr.* 
package */
+    private boolean isCredentialsObject(final Object value) {
+        if ( value == null ) {
+            return false;
+        }
+        Class<?> checkClass = value.getClass();
+        do {
+            final Class<?>[] interfaces = value.getClass().getInterfaces();
+            for(final Class<?> current : interfaces) {
+                if ( current.getName().equals("javax.jcr.Credentials") ) {
+                    return true;
+                }
+            }
+            checkClass = checkClass.getSuperclass();
+        } while ( checkClass != null );
+        return false;
+    }
+
     /**
      * Removes the entry with the given <code>key</code> and returns its former
      * value (if existing). If the <code>key</code> is {...@link #AUTH_TYPE} 
the

Modified: 
sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java?rev=919825&r1=919824&r2=919825&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java
 (original)
+++ 
sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java
 Sat Mar  6 19:28:25 2010
@@ -22,7 +22,6 @@
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import javax.jcr.Credentials;
@@ -76,7 +75,7 @@
         Assert.assertEquals("test", info.getAuthType());
         assertNull(info.getUser());
         assertNull(info.getPassword());
-        assertNull(info.getWorkspaceName());
+        assertNull(info.get(AuthenticationInfo.WORKSPACE));
     }
 
     @Test
@@ -85,7 +84,7 @@
         Assert.assertEquals("test", info.getAuthType());
         Assert.assertEquals("name", info.getUser());
         assertNull(info.getPassword());
-        assertNull(info.getWorkspaceName());
+        assertNull(info.get(AuthenticationInfo.WORKSPACE));
     }
 
     @Test
@@ -96,7 +95,7 @@
         Assert.assertEquals("test", info.getAuthType());
         Assert.assertEquals("name", info.getUser());
         assertSame(pwd, info.getPassword());
-        assertNull(info.getWorkspaceName());
+        assertNull(info.get(AuthenticationInfo.WORKSPACE));
     }
 
     @Test
@@ -107,7 +106,7 @@
         Assert.assertEquals("test", info.getAuthType());
         Assert.assertEquals("name", info.getUser());
         assertSame(pwd, info.getPassword());
-        Assert.assertEquals("wsp", info.getWorkspaceName());
+        Assert.assertEquals("wsp", info.get(AuthenticationInfo.WORKSPACE));
     }
 
     @Test
@@ -189,16 +188,16 @@
     public void testSetWorkspaceName() {
         final AuthenticationInfo info = new AuthenticationInfo("test", "user",
             new char[0], "wsp");
-        Assert.assertEquals("wsp", info.getWorkspaceName());
+        Assert.assertEquals("wsp", info.get(AuthenticationInfo.WORKSPACE));
 
-        info.setWorkspaceName(null);
-        Assert.assertEquals("wsp", info.getWorkspaceName());
+        info.remove(AuthenticationInfo.WORKSPACE);
+        Assert.assertEquals(null, info.get(AuthenticationInfo.WORKSPACE));
 
-        info.setWorkspaceName("dummy");
-        Assert.assertEquals("dummy", info.getWorkspaceName());
+        info.put(AuthenticationInfo.WORKSPACE, "dummy");
+        Assert.assertEquals("dummy", info.get(AuthenticationInfo.WORKSPACE));
 
-        info.setWorkspaceName("");
-        Assert.assertEquals("", info.getWorkspaceName());
+        info.put(AuthenticationInfo.WORKSPACE, "");
+        Assert.assertEquals("", info.get(AuthenticationInfo.WORKSPACE));
     }
 
     @Test
@@ -206,10 +205,10 @@
         final AuthenticationInfo info = new AuthenticationInfo("test");
         info.put(AuthenticationInfo.WORKSPACE, "wsp");
 
-        Assert.assertEquals("wsp", info.getWorkspaceName());
+        Assert.assertEquals("wsp", info.get(AuthenticationInfo.WORKSPACE));
         Assert.assertEquals("wsp", info.get(AuthenticationInfo.WORKSPACE));
         Assert.assertEquals(info.get(AuthenticationInfo.WORKSPACE),
-            info.getWorkspaceName());
+                info.get(AuthenticationInfo.WORKSPACE));
     }
 
     @Test
@@ -217,40 +216,32 @@
         final Credentials creds = new SimpleCredentials("user", new char[0]);
         final AuthenticationInfo info = new AuthenticationInfo("test");
 
-        info.put("user.jcr.credentials", creds);
-        Assert.assertSame(creds, info.getCredentials());
+        info.put(AuthenticationInfo.CREDENTIALS, creds);
+        Assert.assertSame(creds, info.get(AuthenticationInfo.CREDENTIALS));
 
-        info.setCredentials(null);
-        Assert.assertSame(creds, info.getCredentials());
+        info.remove(AuthenticationInfo.WORKSPACE);
+        Assert.assertSame(creds, info.get(AuthenticationInfo.CREDENTIALS));
     }
 
     @Test
     public void testGetCredentials() {
         final AuthenticationInfo info = new AuthenticationInfo("test");
 
-        assertNull(info.getCredentials());
+        assertNull(info.get(AuthenticationInfo.CREDENTIALS));
         assertFalse(info.containsKey(AuthenticationInfo.CREDENTIALS));
 
         final Credentials creds = new SimpleCredentials("user", new char[0]);
-        info.put("user.jcr.credentials", creds);
+        info.put(AuthenticationInfo.CREDENTIALS, creds);
 
-        assertSame(creds, info.getCredentials());
         assertSame(creds, info.get(AuthenticationInfo.CREDENTIALS));
-        assertSame(info.get(AuthenticationInfo.CREDENTIALS),
-            info.getCredentials());
 
         final String user = "user";
         final char[] pwd = new char[5];
         final AuthenticationInfo infoCred = new AuthenticationInfo("TEST",
             user, pwd);
 
-        // credentials auto-generated but not stored in the object
-        assertTrue(infoCred.getCredentials() instanceof SimpleCredentials);
+        // credentials not stored in the object
         assertFalse(infoCred.containsKey(AuthenticationInfo.CREDENTIALS));
-
-        final SimpleCredentials generated = (SimpleCredentials) 
infoCred.getCredentials();
-        assertSame(user, generated.getUserID());
-        assertEquals(pwd, generated.getPassword());
     }
 
     @Test
@@ -275,7 +266,8 @@
     public void testPutStringObject() {
         final AuthenticationInfo info = new AuthenticationInfo("test", "user",
             new char[2], "wsp");
-        info.setCredentials(new SimpleCredentials("user", new char[2]));
+        info.put(AuthenticationInfo.CREDENTIALS,
+                new SimpleCredentials("user", new char[2]));
 
         test_put_fail(info, AuthenticationInfo.AUTH_TYPE, null);
         test_put_fail(info, AuthenticationInfo.USER, null);

Modified: 
sling/trunk/bundles/extensions/formauth/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/formauth/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java?rev=919825&r1=919824&r2=919825&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/formauth/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java
 (original)
+++ 
sling/trunk/bundles/extensions/formauth/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java
 Sat Mar  6 19:28:25 2010
@@ -493,7 +493,7 @@
             final AuthenticationInfo authInfo) {
 
         // get current authentication data, may be missing after first login
-        String authData = getCookieAuthData(authInfo.getCredentials());
+        String authData = 
getCookieAuthData((Credentials)authInfo.get(AuthenticationInfo.CREDENTIALS));
 
         // check whether we have to "store" or create the data
         final boolean refreshCookie = needsRefresh(authData,
@@ -596,7 +596,7 @@
 
         final AuthenticationInfo info = new AuthenticationInfo(
             HttpServletRequest.FORM_AUTH, userId);
-        info.setCredentials(cookieAuthCredentials);
+        info.put(AuthenticationInfo.CREDENTIALS, cookieAuthCredentials);
 
         return info;
     }

Modified: 
sling/trunk/bundles/extensions/openidauth/src/main/java/org/apache/sling/openidauth/impl/OpenIDAuthenticationHandler.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/openidauth/src/main/java/org/apache/sling/openidauth/impl/OpenIDAuthenticationHandler.java?rev=919825&r1=919824&r2=919825&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/openidauth/src/main/java/org/apache/sling/openidauth/impl/OpenIDAuthenticationHandler.java
 (original)
+++ 
sling/trunk/bundles/extensions/openidauth/src/main/java/org/apache/sling/openidauth/impl/OpenIDAuthenticationHandler.java
 Sat Mar  6 19:28:25 2010
@@ -572,7 +572,7 @@
 
     private AuthenticationInfo getAuthInfoFromUser(OpenIdUser user) {
        final AuthenticationInfo info = new 
AuthenticationInfo(OpenIDConstants.OPEN_ID_AUTH_TYPE);
-        info.setCredentials(new OpenIdCredentials(user));
+        info.put(AuthenticationInfo.CREDENTIALS, new OpenIdCredentials(user));
         return info;
     }
 


Reply via email to