Convert context API to use const objects and support chaining provider
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / MetadataProvider.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * @file saml/saml2/metadata/MetadataProvider.h
23  *
24  * Supplies an individual source of metadata.
25  */
26
27 #ifndef __saml2_metadataprov_h__
28 #define __saml2_metadataprov_h__
29
30 #include <saml/base.h>
31
32 #include <vector>
33 #include <iostream>
34 #include <boost/ptr_container/ptr_vector.hpp>
35 #include <xmltooling/exceptions.h>
36 #include <xmltooling/security/CredentialResolver.h>
37
38 namespace xmltooling {
39     class XMLTOOL_API QName;
40     class XMLTOOL_API XMLObject;
41 };
42
43 namespace opensaml {
44
45     class SAML_API SAMLArtifact;
46
47     namespace saml2md {
48
49         class SAML_API EntityDescriptor;
50         class SAML_API EntitiesDescriptor;
51         class SAML_API RoleDescriptor;
52         class SAML_API MetadataCredentialResolver;
53         class SAML_API MetadataFilter;
54         class SAML_API MetadataFilterContext;
55
56 #if defined (_MSC_VER)
57         #pragma warning( push )
58         #pragma warning( disable : 4251 )
59 #endif
60
61         /**
62          * Supplies an individual source of metadata.
63          *
64          * The source can be a local file, remote service, or the result of a
65          * dynamic lookup, can include local caching, etc. Providers
66          * <strong>MUST</strong> be locked before any lookup operations.
67          */
68         class SAML_API MetadataProvider : public virtual xmltooling::CredentialResolver
69         {
70             MAKE_NONCOPYABLE(MetadataProvider);
71         protected:
72             /**
73              * Constructor.
74              *
75              * If a DOM is supplied, a set of default logic will be used to identify
76              * and build MetadataFilter plugins and install them into the provider.
77              *
78              * The following XML content is supported:
79              *
80              * <ul>
81              *  <li>&lt;MetadataFilter&gt; elements with a type attribute and type-specific content
82              *  <li>&lt;Exclude&gt; elements representing a BlacklistMetadataFilter
83              *  <li>&lt;BlacklistMetadataFilter&gt; element containing &lt;Exclude&gt; elements
84              *  <li>&lt;Include&gt; elements representing a WhitelistMetadataFilter
85              *  <li>&lt;SignatureMetadataFilter&gt; element containing a &lt;KeyResolver&gt; element
86              *  <li>&lt;WhitelistMetadataFilter&gt; element containing &lt;Include&gt; elements
87              * </ul>
88              *
89              * XML namespaces are ignored in the processing of these elements.
90              *
91              * @param e DOM to supply configuration for provider
92              */
93             MetadataProvider(const xercesc::DOMElement* e=nullptr);
94
95         public:
96             /**
97              * Destructor will delete any installed filters.
98              */
99             virtual ~MetadataProvider();
100
101             /**
102              * Returns an identifier for the provider for logging/status purposes.
103              *
104              * @return an identifier, or null
105              */
106             virtual const char* getId() const;
107
108             /**
109              * Adds a metadata filter to apply to any resolved metadata. Will not be applied
110              * to metadata that is already loaded.
111              *
112              * @param newFilter metadata filter to add
113              */
114             virtual void addMetadataFilter(MetadataFilter* newFilter);
115
116             /**
117              * Removes a metadata filter. The caller must delete the filter if necessary.
118              *
119              * @param oldFilter metadata filter to remove
120              * @return  the old filter
121              */
122             virtual MetadataFilter* removeMetadataFilter(MetadataFilter* oldFilter);
123
124             /**
125              * Sets a filtering context object for use by the filtering process.
126              * <p>The object's lifetime must last for the duration of the lifetime
127              * of the MetadataProvider.
128              *
129              * @param ctx   a context object
130              */
131             void setContext(const MetadataFilterContext* ctx);
132
133             /**
134              * Should be called after instantiating provider and adding filters, but before
135              * performing any lookup operations. Allows the provider to defer initialization
136              * processes that are likely to result in exceptions until after the provider is
137              * safely created. Providers SHOULD perform as much processing as possible in
138              * this method so as to report/log any errors that would affect later processing.
139              */
140             virtual void init()=0;
141
142             /**
143              * Generate an XML representation of the provider's status. The XML must be
144              * well-formed, but is otherwise arbitrary.
145              *
146              * @param os    stream to write status information to
147              */
148             virtual void outputStatus(std::ostream& os) const;
149
150             /**
151              * Gets the entire metadata tree, after the registered filter has been applied.
152              * The caller MUST unlock the provider when finished with the data.
153              *
154              * @return the entire metadata tree
155              */
156             virtual const xmltooling::XMLObject* getMetadata() const=0;
157
158             /**
159              * Gets the metadata for a given group of entities. If a valid group is returned,
160              * the resolver will be left in a locked state. The caller MUST unlock the
161              * resolver when finished with the group.
162              *
163              * @param name                  the name of the group
164              * @param requireValidMetadata  indicates whether the metadata for the group must be valid/current
165              *
166              * @return the group's metadata or nullptr if there is no metadata or no valid metadata
167              */
168             virtual const EntitiesDescriptor* getEntitiesDescriptor(const XMLCh* name, bool requireValidMetadata=true) 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 nullptr if there is no metadata or no valid metadata
179              */
180             virtual const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const=0;
181
182             /**
183              * Batches up criteria for entity lookup.
184              */
185             struct SAML_API Criteria {
186                 /**
187                  * Default constructor.
188                  */
189                 Criteria();
190
191                 /**
192                  * Constructor.
193                  *
194                  * @param id    entityID to lookup
195                  * @param q     element/type of role, if any
196                  * @param prot  protocol support constant, if any
197                  * @param valid true iff stale metadata should be ignored
198                  */
199                 Criteria(const XMLCh* id, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
200
201                 /**
202                  * Constructor.
203                  *
204                  * @param id    entityID to lookup
205                  * @param q     element/type of role, if any
206                  * @param prot  protocol support constant, if any
207                  * @param valid true iff stale metadata should be ignored
208                  */
209                 Criteria(const char* id, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
210
211                 /**
212                  * Constructor.
213                  *
214                  * @param a     artifact to lookup
215                  * @param q     element/type of role, if any
216                  * @param prot  protocol support constant, if any
217                  * @param valid true iff stale metadata should be ignored
218                  */
219                 Criteria(const SAMLArtifact* a, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
220
221                 virtual ~Criteria();
222
223                 /**
224                  * Restores the object to its default state.
225                  */
226                 virtual void reset();
227
228                 /** Unique ID of entity. */
229                 const XMLCh* entityID_unicode;
230                 /** Unique ID of entity. */
231                 const char* entityID_ascii;
232                 /** SAML artifact */
233                 const SAMLArtifact* artifact;
234                 /** Element or schema type QName of metadata role. */
235                 const xmltooling::QName* role;
236                 /** Protocol support constant. */
237                 const XMLCh* protocol;
238                 /** Backup protocol support constant. */
239                 const XMLCh* protocol2;
240                 /** Controls whether stale metadata is ignored. */
241                 bool validOnly;
242             };
243
244             /**
245              * Gets entity metadata based on supplied criteria. If a valid entity is returned,
246              * the provider will be left in a locked state. The caller MUST unlock the
247              * provider when finished with the entity.
248              *
249              * @param criteria  lookup criteria
250              *
251              * @return the entity's metadata (and optionally a role) or nullptr if there is no qualifying metadata
252              */
253             virtual std::pair<const EntityDescriptor*,const RoleDescriptor*> getEntityDescriptor(const Criteria& criteria) const=0;
254
255         protected:
256             /**
257              * Applies any installed filters to a metadata instance.
258              *
259              * @param xmlObject the metadata to be filtered
260              */
261             void doFilters(xmltooling::XMLObject& xmlObject) const;
262
263         private:
264             const MetadataFilterContext* m_filterContext;
265             boost::ptr_vector<MetadataFilter> m_filters;
266         };
267
268 #if defined (_MSC_VER)
269         #pragma warning( pop )
270 #endif
271
272         /**
273          * Registers MetadataProvider classes into the runtime.
274          */
275         void SAML_API registerMetadataProviders();
276
277         /** MetadataProvider based on local or remote XML file */
278         #define XML_METADATA_PROVIDER  "XML"
279
280         /** MetadataProvider based on dynamic resolution */
281         #define DYNAMIC_METADATA_PROVIDER  "Dynamic"
282
283         /** MetadataProvider that wraps a sequence of metadata providers. */
284         #define CHAINING_METADATA_PROVIDER  "Chaining"
285
286         /** MetadataProvider that loads a directory of files. */
287         #define FOLDER_METADATA_PROVIDER  "Folder"
288
289         /** MetadataProvider that returns an empty "dummy" entity descriptor. */
290         #define NULL_METADATA_PROVIDER  "Null"
291
292         DECL_XMLTOOLING_EXCEPTION(MetadataException,SAML_EXCEPTIONAPI(SAML_API),opensaml::saml2md,xmltooling::XMLToolingException,Exceptions related to metadata use);
293     };
294 };
295
296 #endif /* __saml2_metadataprov_h__ */