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