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 * AbstractMetadataProvider.cpp
20 * Base class for caching metadata providers.
24 #include "binding/SAMLArtifact.h"
25 #include "saml2/metadata/Metadata.h"
26 #include "saml2/metadata/AbstractMetadataProvider.h"
28 #include <xercesc/util/XMLUniDefs.hpp>
29 #include <xmltooling/signature/CachingKeyResolver.h>
30 #include <xmltooling/util/XMLHelper.h>
32 using namespace opensaml::saml2md;
33 using namespace opensaml;
34 using namespace xmltooling;
37 static const XMLCh GenericKeyResolver[] = UNICODE_LITERAL_11(K,e,y,R,e,s,o,l,v,e,r);
38 static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e);
40 AbstractMetadataProvider::AbstractMetadataProvider(const DOMElement* e) : ObservableMetadataProvider(e), m_resolver(NULL)
42 e = e ? XMLHelper::getFirstChildElement(e, GenericKeyResolver) : NULL;
44 auto_ptr_char t(e->getAttributeNS(NULL,type));
46 m_resolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(t.get(),e);
48 throw UnknownExtensionException("<KeyResolver> element found with no type attribute");
52 m_resolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(INLINE_KEY_RESOLVER, NULL);
56 AbstractMetadataProvider::~AbstractMetadataProvider()
61 void AbstractMetadataProvider::emitChangeEvent()
63 xmlsignature::CachingKeyResolver* ckr=dynamic_cast<xmlsignature::CachingKeyResolver*>(m_resolver);
66 ObservableMetadataProvider::emitChangeEvent();
69 void AbstractMetadataProvider::index(EntityDescriptor* site, time_t validUntil)
71 if (validUntil < site->getValidUntilEpoch())
72 site->setValidUntil(validUntil);
74 auto_ptr_char id(site->getEntityID());
76 m_sites.insert(make_pair(id.get(),site));
79 // Process each IdP role.
80 const vector<IDPSSODescriptor*>& roles=const_cast<const EntityDescriptor*>(site)->getIDPSSODescriptors();
81 for (vector<IDPSSODescriptor*>::const_iterator i=roles.begin(); i!=roles.end(); i++) {
83 if ((*i)->hasSupport(SAMLConstants::SAML10_PROTOCOL_ENUM) || (*i)->hasSupport(SAMLConstants::SAML11_PROTOCOL_ENUM)) {
84 // Check for SourceID extension element.
85 const Extensions* exts=(*i)->getExtensions();
87 const list<XMLObject*>& children=exts->getXMLObjects();
88 for (list<XMLObject*>::const_iterator ext=children.begin(); ext!=children.end(); ext++) {
89 SourceID* sid=dynamic_cast<SourceID*>(*ext);
91 auto_ptr_char sourceid(sid->getID());
93 m_sources.insert(pair<string,const EntityDescriptor*>(sourceid.get(),site));
102 pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
105 // Load endpoints for type 0x0002 artifacts.
106 const vector<ArtifactResolutionService*>& locs=const_cast<const IDPSSODescriptor*>(*i)->getArtifactResolutionServices();
107 for (vector<ArtifactResolutionService*>::const_iterator loc=locs.begin(); loc!=locs.end(); loc++) {
108 auto_ptr_char location((*loc)->getLocation());
110 m_sources.insert(pair<string,const EntityDescriptor*>(location.get(),site));
115 if ((*i)->hasSupport(SAMLConstants::SAML20P_NS)) {
118 pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
124 void AbstractMetadataProvider::index(EntitiesDescriptor* group, time_t validUntil)
126 if (validUntil < group->getValidUntilEpoch())
127 group->setValidUntil(validUntil);
129 auto_ptr_char name(group->getName());
131 m_groups.insert(make_pair(name.get(),group));
134 const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor*>(group)->getEntitiesDescriptors();
135 for (vector<EntitiesDescriptor*>::const_iterator i=groups.begin(); i!=groups.end(); i++)
136 index(*i,group->getValidUntilEpoch());
138 const vector<EntityDescriptor*>& sites=const_cast<const EntitiesDescriptor*>(group)->getEntityDescriptors();
139 for (vector<EntityDescriptor*>::const_iterator j=sites.begin(); j!=sites.end(); j++)
140 index(*j,group->getValidUntilEpoch());
143 void AbstractMetadataProvider::clearDescriptorIndex()
150 const EntitiesDescriptor* AbstractMetadataProvider::getEntitiesDescriptor(const char* name, bool strict) const
152 pair<groupmap_t::const_iterator,groupmap_t::const_iterator> range=m_groups.equal_range(name);
154 time_t now=time(NULL);
155 for (groupmap_t::const_iterator i=range.first; i!=range.second; i++)
156 if (now < i->second->getValidUntilEpoch())
159 if (!strict && range.first!=range.second)
160 return range.first->second;
165 const EntityDescriptor* AbstractMetadataProvider::getEntityDescriptor(const char* name, bool strict) const
167 pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sites.equal_range(name);
169 time_t now=time(NULL);
170 for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
171 if (now < i->second->getValidUntilEpoch())
174 if (!strict && range.first!=range.second)
175 return range.first->second;
180 const EntityDescriptor* AbstractMetadataProvider::getEntityDescriptor(const SAMLArtifact* artifact) const
182 pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sources.equal_range(artifact->getSource());
184 time_t now=time(NULL);
185 for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
186 if (now < i->second->getValidUntilEpoch())