Improved Name Mapper tests.
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Tue, 20 Jan 2004 23:25:59 +0000 (23:25 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Tue, 20 Jan 2004 23:25:59 +0000 (23:25 +0000)
Added default Name Mapping to Name Mapper.

git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@828 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/common/NameMapper.java
src/edu/internet2/middleware/shibboleth/hs/HSNameMapper.java
src/edu/internet2/middleware/shibboleth/hs/provider/SharedMemoryShibHandle.java
tests/edu/internet2/middleware/shibboleth/hs/provider/NameMapperTests.java

index 391162c..9ebb65f 100644 (file)
@@ -47,6 +47,7 @@
 
 package edu.internet2.middleware.shibboleth.common;
 
+import java.io.StringReader;
 import java.lang.reflect.Constructor;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -55,8 +56,10 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.log4j.Logger;
+import org.apache.xerces.parsers.DOMParser;
 import org.opensaml.SAMLNameIdentifier;
 import org.w3c.dom.Element;
+import org.xml.sax.InputSource;
 
 import edu.internet2.middleware.shibboleth.hs.provider.SharedMemoryShibHandle;
 
@@ -66,9 +69,10 @@ import edu.internet2.middleware.shibboleth.hs.provider.SharedMemoryShibHandle;
 public class NameMapper {
 
        private static Logger log = Logger.getLogger(NameMapper.class.getName());
-       private Map byFormat = new HashMap();
+       protected Map byFormat = new HashMap();
        private static Map registeredMappingTypes = Collections.synchronizedMap(new HashMap());
        protected boolean initialized = false;
+       protected SharedMemoryShibHandle defaultMapping;
 
        static {
                try {
@@ -89,6 +93,27 @@ public class NameMapper {
                }
        }
 
+       public NameMapper() {
+               try {
+                       String rawConfig =
+                               "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+                                       + "<NameMapping format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
+                                       + "             handleTTL=\"1800\"/>";
+                       DOMParser parser = new DOMParser();
+                       parser.parse(new InputSource(new StringReader(rawConfig)));
+                       defaultMapping = new SharedMemoryShibHandle(parser.getDocument().getDocumentElement());
+
+               } catch (Exception e) {
+                       log.error("Unable to register default Name Identifier Mapping.");
+                       initialize();
+               }
+       }
+
+       protected void initialize() {
+               initialized = true;
+               defaultMapping = null;
+       }
+
        public void addNameMapping(Element e) throws NameIdentifierMappingException {
 
                if (!e.getTagName().equals("NameMapping")) {
@@ -137,7 +162,8 @@ public class NameMapper {
 
        public void addNameMapping(NameIdentifierMapping mapping) {
 
-               initialized = true;
+               initialize();
+
                if (byFormat.containsKey(mapping.getNameIdentifierFormat())) {
                        log.error("Attempted to register multiple Name Mappings with the same format.  Skipping duplicates...");
                        return;
@@ -148,11 +174,7 @@ public class NameMapper {
 
        public NameIdentifierMapping getNameIdentifierMapping(URI format) {
                if (!initialized) {
-                       try {
-                               return new SharedMemoryShibHandle(null);
-                       } catch (NameIdentifierMappingException e) {
-                               return null;
-                       }
+                       return defaultMapping;
                }
                return (NameIdentifierMapping) byFormat.get(format);
        }
index b1a26fe..aa13549 100644 (file)
@@ -47,6 +47,7 @@
 package edu.internet2.middleware.shibboleth.hs;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
 import org.opensaml.SAMLNameIdentifier;
@@ -77,6 +78,21 @@ public class HSNameMapper extends NameMapper {
        }
 
        public HSNameIdentifierMapping getNameIdentifierMappingById(String id) {
+
+               if (id == null || id.equals("")) {
+                       if (!initialized) {
+                               return defaultMapping;
+                       }
+
+                       if (byFormat.size() == 1) {
+                               Iterator values = byFormat.values().iterator();
+                               Object mapping = values.next();
+                               if (mapping instanceof HSNameIdentifierMapping) {
+                                       return (HSNameIdentifierMapping) mapping;
+                               }
+                       }
+               }
+
                return (HSNameIdentifierMapping) byId.get(id);
        }
 
index 76ba2b8..40ccbd6 100644 (file)
@@ -76,7 +76,6 @@ public class SharedMemoryShibHandle extends AQHNameIdentifierMapping implements
 
        public SharedMemoryShibHandle(Element config) throws NameIdentifierMappingException {
                super(config);
-               //If we add anything to this constructor, 
        }
 
        public SAMLNameIdentifier getNameIdentifierName(
index 50aa7f9..1ab6862 100644 (file)
@@ -72,6 +72,7 @@ import edu.internet2.middleware.shibboleth.common.IdentityProvider;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
 import edu.internet2.middleware.shibboleth.common.NameMapper;
 import edu.internet2.middleware.shibboleth.common.ServiceProvider;
+import edu.internet2.middleware.shibboleth.hs.HSNameIdentifierMapping;
 import edu.internet2.middleware.shibboleth.hs.HSNameMapper;
 
 /**
@@ -82,20 +83,19 @@ import edu.internet2.middleware.shibboleth.hs.HSNameMapper;
 
 public class NameMapperTests extends TestCase {
 
-       private static Logger log = Logger.getLogger(NameMapperTests.class.getName());
        private DOMParser parser = new DOMParser();
 
        public NameMapperTests(String name) {
                super(name);
                BasicConfigurator.resetConfiguration();
                BasicConfigurator.configure();
-               Logger.getRootLogger().setLevel(Level.DEBUG);
+               Logger.getRootLogger().setLevel(Level.OFF);
        }
 
        public static void main(String[] args) {
                junit.textui.TestRunner.run(NameMapperTests.class);
                BasicConfigurator.configure();
-               Logger.getRootLogger().setLevel(Level.DEBUG);
+               Logger.getRootLogger().setLevel(Level.OFF);
        }
 
        protected void setUp() throws Exception {
@@ -164,9 +164,14 @@ public class NameMapperTests extends TestCase {
                        nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
 
                        SAMLNameIdentifier nameId =
-                               nameMapper.getNameIdentifierName("cryptotest", new AuthNPrincipal("testprincipal"), null, null);
+                               nameMapper.getNameIdentifierName(
+                                       "cryptotest",
+                                       new AuthNPrincipal("testprincipal"),
+                                       new BasicServiceProvider(),
+                                       new BasicIdentityProvider("urn-x:testid"));
 
-                       AuthNPrincipal principal = nameMapper.getPrincipal(nameId, null, null);
+                       AuthNPrincipal principal =
+                               nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
                        assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
 
                } catch (MalformedURLException e) {
@@ -183,19 +188,52 @@ public class NameMapperTests extends TestCase {
                try {
 
                        HSNameMapper nameMapper = new HSNameMapper();
-/*
+
+                       SAMLNameIdentifier nameId =
+                               nameMapper.getNameIdentifierName(
+                                       null,
+                                       new AuthNPrincipal("testprincipal"),
+                                       new BasicServiceProvider(),
+                                       new BasicIdentityProvider("urn-x:testid"));
+
+                       AuthNPrincipal principal =
+                               nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
+
+                       assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
+
+               } catch (NameIdentifierMappingException e) {
+                       fail("Error exercising NameMaper: " + e.getMessage());
+               } catch (Exception e) {
+                       fail("Error exercising NameMaper: " + e.getMessage());
+               }
+       }
+
+       public void testDefaultingId() {
+
+               try {
+
+                       HSNameMapper nameMapper = new HSNameMapper();
+
+                       File file = new File("data/handle.jks");
+
                        String rawConfig =
                                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
-                                       + "<NameMapping id=\"memorytest\" format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
-                                       + "             type=\"SharedMemoryShibHandle\" handleTTL=\"1800\"/>";
-*/
-                       //parser.parse(new InputSource(new StringReader(rawConfig)));
-                       //nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
-                       
-                       //TODO fix
+                                       + "<NameMapping format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
+                                       + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
+                                       + "             <KeyStorePath>"
+                                       + file.toURL().toString()
+                                       + "</KeyStorePath>"
+                                       + "             <KeyStorePassword>shibhs</KeyStorePassword>"
+                                       + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
+                                       + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
+                                       + "     </NameMapping>";
+
+                       parser.parse(new InputSource(new StringReader(rawConfig)));
+                       nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
+
                        SAMLNameIdentifier nameId =
                                nameMapper.getNameIdentifierName(
-                                       "",
+                                       null,
                                        new AuthNPrincipal("testprincipal"),
                                        new BasicServiceProvider(),
                                        new BasicIdentityProvider("urn-x:testid"));
@@ -205,12 +243,73 @@ public class NameMapperTests extends TestCase {
 
                        assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
 
+                       HSNameIdentifierMapping nameMapping = nameMapper.getNameIdentifierMappingById(null);
+                       if (!(nameMapping instanceof CryptoShibHandle)) {
+                               fail("HSNameMapper defaulted to incorrect name mapping.");
+                       }
+
                } catch (NameIdentifierMappingException e) {
                        fail("Error exercising NameMaper: " + e.getMessage());
                } catch (Exception e) {
                        fail("Error exercising NameMaper: " + e.getMessage());
                }
        }
+
+       public void testDefaultingAmbiguousId() {
+
+               try {
+
+                       HSNameMapper nameMapper = new HSNameMapper();
+
+                       File file = new File("data/handle.jks");
+
+                       String rawConfig =
+                               "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+                                       + "<NameMapping format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
+                                       + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
+                                       + "             <KeyStorePath>"
+                                       + file.toURL().toString()
+                                       + "</KeyStorePath>"
+                                       + "             <KeyStorePassword>shibhs</KeyStorePassword>"
+                                       + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
+                                       + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
+                                       + "     </NameMapping>";
+
+                       parser.parse(new InputSource(new StringReader(rawConfig)));
+                       nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
+
+                       String rawConfig2 =
+                               "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+                                       + "<NameMapping format=\"urn-x:testNameIdentifier\""
+                                       + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
+                                       + "             <KeyStorePath>"
+                                       + file.toURL().toString()
+                                       + "</KeyStorePath>"
+                                       + "             <KeyStorePassword>shibhs</KeyStorePassword>"
+                                       + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
+                                       + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
+                                       + "     </NameMapping>";
+
+                       parser.parse(new InputSource(new StringReader(rawConfig2)));
+
+                       nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
+
+                       nameMapper.getNameIdentifierName(
+                               null,
+                               new AuthNPrincipal("testprincipal"),
+                               new BasicServiceProvider(),
+                               new BasicIdentityProvider("urn-x:testid"));
+
+                       fail("HSNameMapper defaulted to incorrect name mapping.");
+
+                       //This is only a failure if we don't get this exception
+               } catch (NameIdentifierMappingException e) {
+
+               } catch (Exception e) {
+
+                       fail("Error exercising NameMaper: " + e.getMessage());
+               }
+       }
        public void testMemoryMapping() {
 
                try {
@@ -218,22 +317,22 @@ public class NameMapperTests extends TestCase {
                        HSNameMapper nameMapper = new HSNameMapper();
 
                        String rawConfig =
-                       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
-                       + "<NameMapping id=\"memorytest\" format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
-                       + "             type=\"SharedMemoryShibHandle\" handleTTL=\"1800\"/>";
+                               "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+                                       + "<NameMapping id=\"memorytest\" format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
+                                       + "             type=\"SharedMemoryShibHandle\" handleTTL=\"1800\"/>";
 
                        parser.parse(new InputSource(new StringReader(rawConfig)));
                        nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
 
                        SAMLNameIdentifier nameId =
-                       nameMapper.getNameIdentifierName(
+                               nameMapper.getNameIdentifierName(
                                        "memorytest",
                                        new AuthNPrincipal("testprincipal"),
                                        new BasicServiceProvider(),
                                        new BasicIdentityProvider("urn-x:testid"));
 
                        AuthNPrincipal principal =
-                       nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
+                               nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
 
                        assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");