View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
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
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.config.relyingparty;
18  
19  import java.util.List;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.opensaml.xml.util.DatatypeHelper;
24  import org.opensaml.xml.util.XMLHelper;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.beans.factory.support.AbstractBeanDefinition;
28  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
29  import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
30  import org.springframework.beans.factory.xml.ParserContext;
31  import org.w3c.dom.Element;
32  
33  import edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils;
34  import edu.internet2.middleware.shibboleth.common.relyingparty.provider.SAMLMDRelyingPartyConfigurationManager;
35  
36  /**
37   * Bean definition parser for relying party elements.
38   */
39  public class RelyingPartyConfigurationBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
40  
41      /** Schema type name. */
42      public static final QName URP_TYPE_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
43              "UnidentifiedRelyingParty");
44  
45      /** Schema type name. */
46      public static final QName RP_TYPE_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE, 
47              "IdentifiedRelyingParty");
48  
49      /** Name of the anonymous relying party configuration element. */
50      public static final QName ANON_RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
51              "AnonymousRelyingParty");
52  
53      /** Name of the default relying party configuration element. */
54      public static final QName DEFAULT_RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
55              "DefaultRelyingParty");
56  
57      /** Name of the relying party configuration element. */
58      public static final QName RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE, "RelyingParty");
59  
60      /** Class logger. */
61      private final Logger log = LoggerFactory.getLogger(RelyingPartyConfigurationBeanDefinitionParser.class);
62  
63      /** {@inheritDoc} */
64      protected Class getBeanClass(Element arg0) {
65          return RelyingPartyFactoryBean.class;
66      }
67  
68      /** {@inheritDoc} */
69      protected void doParse(Element config, ParserContext parserContext, BeanDefinitionBuilder builder) {
70          String rpId = getRelyingPartyId(config);
71          log.info("Parsing configuration for relying party with id: {}", rpId);
72          builder.addPropertyValue("relyingPartyId", rpId);
73  
74          String provider = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "provider"));
75          log.debug("Relying party configuration - provider ID: {}", provider);
76          builder.addPropertyValue("providerId", provider);
77  
78          String authnMethod = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null,
79                  "defaultAuthenticationMethod"));
80          log.debug("Relying party configuration - default authentication method: {}", authnMethod);
81          builder.addPropertyValue("defaultAuthenticationMethod", authnMethod);
82  
83          String secCredRef = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null,
84                  "defaultSigningCredentialRef"));
85          if (secCredRef != null) {
86              log.debug("Relying party configuration - default signing credential: {}", secCredRef);
87              builder.addPropertyReference("defaultSigningCredential", secCredRef);
88          }
89  
90          List<Element> profileConfigs = XMLHelper.getChildElementsByTagNameNS(config,
91                  RelyingPartyNamespaceHandler.NAMESPACE, "ProfileConfiguration");
92          if (profileConfigs != null && profileConfigs.size() > 0) {
93              log.debug("Relying party configuration - {} profile configurations", profileConfigs.size());
94              builder.addPropertyValue("profileConfigurations", SpringConfigurationUtils.parseInnerCustomElements(
95                      profileConfigs, parserContext));
96          }
97      }
98  
99      /**
100      * Gets the ID of the relying party.
101      * 
102      * @param config relying party configuration element
103      * 
104      * @return ID of the relying party
105      */
106     protected String getRelyingPartyId(Element config) {
107         String id = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "id"));
108         if (id == null) {
109             if (XMLHelper.getNodeQName(config).equals(ANON_RP_ELEMENT_NAME)) {
110                 id = SAMLMDRelyingPartyConfigurationManager.ANONYMOUS_RP_NAME;
111             } else if (XMLHelper.getNodeQName(config).equals(DEFAULT_RP_ELEMENT_NAME)) {
112                 id = SAMLMDRelyingPartyConfigurationManager.DEFAULT_RP_NAME;
113             }
114         }
115 
116         return id;
117     }
118 
119     /** {@inheritDoc} */
120     protected String resolveId(Element arg0, AbstractBeanDefinition arg1, ParserContext arg2) {
121         return getRelyingPartyId(arg0);
122     }
123 }