More name mapping updates.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / NameMapper.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation
3  * for Advanced Internet Development, Inc. All rights reserved
4  * 
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 
9  * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution, if any, must include
15  * the following acknowledgment: "This product includes software developed by
16  * the University Corporation for Advanced Internet Development
17  * <http://www.ucaid.edu> Internet2 Project. Alternately, this acknowledegement
18  * may appear in the software itself, if and wherever such third-party
19  * acknowledgments normally appear.
20  * 
21  * Neither the name of Shibboleth nor the names of its contributors, nor
22  * Internet2, nor the University Corporation for Advanced Internet Development,
23  * Inc., nor UCAID may be used to endorse or promote products derived from this
24  * software without specific prior written permission. For written permission,
25  * please contact shibboleth@shibboleth.org
26  * 
27  * Products derived from this software may not be called Shibboleth, Internet2,
28  * UCAID, or the University Corporation for Advanced Internet Development, nor
29  * may Shibboleth appear in their name, without prior written permission of the
30  * University Corporation for Advanced Internet Development.
31  * 
32  * 
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
36  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
37  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
38  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
39  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY
40  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  */
47
48 package edu.internet2.middleware.shibboleth.common;
49
50 import java.lang.reflect.Constructor;
51 import java.net.URI;
52 import java.net.URISyntaxException;
53 import java.util.Collections;
54 import java.util.HashMap;
55 import java.util.Map;
56
57 import org.apache.log4j.Logger;
58 import org.opensaml.SAMLNameIdentifier;
59 import org.w3c.dom.Element;
60
61 import edu.internet2.middleware.shibboleth.hs.provider.SharedMemoryShibHandle;
62
63 /**
64  * @author Walter Hoehn
65  */
66 public class NameMapper {
67
68         private static Logger log = Logger.getLogger(NameMapper.class.getName());
69         private Map byFormat = new HashMap();
70         private static Map registeredMappingTypes = Collections.synchronizedMap(new HashMap());
71         protected boolean initialized = false;
72
73         static {
74                 try {
75                         registeredMappingTypes.put(
76                                 "CryptoHandleGenerator",
77                                 Class.forName("edu.internet2.middleware.shibboleth.hs.provider.CryptoShibHandle"));
78
79                         registeredMappingTypes.put(
80                                 "SharedMemoryShibHandle",
81                                 Class.forName("edu.internet2.middleware.shibboleth.hs.provider.SharedMemoryShibHandle"));
82
83                         registeredMappingTypes.put(
84                                 "Principal",
85                                 Class.forName("edu.internet2.middleware.shibboleth.common.PrincipalNameIdentifier"));
86
87                 } catch (ClassNotFoundException e) {
88                         log.error("Unable to pre-register Name mapping implementation types.");
89                 }
90         }
91
92         public void addNameMapping(Element e) throws NameIdentifierMappingException {
93
94                 if (!e.getTagName().equals("NameMapping")) {
95                         throw new IllegalArgumentException();
96                 }
97
98                 String type = ((Element) e).getAttribute("type");
99                 String implementation = ((Element) e).getAttribute("class");
100                 if (type != null && (!type.equals("")) && implementation != null && (!implementation.equals(""))) {
101                         log.error("Name Mapping has both a \"type\" and a \"class\" attribute. Only \"type\" will take effect.");
102                 }
103
104                 if (type != null && (!type.equals(""))) {
105
106                         Class registeredImplementation = (Class) registeredMappingTypes.get(type);
107                         if (registeredImplementation == null) {
108                                 log.error("Name Mapping refers to an unregistered implementation type.");
109                                 throw new NameIdentifierMappingException("Invalid mapping implementation specified.");
110                         }
111
112                         log.debug(
113                                 "Found type ("
114                                         + type
115                                         + ") registered with an implementation class of ("
116                                         + registeredImplementation.getName()
117                                         + ").");
118                         addNameMapping(loadNameIdentifierMapping(registeredImplementation, e));
119
120                 } else if (implementation != null && (!implementation.equals(""))) {
121
122                         try {
123                                 Class implementorClass = Class.forName(implementation);
124                                 addNameMapping(loadNameIdentifierMapping(implementorClass, e));
125
126                         } catch (ClassNotFoundException cnfe) {
127                                 log.error("Name Mapping refers to an implementation class that cannot be loaded: " + cnfe);
128                                 throw new NameIdentifierMappingException("Invalid mapping implementation specified.");
129                         }
130
131                 } else {
132                         log.error("Name Mapping requires either a \"type\" or a \"class\" attribute.");
133                         throw new NameIdentifierMappingException("No mapping implementation specified.");
134                 }
135
136         }
137
138         public void addNameMapping(NameIdentifierMapping mapping) {
139
140                 initialized = true;
141                 if (byFormat.containsKey(mapping.getNameIdentifierFormat())) {
142                         log.error("Attempted to register multiple Name Mappings with the same format.  Skipping duplicates...");
143                         return;
144                 }
145                 byFormat.put(mapping.getNameIdentifierFormat(), mapping);
146
147         }
148
149         public NameIdentifierMapping getNameIdentifierMapping(URI format) {
150                 if (!initialized) {
151                         try {
152                                 return new SharedMemoryShibHandle(null);
153                         } catch (NameIdentifierMappingException e) {
154                                 return null;
155                         }
156                 }
157                 return (NameIdentifierMapping) byFormat.get(format);
158         }
159
160         protected NameIdentifierMapping loadNameIdentifierMapping(Class implementation, Element config)
161                 throws NameIdentifierMappingException {
162
163                 try {
164                         Class[] params = new Class[] { Element.class };
165                         Constructor implementorConstructor = implementation.getConstructor(params);
166                         Object[] args = new Object[] { config };
167                         log.debug("Initializing Name Identifier Mapping of type (" + implementation.getName() + ").");
168                         return (NameIdentifierMapping) implementorConstructor.newInstance(args);
169
170                 } catch (NoSuchMethodException nsme) {
171                         log.error(
172                                 "Failed to instantiate a Name Identifier Mapping: NameIdentifierMapping "
173                                         + "implementation must contain a constructor that accepts an Element object for "
174                                         + "configuration data.");
175                         throw new NameIdentifierMappingException("Failed to instantiate a Name Identifier Mapping.");
176
177                 } catch (Exception e) {
178                         log.error("Failed to instantiate a Name Identifier Mapping: " + e + ":" + e.getCause());
179                         throw new NameIdentifierMappingException("Failed to instantiate a Name Identifier Mapping: " + e);
180
181                 }
182
183         }
184
185         public AuthNPrincipal getPrincipal(SAMLNameIdentifier nameId, ServiceProvider sProv, IdentityProvider idProv)
186                 throws NameIdentifierMappingException, InvalidNameIdentifierException {
187
188                 NameIdentifierMapping mapping = null;
189                 try {
190                         mapping = getNameIdentifierMapping(new URI(nameId.getFormat()));
191                 } catch (URISyntaxException e) {
192                         log.error("Invalid Name Identifier format.");
193                 }
194                 if (mapping == null) {
195                         throw new InvalidNameIdentifierException("Name Identifier format not registered.");
196                 }
197                 return mapping.getPrincipal(nameId, sProv, idProv);
198         }
199
200 }