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