directory-commits mailing list archives

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

URL: http://svn.apache.org/viewcvs?view=rev&rev=109673
Log:
Selector interfaces moved to core module. Progressing towards adherence to Realm interface
...
Removed:
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SameCredentialsBuilder.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SelectorBuilder.java
Modified:
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.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/JDBCSelector.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/JDBCRealmTest.java
   incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java?view=diff&rev=109673&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java&r1=109672&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java&r2=109673
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java
Fri Dec  3 05:26:11 2004
@@ -16,6 +16,10 @@
  */
 package org.apache.janus.authentication.realm;
 
+import org.apache.janus.authentication.Credential;
+
+import java.sql.ResultSet;
+
 public class Column
 {
     private final String m_name;
@@ -70,5 +74,20 @@
     public String toString()
     {
         return "m_name=" + m_name + ", m_type=" + m_type;
+    }
+
+    public Object getObject( ResultSet rs ) throws JDBCException
+    {
+        Credential value = null;
+        try
+        {
+            value = (Credential) m_type.newInstance();
+            value.setValue( rs.getString( m_name ));
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException();
+        }
+        return value;
     }
 }

Deleted: /incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java?view=auto&rev=109672
==============================================================================

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=109673&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java&r1=109672&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java&r2=109673
==============================================================================
--- 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 05:26:11 2004
@@ -16,31 +16,34 @@
  */
 package org.apache.janus.authentication.realm;
 
+import org.apache.janus.authentication.CredentialSet;
+
 import javax.sql.DataSource;
 import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.security.Principal;
 
 public class JDBCRealm
 {
     private final DataSource m_ds;
     private final Table m_table;
+    private final AuthenticationMethod m_authenticationMethod;
 
-    public JDBCRealm( DataSource ds, Table table )
+    public JDBCRealm( DataSource ds, Table table, AuthenticationMethod authenticationMethod
)
     {
         m_ds = ds;
         m_table = table;
+        m_authenticationMethod = authenticationMethod;
     }
 
-    public boolean find( JDBCSelector selector ) throws JDBCException
+    public Principal find( JDBCSelector selector ) throws JDBCException
     {
         Connection conn = null;
-        boolean found = false;
+        CredentialSet creds = null;
         try
         {
             conn = m_ds.getConnection();
-            found = selector.select( conn );            
+            creds = selector.select( conn );
         }
         catch ( SQLException e )
         {
@@ -51,7 +54,7 @@
             closeConnection( conn );
         }
 
-        return found;
+        return creds != null ? m_authenticationMethod.principal( creds ) : null;
     }
 
     private void closeConnection( Connection conn )

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java?view=diff&rev=109673&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java&r1=109672&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java&r2=109673
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java
Fri Dec  3 05:26:11 2004
@@ -16,6 +16,8 @@
  */
 package org.apache.janus.authentication.realm;
 
+import org.apache.janus.authentication.CredentialSet;
+
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -41,15 +43,27 @@
         m_expressions.add( expression );
     }
 
-    public boolean select( Connection c ) throws SQLException
+    public CredentialSet select( Connection c ) throws SQLException
     {
-        PreparedStatement statement = c.prepareStatement( buildQueryString() );
-        bindParameterValues( statement, getParameterValues() );
-        ResultSet rs = statement.executeQuery();
-        boolean found = rs.next();
-        statement.close();
+        PreparedStatement statement = null;
+        CredentialSet creds = null;
+        try
+        {
+            statement = c.prepareStatement( buildQueryString() );
+            bindParameterValues( statement, getParameterValues() );
+            ResultSet rs = statement.executeQuery();
+            if ( rs.next() )
+            {
+                creds = m_table.instanciate( rs );
+            }
+            rs.close();
+        }
+        finally
+        {
+            if (statement != null) statement.close();
+        }
 
-        return found;
+        return creds;
     }
 
     private String[] getParameterValues()

Deleted: /incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SameCredentialsBuilder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SameCredentialsBuilder.java?view=auto&rev=109672
==============================================================================

Deleted: /incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java?view=auto&rev=109672
==============================================================================

Deleted: /incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SelectorBuilder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SelectorBuilder.java?view=auto&rev=109672
==============================================================================

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=109673&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java&r1=109672&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java&r2=109673
==============================================================================
--- 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 05:26:11 2004
@@ -16,10 +16,14 @@
  */
 package org.apache.janus.authentication.realm;
 
