Multi-line svn commit, see body.
[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 #include "saml2/metadata/MetadataCredentialContext.h"
28 #include "saml2/metadata/MetadataCredentialCriteria.h"
29
30 #include <xercesc/util/XMLUniDefs.hpp>
31 #include <xmltooling/security/KeyInfoResolver.h>
32 #include <xmltooling/util/XMLHelper.h>
33
34 using namespace opensaml::saml2md;
35 using namespace xmltooling;
36 using namespace std;
37 using opensaml::SAMLArtifact;
38
39 static const XMLCh _KeyInfoResolver[] = UNICODE_LITERAL_15(K,e,y,I,n,f,o,R,e,s,o,l,v,e,r);
40 static const XMLCh type[] =             UNICODE_LITERAL_4(t,y,p,e);
41
42 AbstractMetadataProvider::AbstractMetadataProvider(const DOMElement* e)
43     : ObservableMetadataProvider(e), m_resolver(NULL), m_credentialLock(NULL)
44 {
45     e = e ? XMLHelper::getFirstChildElement(e, _KeyInfoResolver) : NULL;
46     if (e) {
47         auto_ptr_char t(e->getAttributeNS(NULL,type));
48         if (t.get())
49             m_resolver = XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.get(),e);
50         else
51             throw UnknownExtensionException("<KeyInfoResolver> element found with no type attribute");
52     }
53     m_credentialLock = Mutex::create();
54 }
55
56 AbstractMetadataProvider::~AbstractMetadataProvider()
57 {
58     for (credmap_t::iterator c = m_credentialMap.begin(); c!=m_credentialMap.end(); ++c)
59         for_each(c->second.begin(), c->second.end(), xmltooling::cleanup<Credential>());
60     delete m_credentialLock;
61     delete m_resolver;
62 }
63
64 void AbstractMetadataProvider::emitChangeEvent() const
65 {
66     for (credmap_t::iterator c = m_credentialMap.begin(); c!=m_credentialMap.end(); ++c)
67         for_each(c->second.begin(), c->second.end(), xmltooling::cleanup<Credential>());
68     m_credentialMap.clear();
69     ObservableMetadataProvider::emitChangeEvent();
70 }
71
72 void AbstractMetadataProvider::index(EntityDescriptor* site, time_t validUntil)
73 {
74     if (validUntil < site->getValidUntilEpoch())
75         site->setValidUntil(validUntil);
76
77     auto_ptr_char id(site->getEntityID());
78     if (id.get()) {
79         m_sites.insert(sitemap_t::value_type(id.get(),site));
80     }
81     
82     // Process each IdP role.
83     const vector<IDPSSODescriptor*>& roles=const_cast<const EntityDescriptor*>(site)->getIDPSSODescriptors();
84     for (vector<IDPSSODescriptor*>::const_iterator i=roles.begin(); i!=roles.end(); i++) {
85         // SAML 1.x?
86         if ((*i)->hasSupport(samlconstants::SAML10_PROTOCOL_ENUM) || (*i)->hasSupport(samlconstants::SAML11_PROTOCOL_ENUM)) {
87             // Check for SourceID extension element.
88             const Extensions* exts=(*i)->getExtensions();
89             if (exts && exts->hasChildren()) {
90                 const vector<XMLObject*>& children=exts->getUnknownXMLObjects();
91                 for (vector<XMLObject*>::const_iterator ext=children.begin(); ext!=children.end(); ++ext) {
92                     SourceID* sid=dynamic_cast<SourceID*>(*ext);
93                     if (sid) {
94                         auto_ptr_char sourceid(sid->getID());
95                         if (sourceid.get()) {
96                             m_sources.insert(sitemap_t::value_type(sourceid.get(),site));
97                             break;
98                         }
99                     }
100                 }
101             }
102             
103             // Hash the ID.
104             m_sources.insert(sitemap_t::value_type(SAMLConfig::getConfig().hashSHA1(id.get(), true),site));
105                 
106             // Load endpoints for type 0x0002 artifacts.
107             const vector<ArtifactResolutionService*>& locs=const_cast<const IDPSSODescriptor*>(*i)->getArtifactResolutionServices();
108             for (vector<ArtifactResolutionService*>::const_iterator loc=locs.begin(); loc!=locs.end(); loc++) {
109                 auto_ptr_char location((*loc)->getLocation());
110                 if (location.get())
111                     m_sources.insert(sitemap_t::value_type(location.get(),site));
112             }
113         }
114         
115         // SAML 2.0?
116         if ((*i)->hasSupport(samlconstants::SAML20P_NS)) {
117             // Hash the ID.
118             m_sources.insert(sitemap_t::value_type(SAMLConfig::getConfig().hashSHA1(id.get(), true),site));
119         }
120     }
121 }
122
123 void AbstractMetadataProvider::index(EntitiesDescriptor* group, time_t validUntil)
124 {
125     if (validUntil < group->getValidUntilEpoch())
126         group->setValidUntil(validUntil);
127
128     auto_ptr_char name(group->getName());
129     if (name.get()) {
130         m_groups.insert(groupmap_t::value_type(name.get(),group));
131     }
132     
133     const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor*>(group)->getEntitiesDescriptors();
134     for (vector<EntitiesDescriptor*>::const_iterator i=groups.begin(); i!=groups.end(); i++)
135         index(*i,group->getValidUntilEpoch());
136
137     const vector<EntityDescriptor*>& sites=const_cast<const EntitiesDescriptor*>(group)->getEntityDescriptors();
138     for (vector<EntityDescriptor*>::const_iterator j=sites.begin(); j!=sites.end(); j++)
139         index(*j,group->getValidUntilEpoch());
140 }
141
142 void AbstractMetadataProvider::clearDescriptorIndex()
143 {
144     m_sources.clear();
145     m_sites.clear();
146     m_groups.clear();
147 }
148
149 const EntitiesDescriptor* AbstractMetadataProvider::getEntitiesDescriptor(const char* name, bool strict) const
150 {
151     pair<groupmap_t::const_iterator,groupmap_t::const_iterator> range=m_groups.equal_range(name);
152
153     time_t now=time(NULL);
154     for (groupmap_t::const_iterator i=range.first; i!=range.second; i++)
155         if (now < i->second->getValidUntilEpoch())
156             return i->second;
157     
158     if (!strict && range.first!=range.second)
159         return range.first->second;
160         
161     return NULL;
162 }
163
164 const EntityDescriptor* AbstractMetadataProvider::getEntityDescriptor(const char* name, bool strict) const
165 {
166     pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sites.equal_range(name);
167
168     time_t now=time(NULL);
169     for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
170         if (now < i->second->getValidUntilEpoch())
171             return i->second;
172     
173     if (!strict && range.first!=range.second)
174         return range.first->second;
175         
176     return NULL;
177 }
178
179 const EntityDescriptor* AbstractMetadataProvider::getEntityDescriptor(const SAMLArtifact* artifact) const
180 {
181     pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sources.equal_range(artifact->getSource());
182
183     time_t now=time(NULL);
184     for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
185         if (now < i->second->getValidUntilEpoch())
186             return i->second;
187
188     return NULL;
189 }
190
191 const Credential* AbstractMetadataProvider::resolve(const CredentialCriteria* criteria) const
192 {
193     const MetadataCredentialCriteria* metacrit = dynamic_cast<const MetadataCredentialCriteria*>(criteria);
194     if (!metacrit)
195         throw MetadataException("Cannot resolve credentials without a MetadataCredentialCriteria object.");
196
197     Lock lock(m_credentialLock);
198     const credmap_t::mapped_type& creds = resolveCredentials(metacrit->getRole());
199
200     for (credmap_t::mapped_type::const_iterator c = creds.begin(); c!=creds.end(); ++c)
201         if (metacrit->matches(*(*c)))
202             return *c;
203     return NULL;
204 }
205
206 vector<const Credential*>::size_type AbstractMetadataProvider::resolve(
207     vector<const Credential*>& results, const CredentialCriteria* criteria
208     ) const
209 {
210     const MetadataCredentialCriteria* metacrit = dynamic_cast<const MetadataCredentialCriteria*>(criteria);
211     if (!metacrit)
212         throw MetadataException("Cannot resolve credentials without a MetadataCredentialCriteria object.");
213
214     Lock lock(m_credentialLock);
215     const credmap_t::mapped_type& creds = resolveCredentials(metacrit->getRole());
216
217     for (credmap_t::mapped_type::const_iterator c = creds.begin(); c!=creds.end(); ++c)
218         if (metacrit->matches(*(*c)))
219             results.push_back(*c);
220     return results.size();
221 }
222
223 const AbstractMetadataProvider::credmap_t::mapped_type& AbstractMetadataProvider::resolveCredentials(const RoleDescriptor& role) const
224 {
225     credmap_t::const_iterator i = m_credentialMap.find(&role);
226     if (i!=m_credentialMap.end())
227         return i->second;
228
229     const KeyInfoResolver* resolver = m_resolver ? m_resolver : XMLToolingConfig::getConfig().getKeyInfoResolver();
230     const vector<KeyDescriptor*>& keys = role.getKeyDescriptors();
231     AbstractMetadataProvider::credmap_t::mapped_type& resolved = m_credentialMap[&role];
232     for (vector<KeyDescriptor*>::const_iterator k = keys.begin(); k!=keys.end(); ++k) {
233         if ((*k)->getKeyInfo()) {
234             auto_ptr<MetadataCredentialContext> mcc(new MetadataCredentialContext(*(*k)));
235             Credential* c = resolver->resolve(mcc.get());
236             mcc.release();
237             resolved.push_back(c);
238         }
239     }
240     return resolved;
241 }