2 * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package edu.internet2.middleware.shibboleth.common;
19 import java.net.MalformedURLException;
21 import java.net.URISyntaxException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import org.apache.log4j.Logger;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.NodeList;
32 import edu.internet2.middleware.shibboleth.idp.IdPConfig;
33 import edu.internet2.middleware.shibboleth.metadata.EntitiesDescriptor;
34 import edu.internet2.middleware.shibboleth.metadata.EntityDescriptor;
35 import edu.internet2.middleware.shibboleth.metadata.Metadata;
38 * Class for determining the effective relying party from the unique id of the service provider. Checks first for an
39 * exact match on the service provider, then for membership in a group of providers (perhaps a federation). Uses the
40 * default relying party if neither is found.
42 * @author Walter Hoehn
44 public class ServiceProviderMapper {
46 private static Logger log = Logger.getLogger(ServiceProviderMapper.class.getName());
47 protected Map relyingParties = new HashMap();
48 private Metadata metaData;
49 private IdPConfig configuration;
50 private Credentials credentials;
51 private NameMapper nameMapper;
53 public ServiceProviderMapper(Element rawConfig, IdPConfig configuration, Credentials credentials,
54 NameMapper nameMapper) throws ServiceProviderMapperException {
56 this.configuration = configuration;
57 this.credentials = credentials;
58 this.nameMapper = nameMapper;
60 NodeList itemElements = rawConfig.getElementsByTagNameNS(IdPConfig.configNameSpace, "RelyingParty");
62 for (int i = 0; i < itemElements.getLength(); i++) {
63 addRelyingParty((Element) itemElements.item(i));
66 verifyDefaultParty(configuration);
70 public void setMetadata(Metadata metadata) {
72 this.metaData = metadata;
75 private IdPConfig getIdPConfig() {
80 protected void verifyDefaultParty(IdPConfig configuration) throws ServiceProviderMapperException {
82 // Verify we have a proper default party
83 String defaultParty = configuration.getDefaultRelyingPartyName();
84 if (defaultParty == null || defaultParty.equals("")) {
85 if (relyingParties.size() != 1) {
87 .error("Default Relying Party not specified. Add a (defaultRelyingParty) attribute to <IdPConfig>.");
88 throw new ServiceProviderMapperException("Required configuration not specified.");
90 log.debug("Only one Relying Party loaded. Using this as the default.");
93 log.debug("Default Relying Party set to: (" + defaultParty + ").");
94 if (!relyingParties.containsKey(defaultParty)) {
95 log.error("Default Relying Party refers to a Relying Party that has not been loaded.");
96 throw new ServiceProviderMapperException("Invalid configuration (Default Relying Party).");
100 protected RelyingParty getRelyingPartyImpl(String providerIdFromSP) {
102 // Null request, send the default
103 if (providerIdFromSP == null) {
104 RelyingParty relyingParty = getDefaultRelyingParty();
105 log.info("Using default Relying Party: (" + relyingParty.getName() + ").");
106 return new UnknownProviderWrapper(relyingParty, providerIdFromSP);
109 // Look for a configuration for the specific relying party
110 if (relyingParties.containsKey(providerIdFromSP)) {
111 log.info("Found Relying Party for (" + providerIdFromSP + ").");
112 return (RelyingParty) relyingParties.get(providerIdFromSP);
115 // Next, check to see if the relying party is in any groups
116 RelyingParty groupParty = findRelyingPartyByGroup(providerIdFromSP);
117 if (groupParty != null) {
118 log.info("Provider is a member of Relying Party (" + groupParty.getName() + ").");
119 return new RelyingPartyGroupWrapper(groupParty, providerIdFromSP);
122 // OK, we can't find it... just send the default
123 RelyingParty relyingParty = getDefaultRelyingParty();
124 log.info("Could not locate Relying Party configuration for (" + providerIdFromSP
125 + "). Using default Relying Party: (" + relyingParty.getName() + ").");
126 return new UnknownProviderWrapper(relyingParty, providerIdFromSP);
129 private RelyingParty findRelyingPartyByGroup(String providerIdFromSP) {
131 if (metaData == null) { return null; }
133 EntityDescriptor provider = metaData.lookup(providerIdFromSP);
134 if (provider != null) {
135 EntitiesDescriptor parent = provider.getEntitiesDescriptor();
136 while (parent != null) {
137 if (parent.getName() != 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());
143 log.debug("Provider is a member of group (" + parent.getName()
144 + "), but no matching Relying Party was found.");
147 parent = parent.getEntitiesDescriptor();
153 public RelyingParty getDefaultRelyingParty() {
155 // If there is no explicit default, pick the single configured Relying
157 String defaultParty = getIdPConfig().getDefaultRelyingPartyName();
158 if (defaultParty == null || defaultParty.equals("")) { return (RelyingParty) relyingParties.values().iterator()
161 // If we do have a default specified, use it...
162 return (RelyingParty) relyingParties.get(defaultParty);
166 * Returns the relying party for a legacy provider(the default)
168 public RelyingParty getLegacyRelyingParty() {
170 RelyingParty relyingParty = getDefaultRelyingParty();
171 log.info("Request is from legacy shib SP. 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 providerIdFromSP) {
181 if (providerIdFromSP == null || providerIdFromSP.equals("")) {
182 RelyingParty relyingParty = getDefaultRelyingParty();
183 log.info("Selecting default Relying Party: (" + relyingParty.getName() + ").");
184 return new NoMetadataWrapper((RelyingParty) relyingParty);
187 return (RelyingParty) getRelyingPartyImpl(providerIdFromSP);
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 overridenIdPProviderId;
215 private URL overridenAAUrl;
216 private URI overridenDefaultAuthMethod;
217 private List mappingIds = new ArrayList();
218 private IdPConfig configuration;
219 private boolean overridenPassThruErrors = false;
220 private boolean passThruIsOverriden = false;
221 private boolean forceAttributePush = false;
222 private boolean forceAttributeNoPush = false;
223 private boolean singleAssertion = false;
224 private boolean defaultToPOST = true;
225 private boolean wantsAssertionsSigned = false;
226 private int preferredArtifactType = 1;
227 private String defaultTarget;
228 private boolean wantsSchemaHack = false;
230 public RelyingPartyImpl(Element partyConfig, IdPConfig globalConfig, Credentials credentials,
231 NameMapper nameMapper) throws ServiceProviderMapperException {
233 configuration = globalConfig;
236 name = ((Element) partyConfig).getAttribute("name");
237 if (name == null || name.equals("")) {
238 log.error("Relying Party name not set. Add a (name) attribute to <RelyingParty>.");
239 throw new ServiceProviderMapperException("Required configuration not specified.");
241 log.debug("Loading Relying Party: (" + name + ").");
243 // Process overrides for global configuration data
244 String attribute = ((Element) partyConfig).getAttribute("providerId");
245 if (attribute != null && !attribute.equals("")) {
246 log.debug("Overriding providerId for Relying Pary (" + name + ") with (" + attribute + ").");
247 overridenIdPProviderId = attribute;
250 attribute = ((Element) partyConfig).getAttribute("AAUrl");
251 if (attribute != null && !attribute.equals("")) {
252 log.debug("Overriding AAUrl for Relying Pary (" + name + ") with (" + attribute + ").");
254 overridenAAUrl = new URL(attribute);
255 } catch (MalformedURLException e) {
256 log.error("(AAUrl) attribute to is not a valid URL.");
257 throw new ServiceProviderMapperException("Configuration is invalid.");
261 attribute = ((Element) partyConfig).getAttribute("defaultAuthMethod");
262 if (attribute != null && !attribute.equals("")) {
263 log.debug("Overriding defaultAuthMethod for Relying Pary (" + name + ") with (" + attribute + ").");
265 overridenDefaultAuthMethod = new URI(attribute);
266 } catch (URISyntaxException e1) {
267 log.error("(defaultAuthMethod) attribute to is not a valid URI.");
268 throw new ServiceProviderMapperException("Configuration is invalid.");
272 attribute = ((Element) partyConfig).getAttribute("passThruErrors");
273 if (attribute != null && !attribute.equals("")) {
274 log.debug("Overriding passThruErrors for Relying Pary (" + name + ") with (" + attribute + ").");
275 overridenPassThruErrors = Boolean.valueOf(attribute).booleanValue();
276 passThruIsOverriden = true;
279 // SSO profile defaulting
280 attribute = ((Element) partyConfig).getAttribute("defaultToPOSTProfile");
281 if (attribute != null && !attribute.equals("")) {
282 defaultToPOST = Boolean.valueOf(attribute).booleanValue();
285 log.debug("Relying party defaults to POST profile.");
287 log.debug("Relying party defaults to Artifact profile.");
290 // Relying Party wants assertions signed?
291 attribute = ((Element) partyConfig).getAttribute("signAssertions");
292 if (attribute != null && !attribute.equals("")) {
293 wantsAssertionsSigned = Boolean.valueOf(attribute).booleanValue();
295 if (wantsAssertionsSigned) {
296 log.debug("Relying party wants SAML Assertions to be signed.");
298 log.debug("Relying party does not want SAML Assertions to be signed.");
301 // Decide whether or not to use the schema hack for old xerces
302 attribute = ((Element) partyConfig).getAttribute("schemaHack");
303 if (attribute != null && !attribute.equals("")) {
304 wantsSchemaHack = Boolean.valueOf(attribute).booleanValue();
306 if (wantsSchemaHack) {
307 log.debug("XML schema hack enabled for this relying party.");
310 // Set a default target for use in artifact redirects
311 defaultTarget = ((Element) partyConfig).getAttribute("defaultTarget");
313 // Determine whether or not we are forcing attribute push on or off
314 String forcePush = ((Element) partyConfig).getAttribute("forceAttributePush");
315 String forceNoPush = ((Element) partyConfig).getAttribute("forceAttributeNoPush");
317 if (forcePush != null && Boolean.valueOf(forcePush).booleanValue() && forceNoPush != null
318 && Boolean.valueOf(forceNoPush).booleanValue()) {
319 log.error("Invalid configuration: Attribute push is forced to ON and OFF for this relying "
320 + "party. Turning off forcing in favor of profile defaults.");
322 forceAttributePush = Boolean.valueOf(forcePush).booleanValue();
323 forceAttributeNoPush = Boolean.valueOf(forceNoPush).booleanValue();
324 log.debug("Attribute push forcing is set to (" + forceAttributePush + ").");
325 log.debug("No attribute push forcing is set to (" + forceAttributeNoPush + ").");
328 attribute = ((Element) partyConfig).getAttribute("preferredArtifactType");
329 if (attribute != null && !attribute.equals("")) {
330 log.debug("Overriding AAUrl for Relying Pary (" + name + ") with (" + attribute + ").");
332 preferredArtifactType = Integer.parseInt(attribute);
333 } catch (NumberFormatException e) {
334 log.error("(preferredArtifactType) attribute to is not a valid integer.");
335 throw new ServiceProviderMapperException("Configuration is invalid.");
337 log.debug("Preferred artifact type: (" + preferredArtifactType + ").");
340 // Load and verify the name mappings that should be used in
341 // assertions for this RelyingParty
343 NodeList nameIDs = ((Element) partyConfig).getElementsByTagNameNS(IdPConfig.configNameSpace, "NameID");
344 // If no specification. Make sure we have a default mapping
345 if (nameIDs.getLength() < 1) {
346 if (nameMapper.getNameIdentifierMappingById(null) == null) {
347 log.error("Relying Party NameId configuration not set. Add a <NameID> element to <RelyingParty>.");
348 throw new ServiceProviderMapperException("Required configuration not specified.");
353 // We do have a specification, so make sure it points to a
354 // valid Name Mapping
356 for (int i = 0; i < nameIDs.getLength(); i++) {
358 String mappingId = ((Element) nameIDs.item(i)).getAttribute("nameMapping");
359 if (mappingId == null || mappingId.equals("")) {
360 log.error("Name mapping not set. Add a (nameMapping) attribute to <NameID>.");
361 throw new ServiceProviderMapperException("Required configuration not specified.");
364 if (nameMapper.getNameIdentifierMappingById(mappingId) == null) {
365 log.error("Relying Party NameID refers to a name mapping that is not loaded.");
366 throw new ServiceProviderMapperException("Required configuration not specified.");
369 mappingIds.add(mappingId);
373 // Load the credential for signing
374 String credentialName = ((Element) partyConfig).getAttribute("signingCredential");
375 Credential signingCredential = credentials.getCredential(credentialName);
376 if (signingCredential == null) {
377 if (credentialName == null || credentialName.equals("")) {
378 log.error("Relying Party credential not set. Add a (signingCredential) "
379 + "attribute to <RelyingParty>.");
380 throw new ServiceProviderMapperException("Required configuration not specified.");
382 log.error("Relying Party credential invalid. Fix the (signingCredential) attribute "
383 + "on <RelyingParty>.");
384 throw new ServiceProviderMapperException("Required configuration is invalid.");
389 // Initialize and Identity Provider object for this use by this relying party
390 identityProvider = new RelyingPartyIdentityProvider(overridenIdPProviderId != null
391 ? overridenIdPProviderId
392 : configuration.getProviderId(), signingCredential);
396 public String getProviderId() {
401 public String getName() {
406 public IdentityProvider getIdentityProvider() {
408 return identityProvider;
411 public boolean isLegacyProvider() {
416 public String[] getNameMapperIds() {
418 return (String[]) mappingIds.toArray(new String[0]);
421 public URI getDefaultAuthMethod() {
423 if (overridenDefaultAuthMethod != null) {
424 return overridenDefaultAuthMethod;
426 return configuration.getDefaultAuthMethod();
430 public URL getAAUrl() {
432 if (overridenAAUrl != null) {
433 return overridenAAUrl;
435 return configuration.getAAUrl();
439 public boolean passThruErrors() {
441 if (passThruIsOverriden) {
442 return overridenPassThruErrors;
444 return configuration.passThruErrors();
448 public boolean forceAttributePush() {
450 return forceAttributePush;
453 public boolean forceAttributeNoPush() {
455 return forceAttributeNoPush;
458 public boolean singleAssertion() {
459 return singleAssertion;
462 public boolean defaultToPOSTProfile() {
464 return defaultToPOST;
467 public boolean wantsAssertionsSigned() {
469 return wantsAssertionsSigned;
472 public int getPreferredArtifactType() {
474 return preferredArtifactType;
477 public String getDefaultTarget() {
479 return defaultTarget;
482 public boolean wantsSchemaHack() {
484 return wantsSchemaHack;
488 * Default identity provider implementation.
490 * @author Walter Hoehn
492 protected class RelyingPartyIdentityProvider implements IdentityProvider {
494 private String providerId;
495 private Credential credential;
497 public RelyingPartyIdentityProvider(String providerId, Credential credential) {
499 this.providerId = providerId;
500 this.credential = credential;
504 * @see edu.internet2.middleware.shibboleth.common.IdentityProvider#getProviderId()
506 public String getProviderId() {
512 * @see edu.internet2.middleware.shibboleth.common.IdentityProvider#getSigningCredential()
514 public Credential getSigningCredential() {
522 * Relying party implementation wrapper for relying parties that are groups.
524 * @author Walter Hoehn
526 class RelyingPartyGroupWrapper implements RelyingParty {
528 private RelyingParty wrapped;
529 private String providerId;
531 RelyingPartyGroupWrapper(RelyingParty wrapped, String providerId) {
533 this.wrapped = wrapped;
534 this.providerId = providerId;
537 public String getName() {
539 return wrapped.getName();
542 public boolean isLegacyProvider() {
547 public IdentityProvider getIdentityProvider() {
549 return wrapped.getIdentityProvider();
552 public String getProviderId() {
557 public String[] getNameMapperIds() {
559 return wrapped.getNameMapperIds();
562 public URL getAAUrl() {
564 return wrapped.getAAUrl();
567 public URI getDefaultAuthMethod() {
569 return wrapped.getDefaultAuthMethod();
572 public boolean passThruErrors() {
574 return wrapped.passThruErrors();
577 public boolean forceAttributePush() {
579 return wrapped.forceAttributePush();
582 public boolean forceAttributeNoPush() {
584 return wrapped.forceAttributeNoPush();
587 public boolean singleAssertion() {
589 return wrapped.singleAssertion();
592 public boolean defaultToPOSTProfile() {
594 return wrapped.defaultToPOSTProfile();
597 public boolean wantsAssertionsSigned() {
599 return wrapped.wantsAssertionsSigned();
602 public int getPreferredArtifactType() {
604 return wrapped.getPreferredArtifactType();
607 public String getDefaultTarget() {
609 return wrapped.getDefaultTarget();
612 public boolean wantsSchemaHack() {
614 return wrapped.wantsSchemaHack();
619 * Relying party implementation wrapper for anonymous service providers.
621 * @author Walter Hoehn
623 protected class UnknownProviderWrapper implements RelyingParty {
625 protected RelyingParty wrapped;
626 protected String providerId;
628 protected UnknownProviderWrapper(RelyingParty wrapped, String providerId) {
630 this.wrapped = wrapped;
631 this.providerId = providerId;
634 public String getName() {
636 return wrapped.getName();
639 public IdentityProvider getIdentityProvider() {
641 return wrapped.getIdentityProvider();
644 public String getProviderId() {
649 public String[] getNameMapperIds() {
651 return wrapped.getNameMapperIds();
654 public boolean isLegacyProvider() {
656 return wrapped.isLegacyProvider();
659 public URL getAAUrl() {
661 return wrapped.getAAUrl();
664 public URI getDefaultAuthMethod() {
666 return wrapped.getDefaultAuthMethod();
669 public boolean passThruErrors() {
671 return wrapped.passThruErrors();
674 public boolean forceAttributePush() {
679 public boolean forceAttributeNoPush() {
684 public boolean singleAssertion() {
689 public boolean defaultToPOSTProfile() {
694 public boolean wantsAssertionsSigned() {
696 return wrapped.wantsAssertionsSigned();
699 public int getPreferredArtifactType() {
701 return wrapped.getPreferredArtifactType();
704 public String getDefaultTarget() {
706 return wrapped.getDefaultTarget();
709 public boolean wantsSchemaHack() {
711 return wrapped.wantsSchemaHack();
716 * Relying party wrapper for Shibboleth <=1.1 service providers.
718 * @author Walter Hoehn
720 class LegacyWrapper extends UnknownProviderWrapper implements RelyingParty {
722 LegacyWrapper(RelyingParty wrapped) {
724 super(wrapped, null);
727 public boolean isLegacyProvider() {
732 public String[] getNameMapperIds() {
734 return ((RelyingParty) wrapped).getNameMapperIds();
737 public URL getAAUrl() {
739 return ((RelyingParty) wrapped).getAAUrl();
742 public URI getDefaultAuthMethod() {
744 return ((RelyingParty) wrapped).getDefaultAuthMethod();
749 * Relying party wrapper for providers for which we have no metadata
751 * @author Walter Hoehn
753 class NoMetadataWrapper extends UnknownProviderWrapper implements RelyingParty {
755 NoMetadataWrapper(RelyingParty wrapped) {
757 super(wrapped, null);
760 public String[] getNameMapperIds() {
762 return ((RelyingParty) wrapped).getNameMapperIds();
765 public URL getAAUrl() {
767 return ((RelyingParty) wrapped).getAAUrl();
770 public URI getDefaultAuthMethod() {
772 return ((RelyingParty) wrapped).getDefaultAuthMethod();