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/02/21 22:06:20 UTC
svn commit: rev 6814 - in incubator/directory/janus/trunk/sandbox: . src src/java src/java/org src/java/org/apache src/java/org/apache/janus src/java/org/apache/janus/authentication src/java/org/apache/janus/authentication/realm src/test src/test/org src/test/org/apache src/test/org/apache/janus src/test/org/apache/janus/authentication src/test/org/apache/janus/authentication/realm
Author: vtence
Date: Sat Feb 21 13:06:20 2004
New Revision: 6814
Added:
incubator/directory/janus/trunk/sandbox/
incubator/directory/janus/trunk/sandbox/src/
incubator/directory/janus/trunk/sandbox/src/java/
incubator/directory/janus/trunk/sandbox/src/java/org/
incubator/directory/janus/trunk/sandbox/src/java/org/apache/
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/Credential.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CredentialSet.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/AbstractPrincipal.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/AuthenticationMethod.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/CredentialsMatcher.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/DefaultRealm.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/EqualCredentials.java (contents, props changed)
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/MutableRealm.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/Realm.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java (contents, props changed)
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePrincipal.java (contents, props changed)
incubator/directory/janus/trunk/sandbox/src/test/
incubator/directory/janus/trunk/sandbox/src/test/org/
incubator/directory/janus/trunk/sandbox/src/test/org/apache/
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/AlwaysMatch.java (contents, props changed)
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/NeverMatch.java (contents, props changed)
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java (contents, props changed)
Log:
o Started rewrite of realm stuff (see DIR-14)
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/Credential.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/Credential.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,78 @@
+/*
+ * Copyright 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.janus.authentication;
+
+import java.io.Serializable;
+
+/**
+ * Class representing a unit of proof of identity.
+ * <p/>
+ * A credential is represented by a type and a value.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public final class Credential implements Serializable
+{
+ private final String m_type;
+ private final Object m_value;
+
+ /**
+ * Constructs a new credential object with the given type
+ * and value.
+ */
+ public Credential( String type, Object value )
+ {
+ m_type = type;
+ m_value = value;
+ }
+
+ public boolean isOfType( String type )
+ {
+ return m_type.equals( type );
+ }
+
+ public Object getValue()
+ {
+ return m_value;
+ }
+
+ public boolean equals( Object o )
+ {
+ if ( this == o ) return true;
+ if ( !(o instanceof Credential) ) return false;
+
+ final Credential credential = (Credential) o;
+
+ if ( !m_type.equals( credential.m_type ) ) return false;
+ if ( !m_value.equals( credential.m_value ) ) return false;
+
+ return true;
+ }
+
+ public int hashCode()
+ {
+ int result;
+ result = m_type.hashCode();
+ result = 29 * result + m_value.hashCode();
+ return result;
+ }
+
+ public String toString()
+ {
+ return "[type = " + m_type + ", value = " + m_value + "]";
+ }
+}
\ No newline at end of file
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CredentialSet.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CredentialSet.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,125 @@
+/*
+ * Copyright 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.janus.authentication;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * Declared final so we make sure no imposter implementation is possible.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public final class CredentialSet
+{
+ private final Set credentials;
+
+ public CredentialSet( Set credentials )
+ {
+ this.credentials = new HashSet( credentials );
+ }
+
+ /**
+ * Checks if this credential set contains credentials.
+ *
+ * @return true is this set is empty, false if it contains
+ * at least one credential.
+ */
+ public boolean isEmpty()
+ {
+ return credentials.isEmpty();
+ }
+
+ /**
+ * Returns a collection containing all the credential objects
+ * in the current collection.
+ *
+ * @return an unmodifiable collection of all the credentials in this set.
+ */
+ public Collection elements()
+ {
+ return Collections.unmodifiableSet( credentials );
+ }
+
+ /**
+ * Returns a subset of the current set
+ * of credentials composed of all credentials
+ * of the given type.
+ *
+ * @param type of credential to be returned.
+ * @return a new CredentialSet containing all
+ * of the Credential objects of the given type.
+ */
+ public CredentialSet getCredentials( String type )
+ {
+ final Set subSet = new HashSet();
+ for ( Iterator it = credentials.iterator(); it.hasNext(); )
+ {
+ final Credential c = (Credential) it.next();
+ if ( c.isOfType( type ) ) subSet.add( c );
+ }
+
+ return new CredentialSet( subSet );
+ }
+
+ public int size()
+ {
+ return credentials.size();
+ }
+
+ public boolean equals( Object o )
+ {
+ if ( this == o ) return true;
+ if ( !(o instanceof CredentialSet) ) return false;
+
+ final CredentialSet credentialSet = (CredentialSet) o;
+
+ if ( !credentials.equals( credentialSet.credentials ) ) return false;
+
+ return true;
+ }
+
+ public int hashCode()
+ {
+ return credentials.hashCode();
+ }
+
+ public String toString()
+ {
+ if ( isEmpty() ) return "{}";
+
+ StringBuffer sb = new StringBuffer( "{" );
+ for ( Iterator it = credentials.iterator(); it.hasNext(); )
+ {
+ Credential c = (Credential) it.next();
+ sb.append( c ).append( ", " );
+ }
+
+ removeTrailingSeparator( sb );
+ sb.append( "}" );
+
+ return sb.toString();
+ }
+
+ private void removeTrailingSeparator( StringBuffer sb )
+ {
+ sb.setLength( sb.length() - 2 );
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/AbstractPrincipal.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/AbstractPrincipal.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,68 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import java.security.Principal;
+
+/**
+ * A principal name is unique within the set
+ * of principals of the same type.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public abstract class AbstractPrincipal implements Principal
+{
+ private final String m_name;
+
+ public AbstractPrincipal( String name )
+ {
+ if ( name == null ) throw new NullPointerException( "name" );
+ if ( name.equals( "" ) )
+ {
+ throw new IllegalArgumentException( "Empty name" );
+ }
+ m_name = name;
+ }
+
+ public String getName()
+ {
+ return m_name;
+ }
+
+ public boolean equals( Object o )
+ {
+ if ( this == o ) return true;
+ if ( !( o instanceof AbstractPrincipal ) ) return false;
+
+ final AbstractPrincipal abstractPrincipal = (AbstractPrincipal) o;
+
+ if ( !m_name.equals( abstractPrincipal.m_name ) ) return false;
+
+ return true;
+ }
+
+ public int hashCode()
+ {
+ return m_name.hashCode();
+ }
+
+ public String toString()
+ {
+ return "name=" + m_name;
+ }
+}
+
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/AuthenticationMethod.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/AuthenticationMethod.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,33 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.CredentialSet;
+
+import java.security.Principal;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public interface AuthenticationMethod
+{
+ Principal getPrincipal( CredentialSet credentialSet );
+
+ boolean supports( CredentialSet credentialSet );
+
+ CredentialsMatcher matcher( CredentialSet credentials );
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/CredentialsMatcher.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/CredentialsMatcher.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,27 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.CredentialSet;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public interface CredentialsMatcher
+{
+ boolean matches( CredentialSet creds );
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/DefaultRealm.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/DefaultRealm.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,73 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.CredentialSet;
+
+import java.security.Principal;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class DefaultRealm implements MutableRealm
+{
+ private final AuthenticationMethod m_authenticationMethod;
+ private final Collection m_identities;
+
+ public DefaultRealm( AuthenticationMethod authenticationMethod )
+ {
+ m_authenticationMethod = authenticationMethod;
+ m_identities = new ArrayList();
+ }
+
+ public Principal validate( CredentialSet credentials )
+ {
+ if ( !m_authenticationMethod.supports( credentials ) ) return null;
+ if ( !contains( credentials ) ) return null;
+
+ return m_authenticationMethod.getPrincipal( credentials );
+ }
+
+ public boolean addIdentity( CredentialSet credentials )
+ {
+ if ( !m_authenticationMethod.supports( credentials ) ) throw new IllegalArgumentException( "Credentials not supported by authentication method" );
+ if ( contains( credentials ) ) return false;
+ m_identities.add( credentials );
+
+ return true;
+ }
+
+ private boolean contains(CredentialSet credentials)
+ {
+ CredentialsMatcher criterion = m_authenticationMethod.matcher( credentials );
+ return search( criterion );
+ }
+
+ public boolean search( CredentialsMatcher criterion )
+ {
+ for ( Iterator it = m_identities.iterator(); it.hasNext(); )
+ {
+ CredentialSet creds = (CredentialSet) it.next();
+ if (criterion.matches( creds )) return true;
+ }
+
+ return false;
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/EqualCredentials.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/EqualCredentials.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,37 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.CredentialSet;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class EqualCredentials implements CredentialsMatcher
+{
+ private final CredentialSet m_toMatch;
+
+ public EqualCredentials( CredentialSet credentials )
+ {
+ m_toMatch = credentials;
+ }
+
+ public boolean matches( CredentialSet creds )
+ {
+ return m_toMatch.equals( creds );
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/MutableRealm.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/MutableRealm.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,27 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.CredentialSet;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public interface MutableRealm extends Realm
+{
+ boolean addIdentity( CredentialSet credentials );
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/Realm.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/Realm.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,29 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.CredentialSet;
+
+import java.security.Principal;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public interface Realm
+{
+ Principal validate( CredentialSet credentials );
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,56 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.Credential;
+import org.apache.janus.authentication.CredentialSet;
+
+import java.security.Principal;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class UsernamePasswordAuthentication implements AuthenticationMethod
+{
+ public UsernamePasswordAuthentication()
+ {
+ }
+
+ public Principal getPrincipal( CredentialSet credentialSet )
+ {
+ CredentialSet usernames = credentialSet.getCredentials( "username" );
+ Credential username = (Credential) usernames.elements().iterator().next();
+ return new UsernamePrincipal( username.getValue().toString() );
+ }
+
+ public boolean supports( CredentialSet credentialSet )
+ {
+ if ( credentialSet.size() != 2 ) return false;
+ CredentialSet usernames = credentialSet.getCredentials( "username" );
+ if ( usernames.size() != 1 ) return false;
+ CredentialSet passwords = credentialSet.getCredentials( "password" );
+ if ( passwords.size() != 1 ) return false;
+
+ return true;
+ }
+
+ public CredentialsMatcher matcher( CredentialSet credentials )
+ {
+ return new EqualCredentials( credentials );
+ }
+
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePrincipal.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePrincipal.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,47 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class UsernamePrincipal extends AbstractPrincipal
+{
+ public UsernamePrincipal( String name )
+ {
+ super( name );
+ }
+
+ public boolean equals( Object o )
+ {
+ if ( this == o ) return true;
+ if ( !( o instanceof UsernamePrincipal ) ) return false;
+ if ( !super.equals( o ) ) return false;
+
+ return true;
+ }
+
+ public int hashCode()
+ {
+ return super.hashCode();
+ }
+
+ public String toString()
+ {
+ return "[Username: " + super.toString() + "]";
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/AlwaysMatch.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/AlwaysMatch.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,30 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.CredentialSet;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class AlwaysMatch implements CredentialsMatcher
+{
+ public boolean matches( CredentialSet credentials )
+ {
+ return true;
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,159 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import com.mockobjects.dynamic.C;
+import com.mockobjects.dynamic.Mock;
+import junit.framework.TestCase;
+import org.apache.janus.authentication.Credential;
+import org.apache.janus.authentication.CredentialSet;
+
+import java.security.Principal;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class DefaultRealmTest extends TestCase
+{
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( DefaultRealmTest.class );
+ }
+
+ private DefaultRealm realm;
+
+ public void testValidationFailsIfCredentialSetNotSupported()
+ {
+ Mock mockAuthenticationMethod = new Mock( AuthenticationMethod.class );
+ realm = new DefaultRealm( (AuthenticationMethod) mockAuthenticationMethod.proxy() );
+ mockAuthenticationMethod.matchAndReturn( "supports", joeCredentials(), false );
+ assertNull( "Empty credential set was validated", realm.validate( joeCredentials() ) );
+ }
+
+ public void testEmptyRealmNeverValidates()
+ {
+ Mock mockAuthenticationMethod = createMockAuthenticationMethod();
+ mockAuthenticationMethod.matchAndReturn( "matcher", C.ANY_ARGS, new AlwaysMatch() );
+ realm = new DefaultRealm( (AuthenticationMethod) mockAuthenticationMethod.proxy() );
+
+ assertNull( "Principal was returned but realm contains no entry",
+ realm.validate( johnCredentials() ) );
+
+ }
+
+ public void testValidationFailsIfCredentialsAreNotMatched()
+ {
+ Mock mockAuthenticationMethod = createMockAuthenticationMethod();
+ mockAuthenticationMethod.matchAndReturn( "matcher", C.ANY_ARGS, new NeverMatch() );
+
+ realm = new DefaultRealm( (AuthenticationMethod) mockAuthenticationMethod.proxy() );
+
+ assertNull( "Principal was returned but credentials are invalid",
+ realm.validate( johnCredentials() ) );
+
+ }
+
+ public void testValidationSucceedsIfOneEntryIsMatched()
+ {
+ Mock mockAuthenticationMethod = createMockAuthenticationMethod();
+ mockAuthenticationMethod.matchAndReturn( "matcher", C.ANY_ARGS, new EqualCredentials( janeCredentials() ) );
+ mockAuthenticationMethod.matchAndReturn( "getPrincipal", janeCredentials(), jane() );
+
+ realm = new DefaultRealm( (AuthenticationMethod) mockAuthenticationMethod.proxy() );
+
+ try
+ {
+ realm.addIdentity( janeCredentials() );
+ realm.addIdentity( johnCredentials() );
+ }
+ catch ( IllegalArgumentException unexpected )
+ {
+ fail( "Invalid credential set was reported when it was valid" );
+ }
+
+ assertEquals( "Principal identified does not match credentials",
+ jane(),
+ realm.validate( janeCredentials() ) );
+ }
+
+
+ public void testRejectsNewEntryIfCredentialSetIsNotSupported()
+ {
+ Mock mockAuthenticationMethod = new Mock( AuthenticationMethod.class );
+ realm = new DefaultRealm( (AuthenticationMethod) mockAuthenticationMethod.proxy() );
+
+ mockAuthenticationMethod.matchAndReturn( "supports", joeCredentials(), false );
+
+ try
+ {
+ realm.addIdentity( joeCredentials() );
+ fail( "Malformed credential set was accepted in realm" );
+ }
+ catch ( IllegalArgumentException expected )
+ {
+ assertTrue( true );
+ }
+ }
+
+ public void testIdentityIsNotAddedIfAlreadyInRealm()
+ {
+ Mock mockAuthenticationMethod = createMockAuthenticationMethod();
+ realm = new DefaultRealm( (AuthenticationMethod) mockAuthenticationMethod.proxy() );
+ mockAuthenticationMethod.matchAndReturn( "matcher", C.ANY_ARGS, new EqualCredentials( joeCredentials() ) );
+
+ realm.addIdentity( joeCredentials() );
+ assertFalse( "Identity reported as added twice", realm.addIdentity( joeCredentials() ) );
+ }
+
+ private CredentialSet johnCredentials()
+ {
+ Set creds = new HashSet();
+ creds.add( new Credential( "username", "john" ) );
+ creds.add( new Credential( "password", "doe" ) );
+ return new CredentialSet( creds );
+ }
+
+ private CredentialSet janeCredentials()
+ {
+ Set creds = new HashSet();
+ creds.add( new Credential( "username", "jane" ) );
+ creds.add( new Credential( "password", "doe" ) );
+ return new CredentialSet( creds );
+ }
+
+ private CredentialSet joeCredentials()
+ {
+ Set creds = new HashSet();
+ creds.add( new Credential( "username", "joe" ) );
+ creds.add( new Credential( "password", "blow" ) );
+ return new CredentialSet( creds );
+ }
+
+ private Principal jane()
+ {
+ return new UsernamePrincipal( "jane" );
+ }
+
+ public Mock createMockAuthenticationMethod()
+ {
+ Mock mockAuthenticationMethod = new Mock( AuthenticationMethod.class );
+ mockAuthenticationMethod.matchAndReturn( "supports", C.ANY_ARGS, true );
+ return mockAuthenticationMethod;
+ }
+}
\ No newline at end of file
Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/NeverMatch.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/NeverMatch.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,30 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import org.apache.janus.authentication.CredentialSet;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class NeverMatch implements CredentialsMatcher
+{
+ public boolean matches( CredentialSet credentials )
+ {
+ return false;
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java Sat Feb 21 13:06:20 2004
@@ -0,0 +1,88 @@
+/*
+ * Copyright 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.janus.authentication.realm;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+import org.apache.janus.authentication.Credential;
+import org.apache.janus.authentication.CredentialSet;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class UsernamePasswordAuthenticationTest extends TestCase
+{
+ private UsernamePasswordAuthentication m_auth;
+
+ protected void setUp() throws Exception
+ {
+ m_auth = new UsernamePasswordAuthentication();
+ }
+
+ private CredentialSet validCredentials()
+ {
+ Set credentials = new HashSet();
+ credentials.add( new Credential( "username", "john" ) );
+ credentials.add( new Credential( "password", "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" ) );
+ 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" ) ) ) ) );
+ }
+
+ public void testCredentialsWithNoPasswordAreNotSupported()
+ {
+ assertFalse( "Reports it supports credentials with no password", m_auth.supports( new CredentialSet( Collections.singleton( new Credential( "username", "foo" ) ) ) ) );
+ }
+
+ public void testExtraCredentialsAreNotSupported()
+ {
+ assertFalse( "Reports it supports extra credentials", m_auth.supports( extraCredentials() ) );
+ }
+
+ public void testUsernameAndPaswordAreValidCredentials()
+ {
+ assertFalse( "Reports it does not support credentials with a username and a password", m_auth.supports( extraCredentials() ) );
+ }
+
+ public void testCredentialsMatchesIfEqual()
+ {
+ assertTrue( "Equal sets of credentials do not match", m_auth.matcher( validCredentials() ).matches( validCredentials() ) );
+ assertFalse( "Different sets of credentials were matched", m_auth.matcher( extraCredentials() ).matches( validCredentials() ) );
+ }
+
+ public void testCreatesUsernamePrincipals()
+ {
+ Assert.assertEquals( "Created wrong principal", new UsernamePrincipal( "john" ), m_auth.getPrincipal( validCredentials() ) );
+ }
+}