You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by vt...@apache.org on 2004/06/08 05:37:40 UTC

svn commit: rev 20894 - in incubator/directory/janus/trunk: core/api/src/java/org/apache/janus/authentication core/impl/src/java/org/apache/janus/authentication/realm core/impl/src/test/org/apache/janus/authentication core/impl/src/test/org/apache/janus/authentication/realm script/src/java/org/apache/janus/script/xml script/src/test/org/apache/janus/script/xml

Author: vtence
Date: Mon Jun  7 20:37:39 2004
New Revision: 20894

Removed:
   incubator/directory/janus/trunk/core/api/src/java/org/apache/janus/authentication/Credential.java
Modified:
   incubator/directory/janus/trunk/core/api/src/java/org/apache/janus/authentication/CredentialSet.java
   incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authentication/realm/UsernameCredentialMatcher.java
   incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java
   incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/DefaultAuthenticatorTest.java
   incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java
   incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java
   incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java
   incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java
Log:
Credentials now strongly typed

Modified: incubator/directory/janus/trunk/core/api/src/java/org/apache/janus/authentication/CredentialSet.java
==============================================================================
--- incubator/directory/janus/trunk/core/api/src/java/org/apache/janus/authentication/CredentialSet.java	(original)
+++ incubator/directory/janus/trunk/core/api/src/java/org/apache/janus/authentication/CredentialSet.java	Mon Jun  7 20:37:39 2004
@@ -16,6 +16,7 @@
  */
 package org.apache.janus.authentication;
 
+import java.io.Serializable;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
@@ -46,7 +47,7 @@
         m_credentials = new HashSet( credentials );
     }
 
-    public boolean add( Credential c )
+    public boolean add( Serializable c )
     {
         return m_credentials.add( c );
     }
@@ -78,31 +79,31 @@
      * of credentials composed of all credentials
      * of the given type.
      *
-     * @param type of credential to be returned.
+     * @param c the class of credential to be returned.
      * @return a new CredentialSet containing all
      *         of the Credential objects of the given type.
      */
-    public CredentialSet getCredentials( String type )
+    public CredentialSet getCredentials( Class c )
     {
         final CredentialSet subSet = new CredentialSet();
         for ( Iterator it = m_credentials.iterator(); it.hasNext(); )
         {
-            final Credential c = ( Credential ) it.next();
-            if ( c.typeIs( type ) ) subSet.add( c );
+            final Serializable credential = ( Serializable ) it.next();
+            if ( c.isInstance( credential ) ) subSet.add( c );
         }
 
         return subSet;
     }
 
-    public Credential getCredential( String type )
+    public Serializable getCredential( Class c )
     {
         for ( Iterator it = m_credentials.iterator(); it.hasNext(); )
         {
-            final Credential c = ( Credential ) it.next();
-            if ( c.typeIs( type ) ) return c;
+            final Serializable credential = (Serializable ) it.next();
+            if ( c.isInstance( credential ) ) return credential;
         }
 
-        return null;
+        throw new IllegalArgumentException( "No credential of class " + c.getName());
     }
 
     public int size()
@@ -134,7 +135,7 @@
         StringBuffer sb = new StringBuffer( "{" );
         for ( Iterator it = m_credentials.iterator(); it.hasNext(); )
         {
-            Credential c = ( Credential ) it.next();
+            Serializable c = ( Serializable ) it.next();
             sb.append( c ).append( ", " );
         }
 

Modified: incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authentication/realm/UsernameCredentialMatcher.java
==============================================================================
--- incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authentication/realm/UsernameCredentialMatcher.java	(original)
+++ incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authentication/realm/UsernameCredentialMatcher.java	Mon Jun  7 20:37:39 2004
@@ -16,24 +16,25 @@
  */
 package org.apache.janus.authentication.realm;
 
-import org.apache.janus.authentication.Credential;
 import org.apache.janus.authentication.CredentialSet;
 
+import java.io.Serializable;
+
 /**
  * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
  */
 public class UsernameCredentialMatcher implements CredentialsMatcher
 {
-    private final Credential m_username;
+    private final Serializable m_username;
 
-    public UsernameCredentialMatcher( Credential username )
+    public UsernameCredentialMatcher( Serializable username )
     {
         m_username = username;
     }
 
     public boolean matches( CredentialSet creds )
     {
-        Credential username = creds.getCredential( UsernamePasswordAuthentication.USERNAME );
+        Serializable username = creds.getCredential( UsernameCredential.class );
         return username.equals( m_username );
     }
 }

Modified: incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java
==============================================================================
--- incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java	(original)
+++ incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java	Mon Jun  7 20:37:39 2004
@@ -16,7 +16,6 @@
  */
 package org.apache.janus.authentication.realm;
 
-import org.apache.janus.authentication.Credential;
 import org.apache.janus.authentication.CredentialSet;
 
 import java.security.Principal;
