2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * MetadataProvider.cpp
20 * Registration of factories for built-in providers
24 #include "SAMLArtifact.h"
25 #include "saml2/metadata/MetadataProvider.h"
27 #include <log4cpp/Category.hh>
28 #include <xmltooling/util/NDC.h>
30 using namespace opensaml::saml2md;
31 using namespace opensaml;
32 using namespace xmltooling;
33 using namespace log4cpp;
38 SAML_DLLLOCAL PluginManager<MetadataProvider,const DOMElement*>::Factory FilesystemMetadataProviderFactory;
39 SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory BlacklistMetadataFilterFactory;
40 SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory WhitelistMetadataFilterFactory;
44 void SAML_API opensaml::saml2md::registerMetadataProviders()
46 SAMLConfig& conf=SAMLConfig::getConfig();
47 conf.MetadataProviderManager.registerFactory(FILESYSTEM_METADATA_PROVIDER, FilesystemMetadataProviderFactory);
48 conf.MetadataProviderManager.registerFactory("edu.internet2.middleware.shibboleth.metadata.provider.XMLMetadata", FilesystemMetadataProviderFactory);
49 conf.MetadataProviderManager.registerFactory("edu.internet2.middleware.shibboleth.common.provider.XMLMetadata", FilesystemMetadataProviderFactory);
52 void SAML_API opensaml::saml2md::registerMetadataFilters()
54 SAMLConfig::getConfig().MetadataFilterManager.registerFactory(BLACKLIST_METADATA_FILTER, BlacklistMetadataFilterFactory);
55 SAMLConfig::getConfig().MetadataFilterManager.registerFactory(WHITELIST_METADATA_FILTER, WhitelistMetadataFilterFactory);
58 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);
59 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);
60 static const XMLCh Exclude[] = UNICODE_LITERAL_7(E,x,c,l,u,d,e);
61 static const XMLCh Include[] = UNICODE_LITERAL_7(I,n,c,l,u,d,e);
62 static const XMLCh GenericKeyResolver[] = UNICODE_LITERAL_11(K,e,y,R,e,s,o,l,v,e,r);
63 static const XMLCh GenericMetadataFilter[] = UNICODE_LITERAL_14(M,e,t,a,d,a,t,a,F,i,l,t,e,r);
64 static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e);
66 MetadataProvider::MetadataProvider(const DOMElement* e) : m_resolver(NULL)
69 NDC ndc("MetadataProvider");
71 SAMLConfig& conf=SAMLConfig::getConfig();
73 // Locate any default recognized filters and plugins.
75 DOMElement* child = e ? XMLHelper::getFirstChildElement(e) : NULL;
77 if (!m_resolver && XMLString::equals(child->getLocalName(),GenericKeyResolver)) {
78 auto_ptr_char t(child->getAttributeNS(NULL,type));
80 m_resolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(t.get(),child);
82 else if (XMLString::equals(child->getLocalName(),GenericMetadataFilter)) {
83 auto_ptr_char t(child->getAttributeNS(NULL,type));
85 m_filters.push_back(conf.MetadataFilterManager.newPlugin(t.get(),child));
87 else if (XMLString::equals(child->getLocalName(),Whitelist)) {
88 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,child));
90 else if (XMLString::equals(child->getLocalName(),Blacklist)) {
91 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,child));
93 else if (XMLString::equals(child->getLocalName(),Include)) {
94 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,e));
96 else if (XMLString::equals(child->getLocalName(),Exclude)) {
97 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,e));
99 child = XMLHelper::getNextSiblingElement(child);
103 m_resolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(INLINE_KEY_RESOLVER, child);
106 catch (XMLToolingException& ex) {
107 Category::getInstance(SAML_LOGCAT".Metadata").error("caught exception while installing plugins and filters: %s", ex.what());
109 for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
114 MetadataProvider::~MetadataProvider()
117 for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
120 void MetadataProvider::doFilters(XMLObject& xmlObject) const
123 NDC ndc("doFilters");
125 Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
126 for (std::vector<MetadataFilter*>::const_iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
127 log.info("applying metadata filter (%s)", (*i)->getId());
128 (*i)->doFilter(xmlObject);
132 void MetadataProvider::index(EntityDescriptor* site, time_t validUntil)
134 if (validUntil < site->getValidUntilEpoch())
135 site->setValidUntil(validUntil);
137 auto_ptr_char id(site->getEntityID());
139 m_sites.insert(make_pair(id.get(),site));
142 // Process each IdP role.
143 const vector<IDPSSODescriptor*>& roles=const_cast<const EntityDescriptor*>(site)->getIDPSSODescriptors();
144 for (vector<IDPSSODescriptor*>::const_iterator i=roles.begin(); i!=roles.end(); i++) {
146 if ((*i)->hasSupport(SAMLConstants::SAML10_PROTOCOL_ENUM) || (*i)->hasSupport(SAMLConstants::SAML11_PROTOCOL_ENUM)) {
147 // Check for SourceID extension element.
148 const Extensions* exts=(*i)->getExtensions();
150 const list<XMLObject*>& children=exts->getXMLObjects();
151 for (list<XMLObject*>::const_iterator ext=children.begin(); ext!=children.end(); ext++) {
152 SourceID* sid=dynamic_cast<SourceID*>(*ext);
154 auto_ptr_char sourceid(sid->getID());
155 if (sourceid.get()) {
156 m_sources.insert(pair<string,const EntityDescriptor*>(sourceid.get(),site));
165 pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
168 // Load endpoints for type 0x0002 artifacts.
169 const vector<ArtifactResolutionService*>& locs=const_cast<const IDPSSODescriptor*>(*i)->getArtifactResolutionServices();
170 for (vector<ArtifactResolutionService*>::const_iterator loc=locs.begin(); loc!=locs.end(); loc++) {
171 auto_ptr_char location((*loc)->getLocation());
173 m_sources.insert(pair<string,const EntityDescriptor*>(location.get(),site));
178 if ((*i)->hasSupport(SAMLConstants::SAML20P_NS)) {
181 pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
187 void MetadataProvider::index(EntitiesDescriptor* group, time_t validUntil)
189 if (validUntil < group->getValidUntilEpoch())
190 group->setValidUntil(validUntil);
192 auto_ptr_char name(group->getName());
194 m_groups.insert(make_pair(name.get(),group));
197 const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor*>(group)->getEntitiesDescriptors();
198 for (vector<EntitiesDescriptor*>::const_iterator i=groups.begin(); i!=groups.end(); i++)
199 index(*i,group->getValidUntilEpoch());
201 const vector<EntityDescriptor*>& sites=const_cast<const EntitiesDescriptor*>(group)->getEntityDescriptors();
202 for (vector<EntityDescriptor*>::const_iterator j=sites.begin(); j!=sites.end(); j++)
203 index(*j,group->getValidUntilEpoch());
206 void MetadataProvider::clearDescriptorIndex()
213 const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const char* name, bool strict) const
215 pair<groupmap_t::const_iterator,groupmap_t::const_iterator> range=m_groups.equal_range(name);
217 time_t now=time(NULL);
218 for (groupmap_t::const_iterator i=range.first; i!=range.second; i++)
219 if (now < i->second->getValidUntilEpoch())
222 if (!strict && range.first!=range.second)
223 return range.first->second;
228 const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const XMLCh* name, bool strict) const
230 auto_ptr_char temp(name);
231 return getEntitiesDescriptor(temp.get(),strict);
234 const EntityDescriptor* MetadataProvider::getEntityDescriptor(const char* name, bool strict) const
236 pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sites.equal_range(name);
238 time_t now=time(NULL);
239 for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
240 if (now < i->second->getValidUntilEpoch())
243 if (!strict && range.first!=range.second)
244 return range.first->second;
249 const EntityDescriptor* MetadataProvider::getEntityDescriptor(const XMLCh* name, bool strict) const
251 auto_ptr_char temp(name);
252 return getEntityDescriptor(temp.get(),strict);
255 const EntityDescriptor* MetadataProvider::getEntityDescriptor(const SAMLArtifact* artifact) const
257 pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sources.equal_range(artifact->getSource());
259 time_t now=time(NULL);
260 for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
261 if (now < i->second->getValidUntilEpoch())