+import org.apache.janus.authentication.CredentialSet;
+
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Iterator;
+import java.sql.ResultSet;
 
 public class Table
 {
@@ -68,6 +72,18 @@
     public boolean supports( Class type )
     {
         return m_columns.containsKey( type );
+    }
+
+    public CredentialSet instanciate( ResultSet rs )
+    {
+        CredentialSet creds = new CredentialSet();
+        for ( Iterator it = m_columns.values().iterator(); it.hasNext(); )
+        {
+            Column column = ( Column ) it.next();
+            creds.add( column.getObject( rs ) );
+        }
+
+        return creds;
     }
 }
 

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=109673&p1=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java&r1=109672&p2=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java&r2=109673
==============================================================================
--- 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 05:26:11 2004
@@ -34,8 +34,8 @@
         Table table = Tables.users();
         m_db.create( table );
         m_db.insertInto( table, Creds.joe() );
-        m_db.insertInto( table, Creds.jane() );
-        m_realm = new JDBCRealm( m_db.getDataSource() , table );
+
+        m_realm = new JDBCRealm( m_db.getDataSource(), table, new UsernamePasswordAuthentication()
);
     }
 
     protected void tearDown() throws Exception
@@ -43,28 +43,49 @@
         m_db.dropAll();
     }
 
-    public void testExecutesSelectionQueriesAgainstDatabase() throws Exception
+    /**
+     * invalid credentials should return null
+     */
+
+    public void testAuthenticationSucceedsIfCredentialsCanBeSelected() throws Exception
     {
         JDBCSelector selector = new JDBCSelector( Tables.users() );
         SameCredentialsBuilder builder = new SameCredentialsBuilder( Creds.joe() );
         builder.build( new SQLExpressionBuilder( Tables.users() ), selector );
 
-        assertTrue( m_realm.find( selector ) );
+        assertEquals( new UsernamePrincipal( "joe" ), m_realm.find( selector ) );
+    }
+
+    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 ) );
     }
 
+//    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 testThrowsJDBCExceptionIfSQLExceptionOccurs()
     {
         JDBCSelector selector = new JDBCSelector( undefinedTable() );
 
         SameCredentialsBuilder builder = new SameCredentialsBuilder( Creds.joe() );
-        builder.build( new SQLExpressionBuilder(Tables.users()), selector );
+        builder.build( new SQLExpressionBuilder( Tables.users() ), selector );
 
         try
         {
             m_realm.find( selector );
             fail( "JDBCException expected" );
-        }
-        catch ( JDBCException expected )
+        } catch ( JDBCException expected )
         {
             assertTrue( true );
         }

Modified: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java?view=diff&rev=109673&p1=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java&r1=109672&p2=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java&r2=109673
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java
Fri Dec  3 05:26:11 2004
@@ -19,8 +19,6 @@
 import junit.framework.TestCase;
 
 import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 
 public class JDBCSelectorTest extends TestCase
 {
@@ -52,7 +50,7 @@
     public void testFindsDataBasedOnCredentialExpression() throws Exception
     {
         m_selector.addExpression( new EqExpression( Columns.username(), "joe" ) );
-        assertTrue( m_selector.select( m_connection ) );
+        assertEquals( Creds.joe(), m_selector.select( m_connection ) );
     }
 
     public void testSupportsMultipleCredentialExpressions() throws Exception
@@ -60,14 +58,14 @@
         m_selector.addExpression( new EqExpression( Columns.username(), "joe" ) );
         m_selector.addExpression( new EqExpression( Columns.password(), "foo" ) );
 
-        assertTrue( m_selector.select( m_connection ) );
+        assertEquals( Creds.joe(), m_selector.select( m_connection ) );
     }
 
     public void testSupportsAndExpressions() throws Exception
     {
         m_selector.addExpression( new AndExpression( new EqExpression( Columns.username(),
"joe" ),
                 new EqExpression( Columns.password(), "foo" ) ) );
-        assertTrue( m_selector.select( m_connection ) );
+        assertEquals( Creds.joe(), m_selector.select( m_connection ) );
     }
 
     public void testRejectsNonSQLExpressions()

Mime
View raw message