@@ -26,25 +25,22 @@
  */
 public class UsernamePasswordAuthentication implements AuthenticationMethod
 {
-    public static final String USERNAME = "username";
-    public static final String PASSWORD = "password";
-
     public UsernamePasswordAuthentication()
     {
     }
 
     public Principal principal( CredentialSet credentialSet )
     {
-        Credential username = credentialSet.getCredential( USERNAME );
-        return new UsernamePrincipal( username.getValue().toString() );
+        UsernameCredential username = (UsernameCredential ) credentialSet.getCredential( UsernameCredential.class );
+        return new UsernamePrincipal( username.getUsername() );
     }
 
     public boolean supports( CredentialSet credentialSet )
     {
         if ( credentialSet.size() != 2 ) return false;
-        CredentialSet usernames = credentialSet.getCredentials( USERNAME );
+        CredentialSet usernames = credentialSet.getCredentials( UsernameCredential.class );
         if ( usernames.size() != 1 ) return false;
-        CredentialSet passwords = credentialSet.getCredentials( PASSWORD );
+        CredentialSet passwords = credentialSet.getCredentials( PasswordCredential.class );
         if ( passwords.size() != 1 ) return false;
 
         return true;
@@ -62,7 +58,7 @@
 
     public CredentialsMatcher identify( CredentialSet credentials )
     {
-        return new UsernameCredentialMatcher( credentials.getCredential( USERNAME ) );
+        return new UsernameCredentialMatcher( credentials.getCredential( UsernameCredential.class ) );
     }
 
 }

Modified: incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/DefaultAuthenticatorTest.java
==============================================================================
--- incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/DefaultAuthenticatorTest.java	(original)
+++ incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/DefaultAuthenticatorTest.java	Mon Jun  7 20:37:39 2004
@@ -17,6 +17,7 @@
 package org.apache.janus.authentication;
 
 import org.apache.janus.authentication.realm.Realm;
+import org.apache.janus.authentication.realm.UsernameCredential;
 import org.jmock.Mock;
 import org.jmock.MockObjectTestCase;
 
@@ -46,7 +47,7 @@
     private CredentialSet banana()
     {
         CredentialSet credentials = new CredentialSet();
-        credentials.add( new Credential( "fruit", "banana" ) );
+        credentials.add( new FruitCredential( "banana" ) );
         return credentials;
     }
 

Modified: incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java
==============================================================================
--- incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java	(original)
+++ incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java	Mon Jun  7 20:37:39 2004
@@ -16,7 +16,6 @@
  */
 package org.apache.janus.authentication.realm;
 
-import org.apache.janus.authentication.Credential;
 import org.apache.janus.authentication.CredentialSet;
 import org.jmock.MockObjectTestCase;
 
@@ -99,40 +98,40 @@
     private CredentialSet johnCredentials()
     {
         Set creds = new HashSet();
-        creds.add( new Credential( "username", "john" ) );
-        creds.add( new Credential( "password", "doe" ) );
+        creds.add( new UsernameCredential( "john" ) );
+        creds.add( new PasswordCredential( "doe" ) );
         return new CredentialSet( creds );
     }
 
     private CredentialSet janeCredentials()
     {
         Set creds = new HashSet();
-        creds.add( new Credential( "username", "jane" ) );
-        creds.add( new Credential( "password", "doe" ) );
+        creds.add( new UsernameCredential( "jane" ) );
+        creds.add( new PasswordCredential( "doe" ) );
         return new CredentialSet( creds );
     }
 
     private CredentialSet joeCredentials()
     {
         Set creds = new HashSet();
-        creds.add( new Credential( "username", "joe" ) );
-        creds.add( new Credential( "password", "blow" ) );
+        creds.add( new UsernameCredential( "joe" ) );
+        creds.add( new PasswordCredential( "blow" ) );
         return new CredentialSet( creds );
     }
 
     private CredentialSet unsupportedCredentials()
     {
         CredentialSet creds = new CredentialSet();
-        creds.add( new Credential( "eyes", "brown" ) );
-        creds.add( new Credential( "hair", "none" ) );
+        creds.add( new DummyCredential( "foo" ) );
+        creds.add( new DummyCredential( "bar" ) );
         return creds;
     }
 
     private CredentialSet imposterCredentials()
     {
         Set creds = new HashSet();
-        creds.add( new Credential( "username", "john" ) );
-        creds.add( new Credential( "password", "imposter" ) );
+        creds.add( new UsernameCredential( "john" ) );
+        creds.add( new PasswordCredential( "imposter" ) );
         return new CredentialSet( creds );
     }
 

Modified: incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java
==============================================================================
--- incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java	(original)
+++ incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java	Mon Jun  7 20:37:39 2004
@@ -17,7 +17,6 @@
 package org.apache.janus.authentication.realm;
 
 import junit.framework.TestCase;
-import org.apache.janus.authentication.Credential;
 import org.apache.janus.authentication.CredentialSet;
 
 import java.util.Collections;
@@ -39,29 +38,29 @@
     private CredentialSet validCredentials()
     {
         Set credentials = new HashSet();
-        credentials.add( new Credential( "username", "john" ) );
-        credentials.add( new Credential( "password", "doe" ) );
+        credentials.add( new UsernameCredential( "john" ) );
+        credentials.add( new PasswordCredential( "doe" ) );
         return new CredentialSet( credentials );
     }
 
     private CredentialSet extraCredentials()
     {
         Set credentials = new HashSet();
-        credentials.add( new Credential( "username", "john" ) );
-        credentials.add( new Credential( "password", "doe" ) );
-        credentials.add( new Credential( "password", "baz" ) );
-        credentials.add( new Credential( "foo", "bar" ) );
+        credentials.add( new UsernameCredential( "john" ) );
+        credentials.add( new PasswordCredential( "doe" ) );
+        credentials.add( new PasswordCredential( "baz" ) );
+        credentials.add( new DummyCredential( "foo" ) );
         return new CredentialSet( credentials );
     }
 
     public void testCredentialsWithNoUsernameAreNotSupported()
     {
-        assertFalse( "Reports it supports credentials with no username", m_auth.supports( new CredentialSet( Collections.singleton( new Credential( "password", "bar" ) ) ) ) );
+        assertFalse( "Reports it supports credentials with no username", m_auth.supports( new CredentialSet( Collections.singleton( new PasswordCredential( "password" ) ) ) ) );
     }
 
     public void testCredentialsWithNoPasswordAreNotSupported()
     {
-        assertFalse( "Reports it supports credentials with no password", m_auth.supports( new CredentialSet( Collections.singleton( new Credential( "username", "foo" ) ) ) ) );
+        assertFalse( "Reports it supports credentials with no password", m_auth.supports( new CredentialSet( Collections.singleton( new UsernameCredential( "username" ) ) ) ) );
     }
 
     public void testSetsWithAllValidCredentialsPlusExtraOnesAreNotSupported()

Modified: incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java
==============================================================================
--- incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java	(original)
+++ incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java	Mon Jun  7 20:37:39 2004
@@ -16,10 +16,11 @@
  */
 package org.apache.janus.script.xml;
 
-import org.apache.janus.authentication.Credential;
 import org.apache.janus.authentication.CredentialSet;
 import org.apache.janus.authentication.realm.IdentityInUseException;
 import org.apache.janus.authentication.realm.MutableRealm;
+import org.apache.janus.authentication.realm.PasswordCredential;
+import org.apache.janus.authentication.realm.UsernameCredential;
 import org.apache.janus.script.NullRealmBuilderMonitor;
 import org.apache.janus.script.RealmBuilder;
 import org.apache.janus.script.RealmBuilderMonitor;
@@ -73,9 +74,9 @@
             final Element user = ( Element ) it.next();
             CredentialSet creds = new CredentialSet();
             String username = user.attributeValue( "username" );
-            creds.add( new Credential( "username", username ) );
+            creds.add( new UsernameCredential( username ) );
             String password = user.attributeValue( "password" );
-            creds.add( new Credential( "password", password ) );
+            creds.add( new PasswordCredential( password ) );
 
             try
             {

Modified: incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java
==============================================================================
--- incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java	(original)
+++ incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java	Mon Jun  7 20:37:39 2004
@@ -16,10 +16,11 @@
  */
 package org.apache.janus.script.xml;
 
-import org.apache.janus.authentication.Credential;
 import org.apache.janus.authentication.CredentialSet;
 import org.apache.janus.authentication.realm.DefaultRealm;
 import org.apache.janus.authentication.realm.MutableRealm;
+import org.apache.janus.authentication.realm.PasswordCredential;
+import org.apache.janus.authentication.realm.UsernameCredential;
 import org.apache.janus.authentication.realm.UsernamePasswordAuthentication;
 import org.apache.janus.script.RealmBuilderMonitor;
 import org.jmock.Mock;
@@ -65,16 +66,16 @@
     private CredentialSet johnCredentials()
     {
         CredentialSet johnCredentials = new CredentialSet();
-        johnCredentials.add( new Credential( "username", "john" ) );
-        johnCredentials.add( new Credential( "password", "doe" ) );
+        johnCredentials.add( new UsernameCredential( "john" ) );
+        johnCredentials.add( new PasswordCredential( "doe" ) );
         return johnCredentials;
     }
 
     private CredentialSet janeCredentials()
     {
         CredentialSet johnCredentials = new CredentialSet();
-        johnCredentials.add( new Credential( "username", "jane" ) );
-        johnCredentials.add( new Credential( "password", "doe" ) );
+        johnCredentials.add( new UsernameCredential( "jane" ) );
+        johnCredentials.add( new PasswordCredential( "doe" ) );
         return johnCredentials;
     }