Moved key/cred resolution classes out of xmlsig namespace.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / AbstractMetadataProvider.cpp
1 /*
2  *  Copyright 2001-2007 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  * AbstractMetadataProvider.cpp
19  * 
20  * Base class for caching metadata providers.
21  */
22
23 #include "internal.h"
24 #include "binding/SAMLArtifact.h"
25 #include "saml2/metadata/Metadata.h"
26 #include "saml2/metadata/AbstractMetadataProvider.h"
27
28 #include <xercesc/util/XMLUniDefs.hpp>
29 #include <xmltooling/security/CachingKeyResolver.h>
30 #include <xmltooling/util/XMLHelper.h>
31
32 using namespace opensaml::saml2md;
33 using namespace opensaml;
34 using namespace xmltooling;
35 using namespace std;
36
37 static const XMLCh _KeyResolver[] = 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);
39
40 AbstractMetadataProvider::AbstractMetadataProvider(const DOMElement* e) : ObservableMetadataProvider(e), m_resolver(NULL)
41 {
42     e = e ? XMLHelper::getFirstChildElement(e, _KeyResolver) : NULL;
43     if (e) {
44         auto_ptr_char t(e->getAttributeNS(NULL,type));
45         if (t.get())
46             m_resolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(t.get(),e);
47         else
48             throw UnknownExtensionException("<KeyResolver> element found with no type attribute");
49     }
50     
51     if (!m_resolver) {
52         m_resolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(INLINE_KEY_RESOLVER, NULL);
53     }
54 }
55
56 AbstractMetadataProvider::~AbstractMetadataProvider()
57 {
58     delete m_resolver;
59 }
60
61 void AbstractMetadataProvider::emitChangeEvent()
62 {
63     CachingKeyResolver* ckr=dynamic_cast<CachingKeyResolver*>(m_resolver);
64     if (ckr)
65         ckr->clearCache();
66     ObservableMetadataProvider::emitChangeEvent();    
67 }
68
69 void AbstractMetadataProvider::index(EntityDescriptor* site, time_t validUntil)
70 {
71     if (validUntil < site->getValidUntilEpoch())
72         site->setValidUntil(validUntil);
73
74     auto_ptr_char id(site->getEntityID());
75     if (id.get()) {
76         m_sites.insert(make_pair(id.get(),site));
77     }
78     
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++) {
82         // SAML 1.x?
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();
86             if (exts && exts->hasChildren()) {
87                 const vector<XMLObject*>& children=exts->getUnknownXMLObjects();
88                 for (vector<XMLObject*>::const_iterator ext=children.begin(); ext!=children.end(); ++ext) {
89                     SourceID* sid=dynamic_cast<SourceID*>(*ext);
90                     if (sid) {
91                         auto_ptr_char sourceid(sid->getID());
92                         if (sourceid.get()) {
93                             m_sources.insert(pair<string,const EntityDescriptor*>(sourceid.get(),site));
94                             break;
95                         }
96                     }
97                 }
98             }
99             
100             // Hash the ID.
101             m_sources.insert(
102                 pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
103                 );
104                 
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());
109                 if (location.get())
110                     m_sources.insert(pair<string,const EntityDescriptor*>(location.get(),site));
111             }
112         }
113         
114         // SAML 2.0?
115         if ((*i)->hasSupport(samlconstants::SAML20P_NS)) {
116             // Hash the ID.
117             m_sources.insert(
118                 pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
119                 );
120         }
121     }
122 }
123
124 void AbstractMetadataProvider::index(EntitiesDescriptor* group, time_t validUntil)
125 {
126     if (validUntil < group->getValidUntilEpoch())
127         group->setValidUntil(validUntil);
128
129     auto_ptr_char name(group->getName());
130     if (name.get()) {
131         m_groups.insert(make_pair(name.get(),group));
132     }
133     
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());
137
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());
141 }
142
143 void AbstractMetadataProvider::clearDescriptorIndex()
144 {
145     m_sources.clear();
146     m_sites.clear();
147     m_groups.clear();
148 }
149
150 const EntitiesDescriptor* AbstractMetadataProvider::getEntitiesDescriptor(const char* name, bool strict) const
151 {
152     pair<groupmap_t::const_iterator,groupmap_t::const_iterator> range=m_groups.equal_range(name);
153
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())
157             return i->second;
158     
159     if (!strict && range.first!=range.second)
160         return range.first->second;
161         
162     return NULL;
163 }
164
165 const EntityDescriptor* AbstractMetadataProvider::getEntityDescriptor(const char* name, bool strict) const
166 {
167     pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sites.equal_range(name);
168
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())
172             return i->second;
173     
174     if (!strict && range.first!=range.second)
175         return range.first->second;
176         
177     return NULL;
178 }
179
180 const EntityDescriptor* AbstractMetadataProvider::getEntityDescriptor(const SAMLArtifact* artifact) const
181 {
182     pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sources.equal_range(artifact->getSource());
183
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())
187             return i->second;
188
189     return NULL;
190 }