directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vte...@apache.org
Subject svn commit: r109714 - in incubator/directory/janus/trunk/jdbc/src: java/org/apache/janus/authentication/realm test/org/apache/janus/authentication/realm
Date Fri, 03 Dec 2004 18:09:05 GMT
Author: vtence
Date: Fri Dec  3 10:09:03 2004
New Revision: 109714

URL: http://svn.apache.org/viewcvs?view=rev&rev=109714
Log:
Got a working JDBC Realm implementation
Modified:
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/DefaultConverter.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java
   incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Creds.java
   incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java
   incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java?view=diff&rev=109714&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java&r1=109713&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java&r2=109714
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java
Fri Dec  3 10:09:03 2004
@@ -19,6 +19,4 @@
 public interface Converter
 {
     String convert( Object value );
-
-    boolean supports( Class type );
 }

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/DefaultConverter.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/DefaultConverter.java?view=diff&rev=109714&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/DefaultConverter.java&r1=109713&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/DefaultConverter.java&r2=109714
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/DefaultConverter.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/DefaultConverter.java
Fri Dec  3 10:09:03 2004
@@ -22,9 +22,4 @@
     {
         return value.toString();
     }
-
-    public boolean supports( Class type )
-    {
-        return true;
-    }
 }

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java?view=diff&rev=109714&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java&r1=109713&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java&r2=109714
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java
Fri Dec  3 10:09:03 2004
@@ -19,16 +19,21 @@
 import org.apache.janus.authentication.CredentialSet;
 
 import javax.sql.DataSource;
+import java.security.Principal;
 import java.sql.Connection;
 import java.sql.SQLException;
-import java.security.Principal;
 
-public class JDBCRealm
+public class JDBCRealm implements Realm
 {
     private final DataSource m_ds;
     private final Table m_table;
     private final AuthenticationMethod m_authenticationMethod;
 
+    public JDBCRealm( DataSource ds, Table table )
+    {
+        this( ds, table, new UsernamePasswordAuthentication() );
+    }
+
     public JDBCRealm( DataSource ds, Table table, AuthenticationMethod authenticationMethod
)
     {
         m_ds = ds;
@@ -36,7 +41,26 @@
         m_authenticationMethod = authenticationMethod;
     }
 
-    public Principal find( JDBCSelector selector ) throws JDBCException
+    public Principal validateCredentials( CredentialSet credentialSet )
+    {
+        if ( !m_authenticationMethod.supports( credentialSet ) ) return null;
+
+        CredentialSet creds = findAuthenticatedMatch( credentialSet );
+
+        return creds != null ? m_authenticationMethod.principal( creds ) : null;
+    }
+
+    private CredentialSet findAuthenticatedMatch( CredentialSet credentialSet )
+    {
+        SelectorBuilder selectorBuilder = m_authenticationMethod.getAuthenticationSelector(
credentialSet );
+        JDBCSelector selector = new JDBCSelector( m_table );
+        selectorBuilder.build( new SQLExpressionBuilder( m_table ), selector );
+
+        CredentialSet creds = find( selector );
+        return creds;
+    }
+
+    private CredentialSet find( JDBCSelector selector ) throws JDBCException
     {
         Connection conn = null;
         CredentialSet creds = null;
@@ -44,29 +68,27 @@
         {
             conn = m_ds.getConnection();
             creds = selector.select( conn );
-        }
-        catch ( SQLException e )
+        } catch ( SQLException e )
         {
             throw new JDBCException( e );
-        }
-        finally
+        } finally
         {
             closeConnection( conn );
         }
 
-        return creds != null ? m_authenticationMethod.principal( creds ) : null;
+        return creds;
     }
 
     private void closeConnection( Connection conn )
     {
-        if (conn == null) return;
+        if ( conn == null ) return;
 
         try
         {
             conn.close();
-        }
-        catch ( SQLException ignored )
+        } catch ( SQLException ignored )
         {
         }
     }
 }
+

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java?view=diff&rev=109714&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java&r1=109713&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java&r2=109714
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java
Fri Dec  3 10:09:03 2004
@@ -69,11 +69,6 @@
         return column;
     }
 
