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/24 06:22:41 UTC
svn commit: rev 6842 - in incubator/directory/janus/trunk/sandbox/src: java/org/apache/janus/authentication/realm java/org/apache/janus/script java/org/apache/janus/script/xml test/org/apache/janus/authentication/realm test/org/apache/janus/script test/org/apache/janus/script/xml
Author: vtence
Date: Mon Feb 23 21:22:40 2004
New Revision: 6842
Added:
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/RealmBuilder.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/RealmBuilderMonitor.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/NullRealmBuilderMonitor.java
incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/XMLRealm.java
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/XMLRealmTest.java
Modified:
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/Realm.java
incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java
Log:
o Implemented DIR-15 - will have to move to main tree
Modified: 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/DefaultRealm.java (original)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/DefaultRealm.java Mon Feb 23 21:22:40 2004
@@ -37,7 +37,7 @@
m_identities = new ArrayList();
}
- public Principal validate( CredentialSet credentials )
+ public Principal validateCredentials( CredentialSet credentials )
{
if ( !m_authenticationMethod.supports( credentials ) ) return null;
if ( !contains( credentials ) ) return null;
Modified: 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/Realm.java (original)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/realm/Realm.java Mon Feb 23 21:22:40 2004
@@ -25,5 +25,5 @@
*/
public interface Realm
{
- Principal validate( CredentialSet credentials );
+ Principal validateCredentials( CredentialSet credentials );
}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/RealmBuilder.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/RealmBuilder.java Mon Feb 23 21:22:40 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.script;
+
+import org.apache.janus.authentication.realm.MutableRealm;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public interface RealmBuilder
+{
+ void buildRealm( MutableRealm realm ) throws Exception;
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/RealmBuilderMonitor.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/RealmBuilderMonitor.java Mon Feb 23 21:22:40 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.script;
+
+import org.apache.janus.authentication.CredentialSet;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public interface RealmBuilderMonitor
+{
+ void duplicateIdentity( CredentialSet identity );
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java Mon Feb 23 21:22:40 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.script.xml;
+
+import org.apache.janus.authentication.Credential;
+import org.apache.janus.authentication.CredentialSet;
+import org.apache.janus.authentication.realm.MutableRealm;
+import org.apache.janus.script.RealmBuilder;
+import org.apache.janus.script.RealmBuilderMonitor;
+import org.dom4j.Document;
+import org.dom4j.Element;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Builds a realm with username password identities.
+ * <p/>
+ * <strong>Warning:</strong> Document is assumed to be valid.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class Dom4JRealmBuilder implements RealmBuilder
+{
+ private final Document m_doc;
+ private final RealmBuilderMonitor m_monitor;
+
+ public Dom4JRealmBuilder( Document doc, RealmBuilderMonitor monitor )
+ {
+ m_doc = doc;
+ m_monitor = monitor;
+ }
+
+ public void buildRealm( MutableRealm realm ) throws IOException
+ {
+ Element root = m_doc.getRootElement();
+ Element users = root.element( "users" );
+ addUsers( realm, users );
+ }
+
+ private void addUsers( MutableRealm realm, Element users )
+ {
+ List userList = users.elements( "user" );
+
+ for ( Iterator it = userList.iterator(); it.hasNext(); )
+ {
+ final Element user = (Element) it.next();
+ CredentialSet creds = new CredentialSet();
+ String username = user.attributeValue( "username" );
+ creds.add( new Credential( "username", username ) );
+ String password = user.attributeValue( "password" );
+ creds.add( new Credential( "password", password ) );
+
+ boolean added = realm.addIdentity( creds );
+ if ( !added ) m_monitor.duplicateIdentity( creds );
+ }
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/NullRealmBuilderMonitor.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/NullRealmBuilderMonitor.java Mon Feb 23 21:22:40 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.script.xml;
+
+import org.apache.janus.authentication.CredentialSet;
+import org.apache.janus.script.RealmBuilderMonitor;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class NullRealmBuilderMonitor implements RealmBuilderMonitor
+{
+ public void duplicateIdentity( CredentialSet identity )
+ {
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/XMLRealm.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/XMLRealm.java Mon Feb 23 21:22:40 2004
@@ -0,0 +1,57 @@
+/*
+ * 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.script.xml;
+
+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.Realm;
+import org.apache.janus.authentication.realm.UsernamePasswordAuthentication;
+import org.apache.janus.script.RealmBuilder;
+import org.apache.janus.script.RealmBuilderMonitor;
+import org.dom4j.Document;
+import org.dom4j.io.SAXReader;
+
+import java.io.Reader;
+import java.security.Principal;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class XMLRealm implements Realm
+{
+ private final MutableRealm m_delegate;
+
+ public XMLRealm( Reader reader ) throws Exception
+ {
+ this( reader, new NullRealmBuilderMonitor() );
+ }
+
+ public XMLRealm( Reader reader, RealmBuilderMonitor monitor ) throws Exception
+ {
+ SAXReader xmlReader = new SAXReader();
+ Document root = xmlReader.read( reader );
+ RealmBuilder builder = new Dom4JRealmBuilder( root, monitor );
+ m_delegate = new DefaultRealm( new UsernamePasswordAuthentication() );
+ builder.buildRealm( m_delegate );
+ }
+
+ public Principal validateCredentials( CredentialSet credentials )
+ {
+ return m_delegate.validateCredentials( credentials );
+ }
+}
Modified: 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/DefaultRealmTest.java (original)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/realm/DefaultRealmTest.java Mon Feb 23 21:22:40 2004
@@ -43,7 +43,7 @@
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() ) );
+ assertNull( "Empty credential set was validated", realm.validateCredentials( joeCredentials() ) );
}
public void testEmptyRealmNeverValidates()
@@ -53,7 +53,7 @@
realm = new DefaultRealm( (AuthenticationMethod) mockAuthenticationMethod.proxy() );
assertNull( "Principal was returned but realm contains no entry",
- realm.validate( johnCredentials() ) );
+ realm.validateCredentials( johnCredentials() ) );
}
@@ -65,7 +65,7 @@
realm = new DefaultRealm( (AuthenticationMethod) mockAuthenticationMethod.proxy() );
assertNull( "Principal was returned but credentials are invalid",
- realm.validate( johnCredentials() ) );
+ realm.validateCredentials( johnCredentials() ) );
}
@@ -89,7 +89,7 @@
assertEquals( "Principal identified does not match credentials",
jane(),
- realm.validate( janeCredentials() ) );
+ realm.validateCredentials( janeCredentials() ) );
}
Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java Mon Feb 23 21:22:40 2004
@@ -0,0 +1,115 @@
+/*
+ * 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.script.xml;
+
+import com.mockobjects.dynamic.Mock;
+import junit.framework.TestCase;
+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.UsernamePasswordAuthentication;
+import org.apache.janus.script.RealmBuilderMonitor;
+import org.dom4j.Document;
+import org.dom4j.Element;
+import org.dom4j.tree.DefaultDocument;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class Dom4JRealmBuilderTest extends TestCase
+{
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( Dom4JRealmBuilderTest.class );
+ }
+
+ protected void setUp() throws Exception
+ {
+ }
+
+ public void testSimpleBuild() throws Exception
+ {
+ Dom4JRealmBuilder builder = new Dom4JRealmBuilder( simpleRealm(), new NullRealmBuilderMonitor() );
+
+ Mock mockRealm = new Mock( MutableRealm.class );
+ mockRealm.expectAndReturn( "addIdentity", johnCredentials(), true );
+ mockRealm.expectAndReturn( "addIdentity", janeCredentials(), true );
+
+ builder.buildRealm( (MutableRealm) mockRealm.proxy() );
+
+ mockRealm.verify();
+ }
+
+ private Document simpleRealm()
+ {
+ Document doc = new DefaultDocument();
+ Element root = doc.addElement( "realm" );
+ Element users = root.addElement( "users" );
+ Element john = users.addElement( "user" );
+ john.addAttribute( "username", "john" );
+ john.addAttribute( "password", "doe" );
+ Element jane = users.addElement( "user" );
+ jane.addAttribute( "username", "jane" );
+ jane.addAttribute( "password", "doe" );
+
+ return doc;
+ }
+
+ private CredentialSet johnCredentials()
+ {
+ CredentialSet johnCredentials = new CredentialSet();
+ johnCredentials.add( new Credential( "username", "john" ) );
+ johnCredentials.add( new Credential( "password", "doe" ) );
+ return johnCredentials;
+ }
+
+ private CredentialSet janeCredentials()
+ {
+ CredentialSet johnCredentials = new CredentialSet();
+ johnCredentials.add( new Credential( "username", "jane" ) );
+ johnCredentials.add( new Credential( "password", "doe" ) );
+ return johnCredentials;
+ }
+
+ public void testNotifiesOfDuplicateIdentities() throws Exception
+ {
+ Mock mockMonitor = new Mock( RealmBuilderMonitor.class );
+ Dom4JRealmBuilder builder = new Dom4JRealmBuilder( realmWithDuplicateIdentity(),
+ (RealmBuilderMonitor) mockMonitor.proxy() );
+
+ mockMonitor.expect( "duplicateIdentity", johnCredentials() );
+ builder.buildRealm( new DefaultRealm( new UsernamePasswordAuthentication() ) );
+
+ mockMonitor.verify();
+ }
+
+ private Document realmWithDuplicateIdentity()
+ {
+ Document doc = new DefaultDocument();
+ Element root = doc.addElement( "realm" );
+ Element users = root.addElement( "users" );
+ Element john = users.addElement( "user" );
+ john.addAttribute( "username", "john" );
+ john.addAttribute( "password", "doe" );
+ Element jane = users.addElement( "user" );
+ jane.addAttribute( "username", "john" );
+ jane.addAttribute( "password", "doe" );
+
+ return doc;
+ }
+}
Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/XMLRealmTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/XMLRealmTest.java Mon Feb 23 21:22:40 2004
@@ -0,0 +1,98 @@
+/*
+ * 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.script.xml;
+
+import junit.framework.TestCase;
+import org.apache.janus.authentication.Credential;
+import org.apache.janus.authentication.CredentialSet;
+import org.apache.janus.authentication.realm.UsernamePrincipal;
+
+import java.io.StringReader;
+
+/**
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class XMLRealmTest extends TestCase
+{
+ private XMLRealm m_realm;
+
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( XMLRealmTest.class );
+ }
+
+ public void testBuildsRealmFromXMLDocument() throws Exception
+ {
+ m_realm = new XMLRealm( new StringReader( simpleRealm() ) );
+
+ assertEquals( "Could not validateCredentials identity; must be missing in realm", new UsernamePrincipal(
+ "john" ),
+ m_realm.validateCredentials( johnCredentials() ) );
+ assertEquals( "Could not validateCredentials identity; must be missing in realm", new UsernamePrincipal(
+ "jane" ),
+ m_realm.validateCredentials( janeCredentials() ) );
+ }
+
+ private String simpleRealm()
+ {
+ String content = "<?xml version=\"1.0\"?>\n"
+ + "<realm>\n"
+ + " <users>\n"
+ + " <user username=\"john\" password=\"doe\"/>\n"
+ + " <user username=\"jane\" password=\"doe\"/>\n"
+ + " </users>\n"
+ + "</realm>";
+ return content;
+ }
+
+ private CredentialSet johnCredentials()
+ {
+ CredentialSet johnCredentials = new CredentialSet();
+ johnCredentials.add( new Credential( "username", "john" ) );
+ johnCredentials.add( new Credential( "password", "doe" ) );
+ return johnCredentials;
+ }
+
+ private CredentialSet janeCredentials()
+ {
+ CredentialSet janeCredentials = new CredentialSet();
+ janeCredentials.add( new Credential( "username", "jane" ) );
+ janeCredentials.add( new Credential( "password", "doe" ) );
+ return janeCredentials;
+ }
+
+ public void testIgnoresDuplicateIdentities() throws Exception
+ {
+ m_realm = new XMLRealm( new StringReader( realmWithDuplicateIdentities() ) );
+
+ assertEquals( "Could not validateCredentials identity; must be missing in realm", new UsernamePrincipal(
+ "john" ),
+ m_realm.validateCredentials( johnCredentials() ) );
+ }
+
+ private String realmWithDuplicateIdentities()
+ {
+ String content = "<?xml version=\"1.0\"?>\n"
+ + "<realm>\n"
+ + " <users>\n"
+ + " <user username=\"john\" password=\"doe\"/>\n"
+ + " <user username=\"john\" password=\"doe\"/>\n"
+ + " </users>\n"
+ + "</realm>";
+ return content;
+ }
+}