Add EntityRole filter.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / MetadataProvider.cpp
1 /*
2  *  Copyright 2001-2009 Internet2
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 /**
18  * MetadataProvider.cpp
19  *
20  * Registration of factories for built-in providers
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/MetadataFilter.h"
25 #include "saml2/metadata/MetadataProvider.h"
26
27 #include <xercesc/util/XMLUniDefs.hpp>
28 #include <xmltooling/logging.h>
29 #include <xmltooling/util/NDC.h>
30 #include <xmltooling/util/XMLHelper.h>
31
32 using namespace opensaml::saml2md;
33 using namespace opensaml;
34 using namespace xmltooling::logging;
35 using namespace xmltooling;
36 using namespace std;
37
38 namespace opensaml {
39     namespace saml2md {
40         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory XMLMetadataProviderFactory;
41         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory DynamicMetadataProviderFactory;
42         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory ChainingMetadataProviderFactory;
43         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory NullMetadataProviderFactory;
44         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory BlacklistMetadataFilterFactory;
45         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory WhitelistMetadataFilterFactory;
46         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory SignatureMetadataFilterFactory;
47         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory RequireValidUntilMetadataFilterFactory;
48         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory EntityRoleMetadataFilterFactory;
49     };
50 };
51
52 void SAML_API opensaml::saml2md::registerMetadataProviders()
53 {
54     SAMLConfig& conf=SAMLConfig::getConfig();
55     conf.MetadataProviderManager.registerFactory(XML_METADATA_PROVIDER, XMLMetadataProviderFactory);
56     conf.MetadataProviderManager.registerFactory(DYNAMIC_METADATA_PROVIDER, DynamicMetadataProviderFactory);
57     conf.MetadataProviderManager.registerFactory(CHAINING_METADATA_PROVIDER, ChainingMetadataProviderFactory);
58     conf.MetadataProviderManager.registerFactory(NULL_METADATA_PROVIDER, NullMetadataProviderFactory);
59 }
60
61 void SAML_API opensaml::saml2md::registerMetadataFilters()
62 {
63     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(BLACKLIST_METADATA_FILTER, BlacklistMetadataFilterFactory);
64     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(WHITELIST_METADATA_FILTER, WhitelistMetadataFilterFactory);
65     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(SIGNATURE_METADATA_FILTER, SignatureMetadataFilterFactory);
66     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(REQUIREVALIDUNTIL_METADATA_FILTER, RequireValidUntilMetadataFilterFactory);
67     // additional name matching Java code
68     SAMLConfig::getConfig().MetadataFilterManager.registerFactory("RequiredValidUntil", RequireValidUntilMetadataFilterFactory);
69     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(ENTITYROLE_METADATA_FILTER, EntityRoleMetadataFilterFactory);
70 }
71
72 static const XMLCh _MetadataFilter[] =  UNICODE_LITERAL_14(M,e,t,a,d,a,t,a,F,i,l,t,e,r);
73 static const XMLCh Blacklist[] =        UNICODE_LITERAL_23(B,l,a,c,k,l,i,s,t,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
74 static const XMLCh Whitelist[] =        UNICODE_LITERAL_23(W,h,i,t,e,l,i,s,t,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
75 static const XMLCh SigFilter[] =        UNICODE_LITERAL_23(S,i,g,n,a,t,u,r,e,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
76 static const XMLCh Exclude[] =          UNICODE_LITERAL_7(E,x,c,l,u,d,e);
77 static const XMLCh Include[] =          UNICODE_LITERAL_7(I,n,c,l,u,d,e);
78 static const XMLCh type[] =             UNICODE_LITERAL_4(t,y,p,e);
79
80 MetadataProvider::MetadataProvider(const DOMElement* e)
81 {
82 #ifdef _DEBUG
83     NDC ndc("MetadataProvider");
84 #endif
85     Category& log = Category::getInstance(SAML_LOGCAT".Metadata");
86     SAMLConfig& conf=SAMLConfig::getConfig();
87
88     // Locate any default recognized filters and plugins.
89     try {
90         DOMElement* child = e ? XMLHelper::getFirstChildElement(e) : NULL;
91         while (child) {
92             if (XMLString::equals(child->getLocalName(),_MetadataFilter)) {
93                 auto_ptr_char t(child->getAttributeNS(NULL,type));
94                 if (t.get() && *t.get()) {
95                     log.info("building MetadataFilter of type %s", t.get());
96                     m_filters.push_back(conf.MetadataFilterManager.newPlugin(t.get(),child));
97                 }
98             }
99             else if (XMLString::equals(child->getLocalName(),SigFilter)) {
100                 log.info("building MetadataFilter of type %s", SIGNATURE_METADATA_FILTER);
101                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(SIGNATURE_METADATA_FILTER,child));
102             }
103             else if (XMLString::equals(child->getLocalName(),Whitelist)) {
104                 log.info("building MetadataFilter of type %s", WHITELIST_METADATA_FILTER);
105                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,child));
106             }
107             else if (XMLString::equals(child->getLocalName(),Blacklist)) {
108                 log.info("building MetadataFilter of type %s", BLACKLIST_METADATA_FILTER);
109                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,child));
110             }
111             else if (XMLString::equals(child->getLocalName(),Include)) {
112                 log.info("building MetadataFilter of type %s", WHITELIST_METADATA_FILTER);
113                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,e));
114             }
115             else if (XMLString::equals(child->getLocalName(),Exclude)) {
116                 log.info("building MetadataFilter of type %s", BLACKLIST_METADATA_FILTER);
117                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,e));
118             }
119             child = XMLHelper::getNextSiblingElement(child);
120         }
121     }
122     catch (XMLToolingException& ex) {
123         log.error("caught exception while installing filters: %s", ex.what());
124         for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
125         throw;
126     }
127 }
128
129 MetadataProvider::~MetadataProvider()
130 {
131     for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
132 }
133
134 void MetadataProvider::doFilters(XMLObject& xmlObject) const
135 {
136 #ifdef _DEBUG
137     NDC ndc("doFilters");
138 #endif
139     Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
140     for (std::vector<MetadataFilter*>::const_iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
141         log.info("applying metadata filter (%s)", (*i)->getId());
142         (*i)->doFilter(xmlObject);
143     }
144 }
145
146 const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const XMLCh* name, bool strict) const
147 {
148     auto_ptr_char temp(name);
149     return getEntitiesDescriptor(temp.get(),strict);
150 }