2 * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.] Licensed under the Apache License,
3 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy
4 * of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in
5 * writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
6 * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and
7 * limitations under the License.
10 package edu.internet2.middleware.shibboleth.idp;
13 import java.security.Principal;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.List;
20 import javax.xml.namespace.QName;
22 import org.apache.log4j.Logger;
23 import org.apache.xml.security.signature.XMLSignature;
24 import org.opensaml.InvalidCryptoException;
25 import org.opensaml.SAMLAssertion;
26 import org.opensaml.SAMLAttribute;
27 import org.opensaml.SAMLException;
28 import org.opensaml.SAMLResponse;
29 import org.opensaml.saml2.metadata.EntitiesDescriptor;
30 import org.opensaml.saml2.metadata.EntityDescriptor;
31 import org.opensaml.saml2.metadata.RoleDescriptor;
32 import org.opensaml.saml2.metadata.provider.ChainingMetadataProvider;
33 import org.opensaml.saml2.metadata.provider.MetadataFilter;
34 import org.opensaml.saml2.metadata.provider.MetadataProvider;
35 import org.opensaml.saml2.metadata.provider.MetadataProviderException;
36 import org.opensaml.xml.XMLObject;
37 import org.w3c.dom.Element;
39 import edu.internet2.middleware.shibboleth.aa.AAAttribute;
40 import edu.internet2.middleware.shibboleth.aa.AAException;
41 import edu.internet2.middleware.shibboleth.aa.arp.ArpEngine;
42 import edu.internet2.middleware.shibboleth.aa.arp.ArpProcessingException;
43 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
44 import edu.internet2.middleware.shibboleth.artifact.ArtifactMapper;
45 import edu.internet2.middleware.shibboleth.common.Credential;
46 import edu.internet2.middleware.shibboleth.common.NameMapper;
47 import edu.internet2.middleware.shibboleth.common.RelyingParty;
48 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
49 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
50 import edu.internet2.middleware.shibboleth.common.Trust;
51 import edu.internet2.middleware.shibboleth.common.provider.ShibbolethTrust;
52 import edu.internet2.middleware.shibboleth.metadata.MetadataProviderFactory;
55 * Delivers core IdP functionality (Attribute resolution, ARP filtering, Metadata lookup, Signing, Mapping between local &
56 * SAML identifiers, etc.) to components that process protocol-specific requests.
58 * @author Walter Hoehn
60 public class IdPProtocolSupport implements MetadataProvider {
62 private static Logger log = Logger.getLogger(IdPProtocolSupport.class.getName());
63 private Logger transactionLog;
64 private IdPConfig config;
65 private NameMapper nameMapper;
66 private ServiceProviderMapper spMapper;
67 private ArpEngine arpEngine;
68 private AttributeResolver resolver;
69 private ArtifactMapper artifactMapper;
70 private Semaphore throttle;
71 private Trust trust = new ShibbolethTrust();
72 private ChainingMetadataProvider wrappedMetadataProvider = new ChainingMetadataProvider();
74 IdPProtocolSupport(IdPConfig config, Logger transactionLog, NameMapper nameMapper, ServiceProviderMapper spMapper,
75 ArpEngine arpEngine, AttributeResolver resolver, ArtifactMapper artifactMapper)
76 throws ShibbolethConfigurationException {
78 this.transactionLog = transactionLog;
80 this.nameMapper = nameMapper;
81 this.spMapper = spMapper;
82 spMapper.setMetadata(this);
83 this.arpEngine = arpEngine;
84 this.resolver = resolver;
85 this.artifactMapper = artifactMapper;
87 // Load a semaphore that throttles how many requests the IdP will handle at once
88 throttle = new Semaphore(config.getMaxThreads());
91 public Logger getTransactionLog() {
93 return transactionLog;
96 public IdPConfig getIdPConfig() {
101 public NameMapper getNameMapper() {
106 public ServiceProviderMapper getServiceProviderMapper() {
111 public void signAssertions(SAMLAssertion[] assertions, RelyingParty relyingParty) throws InvalidCryptoException,
114 if (relyingParty.getIdentityProvider().getSigningCredential() == null
115 || relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey() == null) { throw new InvalidCryptoException(
116 SAMLException.RESPONDER, "Invalid signing credential."); }
118 for (int i = 0; i < assertions.length; i++) {
119 String assertionAlgorithm;
120 if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.RSA) {
121 assertionAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
122 } else if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.DSA) {
123 assertionAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_DSA;
125 throw new InvalidCryptoException(SAMLException.RESPONDER,
126 "The Shibboleth IdP currently only supports signing with RSA and DSA keys.");
131 assertions[i].sign(assertionAlgorithm, relyingParty.getIdentityProvider().getSigningCredential()
132 .getPrivateKey(), Arrays.asList(relyingParty.getIdentityProvider().getSigningCredential()
133 .getX509CertificateChain()));
140 public void signResponse(SAMLResponse response, RelyingParty relyingParty) throws SAMLException {
142 // Make sure we have an appropriate credential
143 if (relyingParty.getIdentityProvider().getSigningCredential() == null
144 || relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey() == null) { throw new InvalidCryptoException(
145 SAMLException.RESPONDER, "Invalid signing credential."); }
148 String responseAlgorithm;
149 if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.RSA) {
150 responseAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
151 } else if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.DSA) {
152 responseAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_DSA;
154 throw new InvalidCryptoException(SAMLException.RESPONDER,
155 "The Shibboleth IdP currently only supports signing with RSA and DSA keys.");
159 response.sign(responseAlgorithm, relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey(),
160 Arrays.asList(relyingParty.getIdentityProvider().getSigningCredential().getX509CertificateChain()));
166 protected void addMetadataProvider(Element element) {
168 log.debug("Found Metadata Provider configuration element.");
169 if (!element.getTagName().equals("MetadataProvider")) {
170 log.error("Error while attemtping to load Metadata Provider. Malformed provider specificaion.");
175 wrappedMetadataProvider.addMetadataProvider(MetadataProviderFactory.loadProvider(element));
176 } catch (MetadataProviderException e) {
177 log.error("Unable to load Metadata Provider. Skipping...");
182 public Collection<? extends SAMLAttribute> getReleaseAttributes(Principal principal, RelyingParty relyingParty,
183 String requester) throws AAException {
186 Collection<URI> potentialAttributes = arpEngine.listPossibleReleaseAttributes(principal, requester);
187 return getReleaseAttributes(principal, relyingParty, requester, potentialAttributes);
189 } catch (ArpProcessingException e) {
190 log.error("An error occurred while processing the ARPs for principal (" + principal.getName() + ") :"
192 throw new AAException("Error retrieving data for principal.");
196 public Collection<? extends SAMLAttribute> getReleaseAttributes(Principal principal, RelyingParty relyingParty,
197 String requester, Collection<URI> attributeNames) throws AAException {
200 Map<String, AAAttribute> attributes = new HashMap<String, AAAttribute>();
201 for (URI name : attributeNames) {
203 AAAttribute attribute = null;
204 if (relyingParty.wantsSchemaHack()) {
205 attribute = new AAAttribute(name.toString(), true);
207 attribute = new AAAttribute(name.toString(), false);
209 attributes.put(attribute.getName(), attribute);
212 Collection<URI> constraintAttributes = arpEngine.listRequiredConstraintAttributes(principal, requester,
214 for (URI name : constraintAttributes) {
215 if (!attributes.containsKey(name.toString())) {
216 // don't care about schema hack since these attributes won't be returned to SP
217 AAAttribute attribute = new AAAttribute(name.toString(), false);
218 attributes.put(attribute.getName(), attribute);
222 return resolveAttributes(principal, requester, relyingParty.getIdentityProvider().getProviderId(),
225 } catch (SAMLException e) {
226 log.error("An error occurred while creating attributes for principal (" + principal.getName() + ") :"
228 throw new AAException("Error retrieving data for principal.");
230 } catch (ArpProcessingException e) {
231 log.error("An error occurred while processing the ARPs for principal (" + principal.getName() + ") :"
233 throw new AAException("Error retrieving data for principal.");
237 public Collection<? extends SAMLAttribute> resolveAttributes(Principal principal, String requester,
238 String responder, Map<String, AAAttribute> attributeSet) throws ArpProcessingException {
240 resolver.resolveAttributes(principal, requester, responder, attributeSet);
241 arpEngine.filterAttributes(attributeSet.values(), principal, requester);
242 return attributeSet.values();
245 public Collection<? extends SAMLAttribute> resolveAttributesNoPolicies(Principal principal, String requester,
246 String responder, Map<String, AAAttribute> attributeSet) {
248 resolver.resolveAttributes(principal, requester, responder, attributeSet);
249 return attributeSet.values();
253 * Cleanup resources that won't be released when this object is garbage-collected
255 public void destroy() {
261 public ArtifactMapper getArtifactMapper() {
263 return artifactMapper;
266 public Trust getTrust() {
271 public boolean requireValidMetadata() {
273 return wrappedMetadataProvider.requireValidMetadata();
276 public void setRequireValidMetadata(boolean requireValidMetadata) {
278 wrappedMetadataProvider.setRequireValidMetadata(requireValidMetadata);
281 public MetadataFilter getMetadataFilter() {
283 return wrappedMetadataProvider.getMetadataFilter();
286 public void setMetadataFilter(MetadataFilter newFilter) throws MetadataProviderException {
288 wrappedMetadataProvider.setMetadataFilter(newFilter);
291 public XMLObject getMetadata() throws MetadataProviderException {
293 return wrappedMetadataProvider.getMetadata();
296 public EntitiesDescriptor getEntitiesDescriptor(String name) throws MetadataProviderException {
298 return wrappedMetadataProvider.getEntitiesDescriptor(name);
301 public EntityDescriptor getEntityDescriptor(String entityID) throws MetadataProviderException {
303 return wrappedMetadataProvider.getEntityDescriptor(entityID);
306 public List<RoleDescriptor> getRole(String entityID, QName roleName) throws MetadataProviderException {
308 return wrappedMetadataProvider.getRole(entityID, roleName);
311 public List<RoleDescriptor> getRole(String entityID, QName roleName, String supportedProtocol)
312 throws MetadataProviderException {
314 return wrappedMetadataProvider.getRole(entityID, roleName, supportedProtocol);
317 public int providerCount() {
319 return wrappedMetadataProvider.getProviders().size();
322 private class Semaphore {
326 public Semaphore(int value) {
331 public synchronized void enter() {
337 } catch (InterruptedException e) {
338 // squelch and continue
343 public synchronized void exit() {