-    public boolean supports( Class type )
-    {
-        return m_columns.containsKey( type );
-    }
-
     public CredentialSet instanciate( ResultSet rs )
     {
         CredentialSet creds = new CredentialSet();

Modified: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Creds.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Creds.java?view=diff&rev=109714&p1=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Creds.java&r1=109713&p2=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Creds.java&r2=109714
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Creds.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Creds.java
Fri Dec  3 10:09:03 2004
@@ -17,6 +17,7 @@
 package org.apache.janus.authentication.realm;
 
 import org.apache.janus.authentication.CredentialSet;
+import org.apache.janus.authentication.Credential;
 
 public class Creds
 {
@@ -36,5 +37,33 @@
         creds.add( new UsernameCredential( "jane" ) );
         creds.add( new PasswordCredential( "bar" ) );
         return creds;
+    }
+
+    public static CredentialSet unsupported()
+    {
+        CredentialSet creds = new CredentialSet();
+        creds.add( new UsernameCredential( "joe" ) );
+        creds.add( new EyeColorCredential( "brown" ) );
+        return creds;
+    }
+
+    public static class EyeColorCredential implements Credential
+    {
+        private String m_color;
+
+        public EyeColorCredential( String color )
+        {
+            m_color = color;
+        }
+
+        public Object getValue()
+        {
+            return m_color;
+        }
+
+        public void setValue( Object value )
+        {
+            m_color = (String) value;
+        }
     }
 }

Modified: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java?view=diff&rev=109714&p1=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java&r1=109713&p2=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java&r2=109714
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java
Fri Dec  3 10:09:03 2004
@@ -35,7 +35,7 @@
         m_db.create( table );
         m_db.insertInto( table, Creds.joe() );
 
-        m_realm = new JDBCRealm( m_db.getDataSource(), table, new UsernamePasswordAuthentication()
);
+        m_realm = new JDBCRealm( m_db.getDataSource(), table );
     }
 
     protected void tearDown() throws Exception
@@ -43,60 +43,32 @@
         m_db.dropAll();
     }
 
-    /**
-     * invalid credentials should return null
-     */
-
-    public void testAuthenticationSucceedsIfCredentialsCanBeSelected() throws Exception
+    public void testAuthenticationSucceedsIfCredentialsAreMatchedInDatabase() throws Exception
     {
-        JDBCSelector selector = new JDBCSelector( Tables.users() );
-        SameCredentialsBuilder builder = new SameCredentialsBuilder( Creds.joe() );
-        builder.build( new SQLExpressionBuilder( Tables.users() ), selector );
-
-        assertEquals( new UsernamePrincipal( "joe" ), m_realm.find( selector ) );
+        assertEquals( new UsernamePrincipal( "joe" ), m_realm.validateCredentials( Creds.joe()
) );
     }
 
     public void testAuthenticationFailsIfSelectionYieldsEmptyResult() throws Exception
     {
-        JDBCSelector selector = new JDBCSelector( Tables.users() );
-        SameCredentialsBuilder builder = new SameCredentialsBuilder( Creds.jane() );
-        builder.build( new SQLExpressionBuilder( Tables.users() ), selector );
-
-        assertNull( m_realm.find( selector ) );
+        assertNull( m_realm.validateCredentials( Creds.jane() ) );
     }
 
-//    public void testValidationSucceedsIfCredentialSetIsMatchedAgainstDatabase()
-//    {
-//        JDBCSelector selector = new JDBCSelector( Tables.users() );
-//        SameCredentialsBuilder builder = new SameCredentialsBuilder( Creds.joe() );
-//        builder.build( new SQLExpressionBuilder( Tables.users() ), selector );
-//
-//        assertEquals( new UsernamePrincipal( "joe" ), m_realm.validate( Creds.joe(), selector
) );
-//    }
+    public void testValidationFailsIfCredentialSetNotSupported()
+    {
+        assertNull( "Unsupported credential set was validated", m_realm.validateCredentials(
Creds.unsupported() ) );
+    }
 
     public void testThrowsJDBCExceptionIfSQLExceptionOccurs()
     {
-        JDBCSelector selector = new JDBCSelector( undefinedTable() );
-
-        SameCredentialsBuilder builder = new SameCredentialsBuilder( Creds.joe() );
-        builder.build( new SQLExpressionBuilder( Tables.users() ), selector );
-
+        m_realm = new JDBCRealm( m_db.getDataSource(), Tables.undefined() );
         try
         {
-            m_realm.find( selector );
+            m_realm.validateCredentials( Creds.joe() );
             fail( "JDBCException expected" );
         } catch ( JDBCException expected )
         {
             assertTrue( true );
         }
-    }
-
-    private Table undefinedTable()
-    {
-        Table table = new Table( "UNDEFINED" );
-        table.addColumn( Columns.username() );
-        table.addColumn( Columns.password() );
-        return table;
     }
 }
 

Modified: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java?view=diff&rev=109714&p1=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java&r1=109713&p2=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java&r2=109714
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java
Fri Dec  3 10:09:03 2004
@@ -27,4 +27,12 @@
         table.addColumn( Columns.password() );
         return table;
     }
+
+    public static Table undefined()
+    {
+        Table table = new Table( "UNDEFINED" );
+        table.addColumn( Columns.username() );
+        table.addColumn( Columns.password() );
+        return table;
+    }
 }

Mime
View raw message