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.Attr;
32  import org.w3c.dom.Element;
33  
34  import edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils;
35  import edu.internet2.middleware.shibboleth.common.relyingparty.provider.SAMLMDRelyingPartyConfigurationManager;
36  
37  /**
38   * Bean definition parser for relying party elements.
39   */
40  public class RelyingPartyConfigurationBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
41  
42      /** Schema type name. */
43      public static final QName URP_TYPE_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
44              "UnidentifiedRelyingParty");
45  
46      /** Schema type name. */
47      public static final QName RP_TYPE_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE, 
48              "IdentifiedRelyingParty");
49  
50      /** Name of the anonymous relying party configuration element. */
51      public static final QName ANON_RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
52              "AnonymousRelyingParty");
53  
54      /** Name of the default relying party configuration element. */
55      public static final QName DEFAULT_RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
56              "DefaultRelyingParty");
57  
58      /** Name of the relying party configuration element. */
59      public static final QName RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE, "RelyingParty");
60  
61      /** Class logger. */
62      private final Logger log = LoggerFactory.getLogger(RelyingPartyConfigurationBeanDefinitionParser.class);
63  
64      /** {@inheritDoc} */
65      protected Class getBeanClass(Element arg0) {
66          return RelyingPartyFactoryBean.class;
67      }
68  
69      /** {@inheritDoc} */
70      protected void doParse(Element config, ParserContext parserContext, BeanDefinitionBuilder builder) {
71          String rpId = getRelyingPartyId(config);
72          log.info("Parsing configuration for relying party with id: {}", rpId);
73          builder.addPropertyValue("relyingPartyId", rpId);
74  
75          String provider = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "provider"));
76          log.debug("Relying party configuration - provider ID: {}", provider);
77          builder.addPropertyValue("providerId", provider);
78  
79          String authnMethod = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null,
80                  "defaultAuthenticationMethod"));
81          log.debug("Relying party configuration - default authentication method: {}", authnMethod);
82          builder.addPropertyValue("defaultAuthenticationMethod", authnMethod);
83  
84          String secCredRef = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null,
85                  "defaultSigningCredentialRef"));
86          if (secCredRef != null) {
87              log.debug("Relying party configuration - default signing credential: {}", secCredRef);
88              builder.addPropertyReference("defaultSigningCredential", secCredRef);
89          }
90  
91          Attr precedenceAttr = config.getAttributeNodeNS(null, "nameIDFormatPrecedence");
92          if(precedenceAttr != null){
93              List<String> precedence = XMLHelper.getAttributeValueAsList(precedenceAttr);
94              log.debug("Relying party configuration - NameID format precedence: {}", precedence);
95              builder.addPropertyValue("nameIdFormatPrecedence", precedence);
96          }
97          
98          List<Element> profileConfigs = XMLHelper.getChildElementsByTagNameNS(config,
99                  RelyingPartyNamespaceHandler.NAMESPACE, "ProfileConfiguration");
100         if (profileConfigs != null && profileConfigs.size() > 0) {
101             log.debug("Relying party configuration - {} profile configurations", profileConfigs.size());
102             builder.addPropertyValue("profileConfigurations", SpringConfigurationUtils.parseInnerCustomElements(
103                     profileConfigs, parserContext));
104         }
105     }
106 
107     /**
108      * Gets the ID of the relying party.
109      * 
110      * @param config relying party configuration element
111      * 
112      * @return ID of the relying party
113      */
114     protected String getRelyingPartyId(Element config) {
115         String id = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "id"));
116         if (id == null) {
117             if (XMLHelper.getNodeQName(config).equals(ANON_RP_ELEMENT_NAME)) {
118                 id = SAMLMDRelyingPartyConfigurationManager.ANONYMOUS_RP_NAME;
119             } else if (XMLHelper.getNodeQName(config).equals(DEFAULT_RP_ELEMENT_NAME)) {
120                 id = SAMLMDRelyingPartyConfigurationManager.DEFAULT_RP_NAME;
121             }
122         }
123 
124         return id;
125     }
126 
127     /** {@inheritDoc} */
128     protected String resolveId(Element arg0, AbstractBeanDefinition arg1, ParserContext arg2) {
129         return getRelyingPartyId(arg0);
130     }
131 }