f773e411c378896735a55e7d99a49d6911d8542b
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / MetadataProvider.h
1 /*
2  *  Copyright 2001-2006 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  * @file saml/saml2/metadata/MetadataProvider.h
19  * 
20  * Supplies an individual source of metadata.
21  */
22
23 #ifndef __saml2_metadataprov_h__
24 #define __saml2_metadataprov_h__
25
26 #include <saml/saml2/metadata/MetadataFilter.h>
27
28 #include <xmltooling/Lockable.h>
29 #include <xmltooling/signature/KeyResolver.h>
30
31 namespace opensaml {
32     
33     class SAML_API SAMLArtifact;
34
35     namespace saml2md {
36         
37         /**
38          * Supplies an individual source of metadata.
39          * 
40          * The source can be a local file, remote service, or the result of a
41          * dynamic lookup, can include local caching, etc. Providers
42          * <strong>MUST</strong> be locked before any lookup operations.
43          */
44         class SAML_API MetadataProvider : public virtual xmltooling::Lockable
45         {
46             MAKE_NONCOPYABLE(MetadataProvider);
47             
48         protected:
49             /**
50              * Constructor.
51              * 
52              * If a DOM is supplied, a set of default logic will be used to identify
53              * and build MetadataFilter plugins and install them into the provider.
54              * A KeyResolver can also be supplied, or a default resolver will be used.
55              * 
56              * The following XML content is supported:
57              * 
58              * <ul>
59              *  <li>&lt;KeyResolver&gt; elements with a type attribute
60              *  <li>&lt;MetadataFilter&gt; elements with a type attribute
61              *  <li>&lt;Exclude&gt; elements representing a BlacklistMetadataFilter
62              *  <li>&lt;BlacklistMetadataFilter&gt; element containing &lt;Exclude&gt; elements 
63              *  <li>&lt;Include&gt; elements representing a WhitelistMetadataFilter
64              *  <li>&lt;WhitelistMetadataFilter&gt; element containing &lt;Include&gt; elements 
65              * </ul>
66              * 
67              * XML namespaces are ignored in the processing of these elements.
68              * 
69              * @param e DOM to supply configuration for provider
70              */
71             MetadataProvider(const DOMElement* e=NULL);
72             
73         public:
74             /**
75              * Destructor will delete any installed filters.
76              */
77             virtual ~MetadataProvider();
78             
79             /**
80              * Adds a metadata filter to apply to any resolved metadata. Will not be applied
81              * to metadata that is already loaded.
82              * 
83              * @param newFilter metadata filter to add
84              */
85             virtual void addMetadataFilter(MetadataFilter* newFilter) {
86                 m_filters.push_back(newFilter);
87             }
88
89             /**
90              * Removes a metadata filter. The caller must delete the filter if necessary.
91              * 
92              * @param oldFilter metadata filter to remove
93              * @return  the old filter
94              */
95             virtual MetadataFilter* removeMetadataFilter(MetadataFilter* oldFilter) {
96                 for (std::vector<MetadataFilter*>::iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
97                     if (oldFilter==(*i)) {
98                         m_filters.erase(i);
99                         return oldFilter;
100                     }
101                 }
102                 return NULL;
103             }
104             
105             /**
106              * Should be called after instantiating provider and adding filters, but before
107              * performing any lookup operations. Allows the provider to defer initialization
108              * processes that are likely to result in exceptions until after the provider is
109              * safely created. Providers SHOULD perform as much processing as possible in
110              * this method so as to report/log any errors that would affect later processing.
111              */
112             virtual void init()=0;
113             
114             /**
115              * Returns a KeyResolver associated with this metadata provider, if any.
116              * 
117              * @return an associated KeyResolver, or NULL
118              */
119             virtual const xmlsignature::KeyResolver* getKeyResolver() const {
120                 return m_resolver;
121             }
122             
123             /**
124              * Gets the entire metadata tree, after the registered filter has been applied.
125              * The caller MUST unlock the provider when finished with the data.
126              * 
127              * @return the entire metadata tree
128              */
129             virtual const xmltooling::XMLObject* getMetadata() const=0;
130         
131             /**
132              * Gets the metadata for a given entity. If a valid entity is returned,
133              * the provider will be left in a locked state. The caller MUST unlock the
134              * provider when finished with the entity.
135              *  
136              * @param id                    the ID of the entity
137              * @param requireValidMetadata  indicates whether the metadata for the entity must be valid/current
138              * 
139              * @return the entity's metadata or NULL if there is no metadata or no valid metadata
140              */
141             virtual const EntityDescriptor* getEntityDescriptor(const XMLCh* id, bool requireValidMetadata=true) const;
142
143             /**
144              * Gets the metadata for a given entity. If a valid entity is returned,
145              * the provider will be left in a locked state. The caller MUST unlock the
146              * provider when finished with the entity.
147              *  
148              * @param id                    the ID of the entity
149              * @param requireValidMetadata  indicates whether the metadata for the entity must be valid/current
150              * 
151              * @return the entity's metadata or NULL if there is no metadata or no valid metadata
152              */
153             virtual const EntityDescriptor* getEntityDescriptor(const char* id, bool requireValidMetadata=true) const;
154
155             /**
156              * Gets the metadata for an entity that issued a SAML artifact. If a valid entity is returned,
157              * the provider will be left in a locked state. The caller MUST unlock the
158              * provider when finished with the entity.
159              *  
160              * @param artifact              a SAML artifact to find the issuer of
161              * 
162              * @return the entity's metadata or NULL if there is no valid metadata
163              */
164             virtual const EntityDescriptor* getEntityDescriptor(const SAMLArtifact* artifact) const;
165
166             /**
167              * Gets the metadata for a given group of entities. If a valid group is returned,
168              * the resolver will be left in a locked state. The caller MUST unlock the
169              * resolver when finished with the group.
170              * 
171              * @param name                  the name of the group
172              * @param requireValidMetadata  indicates whether the metadata for the group must be valid/current
173              * 
174              * @return the group's metadata or NULL if there is no metadata or no valid metadata
175              */
176             virtual const EntitiesDescriptor* getEntitiesDescriptor(const XMLCh* name, bool requireValidMetadata=true) const;
177
178             /**
179              * Gets the metadata for a given group of entities. If a valid group is returned,
180              * the resolver will be left in a locked state. The caller MUST unlock the
181              * resolver when finished with the group.
182              * 
183              * @param name                  the name of the group
184              * @param requireValidMetadata  indicates whether the metadata for the group must be valid/current
185              * 
186              * @return the group's metadata or NULL if there is no metadata or no valid metadata
187              */
188             virtual const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const;
189
190         protected:
191             /** Embedded KeyResolver instance. */
192             xmlsignature::KeyResolver* m_resolver;
193
194             /**
195              * Applies any installed filters to a metadata instance.
196              * 
197              * @param xmlObject the metadata to be filtered
198              */
199             void doFilters(xmltooling::XMLObject& xmlObject) const;
200
201             /**
202              * Loads an entity into the cache for faster lookup. This includes
203              * processing known reverse lookup strategies for artifacts.
204              * 
205              * @param site          entity definition
206              * @param validUntil    expiration time of the entity definition
207              */
208             virtual void index(EntityDescriptor* site, time_t validUntil);
209
210             /**
211              * Loads a group of entities into the cache for faster lookup.
212              * 
213              * @param group         group definition
214              * @param validUntil    expiration time of the group definition
215              */
216             virtual void index(EntitiesDescriptor* group, time_t validUntil);
217         
218             /**
219              * Clear the cache of known entities and groups.
220              */
221             virtual void clearDescriptorIndex();
222         
223         private:
224             std::vector<MetadataFilter*> m_filters;
225
226             typedef std::multimap<std::string,const EntityDescriptor*> sitemap_t;
227             typedef std::multimap<std::string,const EntitiesDescriptor*> groupmap_t;
228             sitemap_t m_sites;
229             sitemap_t m_sources;
230             groupmap_t m_groups;
231         };
232         
233         /**
234          * Registers MetadataProvider classes into the runtime.
235          */
236         void SAML_API registerMetadataProviders();
237         
238         /** MetadataProvider based on local XML files */
239         #define FILESYSTEM_METADATA_PROVIDER  "org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider"
240     };
241 };
242
243 #endif /* __saml2_metadataprov_h__ */