2 * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3 * All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted
4 * provided that the following conditions are met: Redistributions of source code must retain the above copyright
5 * notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above
6 * copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials
7 * provided with the distribution, if any, must include the following acknowledgment: "This product includes software
8 * developed by the University Corporation for Advanced Internet Development <http://www.ucaid.edu> Internet2 Project.
9 * Alternately, this acknowledegement may appear in the software itself, if and wherever such third-party
10 * acknowledgments normally appear. Neither the name of Shibboleth nor the names of its contributors, nor Internet2, nor
11 * the University Corporation for Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote
12 * products derived from this software without specific prior written permission. For written permission, please contact
13 * shibboleth@shibboleth.org Products derived from this software may not be called Shibboleth, Internet2, UCAID, or the
14 * University Corporation for Advanced Internet Development, nor may Shibboleth appear in their name, without prior
15 * written permission of the University Corporation for Advanced Internet Development. THIS SOFTWARE IS PROVIDED BY THE
16 * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE
18 * DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. IN NO
19 * EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC.
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 package edu.internet2.middleware.shibboleth.common;
28 import java.net.MalformedURLException;
30 import java.net.URISyntaxException;
32 import java.util.HashMap;
35 import org.apache.log4j.Logger;
36 import org.w3c.dom.Element;
37 import org.w3c.dom.NodeList;
39 import edu.internet2.middleware.shibboleth.idp.IdPConfig;
40 import edu.internet2.middleware.shibboleth.metadata.EntitiesDescriptor;
41 import edu.internet2.middleware.shibboleth.metadata.Metadata;
42 import edu.internet2.middleware.shibboleth.metadata.EntityDescriptor;
45 * Class for determining the effective relying party from the unique id of the service provider. Checks first for an
46 * exact match on the service provider, then for membership in a federation. Uses the default relying party if neither
49 * @author Walter Hoehn
51 public class ServiceProviderMapper {
53 private static Logger log = Logger.getLogger(ServiceProviderMapper.class.getName());
54 protected Map relyingParties = new HashMap();
55 private Metadata metaData;
56 private IdPConfig configuration;
57 private Credentials credentials;
58 private NameMapper nameMapper;
60 public ServiceProviderMapper(Element rawConfig, IdPConfig configuration, Credentials credentials,
61 NameMapper nameMapper, Metadata metaData) throws ServiceProviderMapperException {
63 this.metaData = metaData;
64 this.configuration = configuration;
65 this.credentials = credentials;
66 this.nameMapper = nameMapper;
68 NodeList itemElements = rawConfig.getElementsByTagNameNS(IdPConfig.originConfigNamespace, "RelyingParty");
70 for (int i = 0; i < itemElements.getLength(); i++) {
71 addRelyingParty((Element) itemElements.item(i));
74 verifyDefaultParty(configuration);
78 private IdPConfig getOriginConfig() {
83 protected void verifyDefaultParty(IdPConfig configuration) throws ServiceProviderMapperException {
85 // Verify we have a proper default party
86 String defaultParty = configuration.getDefaultRelyingPartyName();
87 if (defaultParty == null || defaultParty.equals("")) {
88 if (relyingParties.size() != 1) {
90 .error("Default Relying Party not specified. Add a (defaultRelyingParty) attribute to <IdPConfig>.");
91 throw new ServiceProviderMapperException("Required configuration not specified.");
93 log.debug("Only one Relying Party loaded. Using this as the default.");
96 log.debug("Default Relying Party set to: (" + defaultParty + ").");
97 if (!relyingParties.containsKey(defaultParty)) {
98 log.error("Default Relying Party refers to a Relying Party that has not been loaded.");
99 throw new ServiceProviderMapperException("Invalid configuration (Default Relying Party).");
103 protected RelyingParty getRelyingPartyImpl(String providerIdFromTarget) {
105 // Null request, send the default
106 if (providerIdFromTarget == null) {
107 RelyingParty relyingParty = getDefaultRelyingParty();
108 log.info("Using default Relying Party: (" + relyingParty.getName() + ").");
109 return new UnknownProviderWrapper(relyingParty, providerIdFromTarget);
112 // Look for a configuration for the specific relying party
113 if (relyingParties.containsKey(providerIdFromTarget)) {
114 log.info("Found Relying Party for (" + providerIdFromTarget + ").");
115 return (RelyingParty) relyingParties.get(providerIdFromTarget);
118 // Next, check to see if the relying party is in any groups
119 RelyingParty groupParty = findRelyingPartyByGroup(providerIdFromTarget);
120 if (groupParty != null) {
121 log.info("Provider is a member of Relying Party (" + groupParty.getName() + ").");
122 return new RelyingPartyGroupWrapper(groupParty, providerIdFromTarget);
125 // OK, we can't find it... just send the default
126 RelyingParty relyingParty = getDefaultRelyingParty();
127 log.info("Could not locate Relying Party configuration for (" + providerIdFromTarget
128 + "). Using default Relying Party: (" + relyingParty.getName() + ").");
129 return new UnknownProviderWrapper(relyingParty, providerIdFromTarget);
132 private RelyingParty findRelyingPartyByGroup(String providerIdFromTarget) {
134 EntityDescriptor provider = metaData.lookup(providerIdFromTarget);
135 if (provider != null) {
136 EntitiesDescriptor parent = provider.getEntitiesDescriptor();
137 while (parent != null) {
138 if (relyingParties.containsKey(parent.getName())) {
139 log.info("Found matching Relying Party for group (" + parent.getName() + ").");
140 return (RelyingParty)relyingParties.get(parent.getName());
142 log.debug("Provider is a member of group (" +
144 "), but no matching Relying Party was found.");
146 parent = parent.getEntitiesDescriptor();
152 public RelyingParty getDefaultRelyingParty() {
154 // If there is no explicit default, pick the single configured Relying
156 String defaultParty = getOriginConfig().getDefaultRelyingPartyName();
157 if (defaultParty == null || defaultParty.equals("")) { return (RelyingParty) relyingParties.values().iterator()
160 // If we do have a default specified, use it...
161 return (RelyingParty) relyingParties.get(defaultParty);
165 * Returns the relying party for a legacy provider(the default)
167 public RelyingParty getLegacyRelyingParty() {
169 RelyingParty relyingParty = getDefaultRelyingParty();
170 log.info("Request is from legacy shib target. Selecting default Relying Party: (" + relyingParty.getName()
172 return new LegacyWrapper((RelyingParty) relyingParty);
177 * Returns the appropriate relying party for the supplied service provider id.
179 public RelyingParty getRelyingParty(String providerIdFromTarget) {
181 if (providerIdFromTarget == null || providerIdFromTarget.equals("")) {
182 RelyingParty relyingParty = getDefaultRelyingParty();
183 log.info("Selecting default Relying Party: (" + relyingParty.getName() + ").");
184 return new NoMetadataWrapper((RelyingParty) relyingParty);
187 return (RelyingParty) getRelyingPartyImpl(providerIdFromTarget);
190 private void addRelyingParty(Element e) throws ServiceProviderMapperException {
192 log.debug("Found a Relying Party.");
194 if (e.getLocalName().equals("RelyingParty")) {
195 RelyingParty party = new RelyingPartyImpl(e, configuration, credentials, nameMapper);
196 log.debug("Relying Party (" + party.getName() + ") loaded.");
197 relyingParties.put(party.getName(), party);
199 } catch (ServiceProviderMapperException exc) {
200 log.error("Encountered an error while attempting to load Relying Party configuration. Skipping...");
206 * Base relying party implementation.
208 * @author Walter Hoehn
210 protected class RelyingPartyImpl implements RelyingParty {
212 private RelyingPartyIdentityProvider identityProvider;
214 private String overridenOriginProviderId;
215 private URL overridenAAUrl;
216 private URI overridenDefaultAuthMethod;
217 private String hsNameFormatId;
218 private IdPConfig configuration;
219 private boolean overridenPassThruErrors = false;
220 private boolean passThruIsOverriden = false;
222 public RelyingPartyImpl(Element partyConfig, IdPConfig globalConfig, Credentials credentials,
223 NameMapper nameMapper) throws ServiceProviderMapperException {
225 configuration = globalConfig;
228 name = ((Element) partyConfig).getAttribute("name");
229 if (name == null || name.equals("")) {
230 log.error("Relying Party name not set. Add a (name) attribute to <RelyingParty>.");
231 throw new ServiceProviderMapperException("Required configuration not specified.");
233 log.debug("Loading Relying Party: (" + name + ").");
235 // Process overrides for global configuration data
236 String attribute = ((Element) partyConfig).getAttribute("providerId");
237 if (attribute != null && !attribute.equals("")) {
238 log.debug("Overriding providerId for Relying Pary (" + name + ") with (" + attribute + ").");
239 overridenOriginProviderId = attribute;
242 attribute = ((Element) partyConfig).getAttribute("AAUrl");
243 if (attribute != null && !attribute.equals("")) {
244 log.debug("Overriding AAUrl for Relying Pary (" + name + ") with (" + attribute + ").");
246 overridenAAUrl = new URL(attribute);
247 } catch (MalformedURLException e) {
248 log.error("(AAUrl) attribute to is not a valid URL.");
249 throw new ServiceProviderMapperException("Configuration is invalid.");
253 attribute = ((Element) partyConfig).getAttribute("defaultAuthMethod");
254 if (attribute != null && !attribute.equals("")) {
255 log.debug("Overriding defaultAuthMethod for Relying Pary (" + name + ") with (" + attribute + ").");
257 overridenDefaultAuthMethod = new URI(attribute);
258 } catch (URISyntaxException e1) {
259 log.error("(defaultAuthMethod) attribute to is not a valid URI.");
260 throw new ServiceProviderMapperException("Configuration is invalid.");
264 attribute = ((Element) partyConfig).getAttribute("passThruErrors");
265 if (attribute != null && !attribute.equals("")) {
266 log.debug("Overriding passThruErrors for Relying Pary (" + name + ") with (" + attribute + ").");
267 overridenPassThruErrors = Boolean.valueOf(attribute).booleanValue();
268 passThruIsOverriden = true;
271 // Load and verify the name format that the HS should use in
272 // assertions for this RelyingParty
273 NodeList hsNameFormats = ((Element) partyConfig).getElementsByTagNameNS(IdPConfig.originConfigNamespace,
275 // If no specification. Make sure we have a default mapping
276 if (hsNameFormats.getLength() < 1) {
277 if (nameMapper.getNameIdentifierMappingById(null) == null) {
278 log.error("Relying Party HS Name Format not set. Add a <HSNameFormat> element to <RelyingParty>.");
279 throw new ServiceProviderMapperException("Required configuration not specified.");
283 // We do have a specification, so make sure it points to a
284 // valid Name Mapping
285 if (hsNameFormats.getLength() > 1) {
286 log.warn("Found multiple HSNameFormat specifications for Relying Party (" + name
287 + "). Ignoring all but the first.");
290 hsNameFormatId = ((Element) hsNameFormats.item(0)).getAttribute("nameMapping");
291 if (hsNameFormatId == null || hsNameFormatId.equals("")) {
292 log.error("HS Name Format mapping not set. Add a (nameMapping) attribute to <HSNameFormat>.");
293 throw new ServiceProviderMapperException("Required configuration not specified.");
296 if (nameMapper.getNameIdentifierMappingById(hsNameFormatId) == null) {
297 log.error("Relying Party HS Name Format refers to a name mapping that is not loaded.");
298 throw new ServiceProviderMapperException("Required configuration not specified.");
302 // Load the credential for signing
303 String credentialName = ((Element) partyConfig).getAttribute("signingCredential");
304 Credential signingCredential = credentials.getCredential(credentialName);
305 if (signingCredential == null) {
306 if (credentialName == null || credentialName.equals("")) {
307 log.error("Relying Party credential not set. Add a (signingCredential) "
308 + "attribute to <RelyingParty>.");
309 throw new ServiceProviderMapperException("Required configuration not specified.");
311 log.error("Relying Party credential invalid. Fix the (signingCredential) attribute "
312 + "on <RelyingParty>.");
313 throw new ServiceProviderMapperException("Required configuration is invalid.");
318 // Initialize and Identity Provider object for this use by this relying party
319 identityProvider = new RelyingPartyIdentityProvider(overridenOriginProviderId != null
320 ? overridenOriginProviderId
321 : configuration.getProviderId(), signingCredential);
325 public String getProviderId() {
330 public String getName() {
335 public IdentityProvider getIdentityProvider() {
337 return identityProvider;
340 public boolean isLegacyProvider() {
345 public String getHSNameFormatId() {
347 return hsNameFormatId;
350 public URI getDefaultAuthMethod() {
352 if (overridenDefaultAuthMethod != null) {
353 return overridenDefaultAuthMethod;
355 return configuration.getDefaultAuthMethod();
359 public URL getAAUrl() {
361 if (overridenAAUrl != null) {
362 return overridenAAUrl;
364 return configuration.getAAUrl();
368 public boolean passThruErrors() {
370 if (passThruIsOverriden) {
371 return overridenPassThruErrors;
373 return configuration.passThruErrors();
378 * Default identity provider implementation.
380 * @author Walter Hoehn
382 protected class RelyingPartyIdentityProvider implements IdentityProvider {
384 private String providerId;
385 private Credential credential;
387 public RelyingPartyIdentityProvider(String providerId, Credential credential) {
389 this.providerId = providerId;
390 this.credential = credential;
394 * @see edu.internet2.middleware.shibboleth.common.IdentityProvider#getProviderId()
396 public String getProviderId() {
402 * @see edu.internet2.middleware.shibboleth.common.IdentityProvider#getSigningCredential()
404 public Credential getSigningCredential() {
412 * Relying party implementation wrapper for relying parties that are federations.
414 * @author Walter Hoehn
416 class RelyingPartyGroupWrapper implements RelyingParty {
418 private RelyingParty wrapped;
419 private String providerId;
421 RelyingPartyGroupWrapper(RelyingParty wrapped, String providerId) {
423 this.wrapped = wrapped;
424 this.providerId = providerId;
427 public String getName() {
429 return wrapped.getName();
432 public boolean isLegacyProvider() {
437 public IdentityProvider getIdentityProvider() {
439 return wrapped.getIdentityProvider();
442 public String getProviderId() {
447 public String getHSNameFormatId() {
449 return wrapped.getHSNameFormatId();
452 public URL getAAUrl() {
454 return wrapped.getAAUrl();
457 public URI getDefaultAuthMethod() {
459 return wrapped.getDefaultAuthMethod();
462 public boolean passThruErrors() {
464 return wrapped.passThruErrors();
469 * Relying party implementation wrapper for anonymous service providers.
471 * @author Walter Hoehn
473 protected class UnknownProviderWrapper implements RelyingParty {
475 protected RelyingParty wrapped;
476 protected String providerId;
478 protected UnknownProviderWrapper(RelyingParty wrapped, String providerId) {
480 this.wrapped = wrapped;
481 this.providerId = providerId;
484 public String getName() {
486 return wrapped.getName();
489 public IdentityProvider getIdentityProvider() {
491 return wrapped.getIdentityProvider();
494 public String getProviderId() {
499 public String getHSNameFormatId() {
501 return wrapped.getHSNameFormatId();
504 public boolean isLegacyProvider() {
506 return wrapped.isLegacyProvider();
509 public URL getAAUrl() {
511 return wrapped.getAAUrl();
514 public URI getDefaultAuthMethod() {
516 return wrapped.getDefaultAuthMethod();
519 public boolean passThruErrors() {
521 return wrapped.passThruErrors();
526 * Relying party wrapper for Shibboleth <=1.1 service providers.
528 * @author Walter Hoehn
530 class LegacyWrapper extends UnknownProviderWrapper implements RelyingParty {
532 LegacyWrapper(RelyingParty wrapped) {
534 super(wrapped, null);
537 public boolean isLegacyProvider() {
542 public String getHSNameFormatId() {
544 return ((RelyingParty) wrapped).getHSNameFormatId();
547 public URL getAAUrl() {
549 return ((RelyingParty) wrapped).getAAUrl();
552 public URI getDefaultAuthMethod() {
554 return ((RelyingParty) wrapped).getDefaultAuthMethod();
559 * Relying party wrapper for providers for which we have no metadata
561 * @author Walter Hoehn
563 class NoMetadataWrapper extends UnknownProviderWrapper implements RelyingParty {
565 NoMetadataWrapper(RelyingParty wrapped) {
567 super(wrapped, null);
570 public String getHSNameFormatId() {
572 return ((RelyingParty) wrapped).getHSNameFormatId();
575 public URL getAAUrl() {
577 return ((RelyingParty) wrapped).getAAUrl();
580 public URI getDefaultAuthMethod() {
582 return ((RelyingParty) wrapped).getDefaultAuthMethod();