View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.config.relyingparty;
19  
20  import java.util.List;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.opensaml.xml.util.DatatypeHelper;
25  import org.opensaml.xml.util.XMLHelper;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.support.AbstractBeanDefinition;
29  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
30  import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
31  import org.springframework.beans.factory.xml.ParserContext;
32  import org.w3c.dom.Attr;
33  import org.w3c.dom.Element;
34  
35  import edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils;
36  import edu.internet2.middleware.shibboleth.common.relyingparty.provider.SAMLMDRelyingPartyConfigurationManager;
37  
38  /**
39   * Bean definition parser for relying party elements.
40   */
41  public class RelyingPartyConfigurationBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
42  
43      /** Schema type name. */
44      public static final QName URP_TYPE_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
45              "UnidentifiedRelyingParty");
46  
47      /** Schema type name. */
48      public static final QName RP_TYPE_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE, 
49              "IdentifiedRelyingParty");
50  
51      /** Name of the anonymous relying party configuration element. */
52      public static final QName ANON_RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
53              "AnonymousRelyingParty");
54  
55      /** Name of the default relying party configuration element. */
56      public static final QName DEFAULT_RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE,
57              "DefaultRelyingParty");
58  
59      /** Name of the relying party configuration element. */
60      public static final QName RP_ELEMENT_NAME = new QName(RelyingPartyNamespaceHandler.NAMESPACE, "RelyingParty");
61  
62      /** Class logger. */
63      private final Logger log = LoggerFactory.getLogger(RelyingPartyConfigurationBeanDefinitionParser.class);
64  
65      /** {@inheritDoc} */
66      protected Class getBeanClass(Element arg0) {
67          return RelyingPartyFactoryBean.class;
68      }
69  
70      /** {@inheritDoc} */
71      protected void doParse(Element config, ParserContext parserContext, BeanDefinitionBuilder builder) {
72          String rpId = getRelyingPartyId(config);
73          log.info("Parsing configuration for relying party with id: {}", rpId);
74          builder.addPropertyValue("relyingPartyId", rpId);
75  
76          String provider = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "provider"));
77          log.debug("Relying party configuration - provider ID: {}", provider);
78          builder.addPropertyValue("providerId", provider);
79  
80          String authnMethod = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null,
81                  "defaultAuthenticationMethod"));
82          log.debug("Relying party configuration - default authentication method: {}", authnMethod);
83          builder.addPropertyValue("defaultAuthenticationMethod", authnMethod);
84  
85          String secCredRef = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null,
86                  "defaultSigningCredentialRef"));
87          if (secCredRef != null) {
88              log.debug("Relying party configuration - default signing credential: {}", secCredRef);
89              builder.addPropertyReference("defaultSigningCredential", secCredRef);
90          }
91  
92          Attr precedenceAttr = config.getAttributeNodeNS(null, "nameIDFormatPrecedence");
93          if(precedenceAttr != null){
94              List<String> precedence = XMLHelper.getAttributeValueAsList(precedenceAttr);
95              log.debug("Relying party configuration - NameID format precedence: {}", precedence);
96              builder.addPropertyValue("nameIdFormatPrecedence", precedence);
97          }
98          
99          List<Element> profileConfigs = XMLHelper.getChildElementsByTagNameNS(config,
100                 RelyingPartyNamespaceHandler.NAMESPACE, "ProfileConfiguration");
101         if (profileConfigs != null && profileConfigs.size() > 0) {
102             log.debug("Relying party configuration - {} profile configurations", profileConfigs.size());
103             builder.addPropertyValue("profileConfigurations", SpringConfigurationUtils.parseInnerCustomElements(
104                     profileConfigs, parserContext));
105         }
106     }
107 
108     /**
109      * Gets the ID of the relying party.
110      * 
111      * @param config relying party configuration element
112      * 
113      * @return ID of the relying party
114      */
115     protected String getRelyingPartyId(Element config) {
116         String id = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "id"));
117         if (id == null) {
118             if (XMLHelper.getNodeQName(config).equals(ANON_RP_ELEMENT_NAME)) {
119                 id = SAMLMDRelyingPartyConfigurationManager.ANONYMOUS_RP_NAME;
120             } else if (XMLHelper.getNodeQName(config).equals(DEFAULT_RP_ELEMENT_NAME)) {
121                 id = SAMLMDRelyingPartyConfigurationManager.DEFAULT_RP_NAME;
122             }
123         }
124 
125         return id;
126     }
127 
128     /** {@inheritDoc} */
129     protected String resolveId(Element arg0, AbstractBeanDefinition arg1, ParserContext arg2) {
130         return getRelyingPartyId(arg0);
131     }
132 }