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/27 00:22:37 UTC
svn commit: rev 6891 - in incubator/directory/janus/trunk/sandbox: . src/java/org/apache/janus/authentication src/java/org/apache/janus/authentication/realm src/test/org/apache/janus/authentication src/test/org/apache/janus/authentication/realm
Author: vtence
Date: Thu Feb 26 15:22:36 2004
New Revision: 6891
Added:
incubator/directory/janus/trunk/sandbox/project.properties
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/AuthenticationException.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/Authenticator.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/DefaultAuthenticator.java
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/DefaultAuthenticatorTest.java
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/FruitPrincipal.java
Modified:
incubator/directory/janus/trunk/sandbox/ (props changed)
incubator/directory/janus/trunk/sandbox/project.xml
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/UsernamePasswordAuthentication.java
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java
Log:
o Preparing integration of new realm code to main tree
Added: incubator/directory/janus/trunk/sandbox/project.properties
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/project.properties Thu Feb 26 15:22:36 2004
@@ -0,0 +1,14 @@
+maven.compile.source=1.4
+maven.compile.target=1.4
+maven.test.source=1.4
+maven.compile.deprecation=true
+maven.compile.debug=true
+maven.compile.optimize=true
+
+maven.javadoc.links=http://java.sun.com/j2se/1.4.1/docs/api/
+maven.javadoc.source=1.4
+
+maven.test.search.classdir = true
+
+maven.junit.fork=true
+maven.junit.usefile=true
\ No newline at end of file
Modified: incubator/directory/janus/trunk/sandbox/project.xml
==============================================================================
--- incubator/directory/janus/trunk/sandbox/project.xml (original)
+++ incubator/directory/janus/trunk/sandbox/project.xml Thu Feb 26 15:22:36 2004
@@ -20,17 +20,22 @@
</dependency>
<dependency>
- <id>asm</id>
- <version>1.4.1</version>
- <url>http://asm.objectweb.org/</url>
+ <id>asm</id>
+ <version>1.4.1</version>
+ <url>http://asm.objectweb.org/</url>
</dependency>
<dependency>
- <id>asm+util</id>
- <version>1.4.1</version>
- <url>http://asm.objectweb.org/</url>
+ <id>asm+util</id>
+ <version>1.4.1</version>
+ <url>http://asm.objectweb.org/</url>
+ </dependency>
+
+ <dependency>
+ <groupId>dom4j</groupId>
+ <artifactId>dom4j</artifactId>
+ <version>1.4</version>
</dependency>
-
</dependencies>
</project>
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/AuthenticationException.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/AuthenticationException.java Thu Feb 26 15:22:36 2004
@@ -0,0 +1,31 @@
+/*
+ * 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;
+
+/**
+ * /**
+ * Thrown if there is a problem performing authentication.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class AuthenticationException extends Exception
+{
+ public AuthenticationException( String s )
+ {
+ super( s );
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/Authenticator.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/Authenticator.java Thu Feb 26 15:22:36 2004
@@ -0,0 +1,46 @@
+/*
+ * 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 javax.security.auth.Subject;
+
+/**
+ * <i><strong>Warning:</strong> This is experimental. Don't know yet if authenticator
+ * may use a single realm or several realms to perform authentication. In the case of several realms,
+ * each realm will probably support a unique authentication method and the argument to <code>authenticate</code>
+ * will change to a grouping of credential collections.</i>
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public interface Authenticator
+{
+ /**
+ * Returns a populated Subject with the principals which represent the
+ * identity of the user as well as any other principal for which permissions may be associated.
+ * <p/>
+ * If the configured realm implementation has <code>GroupSupport</code> then this
+ * authenticator may choose to add a principal for each group the user is a member of.
+ *
+ * @param credentials A collection of credential objects provided as proof of identity
+ * @return a Subject populated with appropriate principals
+ * @throws AuthenticationException
+ * thrown if there is a problem during authentication
+ */
+ Subject authenticate( CredentialSet credentials )
+ throws AuthenticationException;
+}
+
Modified: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CredentialSet.java
==============================================================================
--- incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CredentialSet.java (original)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/CredentialSet.java Thu Feb 26 15:22:36 2004
@@ -94,6 +94,22 @@
return subSet;
}
+ public Credential getCredential( String type )
+ {
+ for ( Iterator it = m_credentials.iterator(); it.hasNext(); )
+ {
+ final Credential c = (Credential) it.next();
+ if ( c.isOfType( type ) ) return c;
+ }
+
+ return null;
+ }
+
+ public Credential getCredentialCount( String type )
+ {
+ return getCredential( type );
+ }
+
public int size()
{
return m_credentials.size();
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/DefaultAuthenticator.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/DefaultAuthenticator.java Thu Feb 26 15:22:36 2004
@@ -0,0 +1,52 @@
+/*
+ * 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 org.apache.janus.authentication.realm.Realm;
+
+import javax.security.auth.Subject;
+import java.security.Principal;
+
+/**
+ * An implementation of an authenticator that uses a single realm to perform authentication.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class DefaultAuthenticator implements Authenticator
+{
+ private final Realm m_realm;
+
+ public DefaultAuthenticator( Realm realm )
+ {
+ m_realm = realm;
+ }
+
+ public Subject authenticate( CredentialSet credentials )
+ throws AuthenticationException
+ {
+ Principal p = m_realm.validateCredentials( credentials );
+ if ( p == null )
+ {
+ throw new AuthenticationException( "Credentials rejected" );
+ }
+
+ Subject subject = new Subject();
+ subject.getPrincipals().add( p );
+
+ return subject;
+ }
+}
Modified: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java
==============================================================================
--- incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java (original)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/UsernamePasswordAuthentication.java Thu Feb 26 15:22:36 2004
@@ -32,8 +32,7 @@
public Principal getPrincipal( CredentialSet credentialSet )
{
- CredentialSet usernames = credentialSet.getCredentials( "username" );
- Credential username = (Credential) usernames.elements().iterator().next();
+ Credential username = credentialSet.getCredential( "username" );
return new UsernamePrincipal( username.getValue().toString() );
}
Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/DefaultAuthenticatorTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/DefaultAuthenticatorTest.java Thu Feb 26 15:22:36 2004
@@ -0,0 +1,86 @@
+/*
+ * 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 com.mockobjects.dynamic.C;
+import com.mockobjects.dynamic.Mock;
+import junit.framework.TestCase;
+import org.apache.janus.authentication.realm.Realm;
+
+import javax.security.auth.Subject;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class DefaultAuthenticatorTest extends TestCase
+{
+ private DefaultAuthenticator m_authenticator;
+ private Mock m_mockRealm;
+
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( DefaultAuthenticatorTest.class );
+ }
+
+ protected void setUp() throws Exception
+ {
+ m_mockRealm
+ = new Mock( Realm.class );
+ m_authenticator
+ = new DefaultAuthenticator( (Realm) m_mockRealm.proxy() );
+ }
+
+ private CredentialSet banana()
+ {
+ CredentialSet credentials = new CredentialSet();
+ credentials.add( new Credential( "fruit", "banana" ) );
+ return credentials;
+ }
+
+ public void testAuthentication()
+ {
+ m_mockRealm.matchAndReturn( "validateCredentials", C.eq( banana() ),
+ new FruitPrincipal( "banana" ) );
+
+ Subject subject = null;
+ try
+ {
+ subject = m_authenticator.authenticate( banana() );
+ }
+ catch ( AuthenticationException e )
+ {
+ fail( "Login failed" );
+ }
+ assertTrue( "Principal was not added to subject",
+ subject.getPrincipals().contains( new FruitPrincipal( "banana" ) ) );
+ }
+
+ public void testAuthenticationFailure()
+ {
+ m_mockRealm.matchAndReturn( "validateCredentials", C.ANY_ARGS, null );
+
+ try
+ {
+ m_authenticator.authenticate( new CredentialSet() );
+ fail( "Login has not failed" );
+ }
+ catch ( AuthenticationException expected )
+ {
+ assertTrue( true );
+ }
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/FruitPrincipal.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/FruitPrincipal.java Thu Feb 26 15:22:36 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;
+
+import org.apache.janus.authentication.realm.AbstractPrincipal;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class FruitPrincipal extends AbstractPrincipal
+{
+ public FruitPrincipal( String name )
+ {
+ super( name );
+ }
+}
Modified: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java
==============================================================================
--- incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java (original)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/UsernamePasswordAuthenticationTest.java Thu Feb 26 15:22:36 2004
@@ -16,7 +16,6 @@
*/
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;