Add API for status generation.
[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 <xmltooling/exceptions.h>
35 #include <xmltooling/security/CredentialResolver.h>
36
37 namespace xmltooling {
38     class XMLTOOL_API QName;
39     class XMLTOOL_API XMLObject;
40 };
41
42 namespace opensaml {
43
44     class SAML_API SAMLArtifact;
45
46     namespace saml2md {
47
48         class SAML_API EntityDescriptor;
49         class SAML_API EntitiesDescriptor;
50         class SAML_API RoleDescriptor;
51         class SAML_API MetadataCredentialResolver;
52         class SAML_API MetadataFilter;
53
54 #if defined (_MSC_VER)
55         #pragma warning( push )
56         #pragma warning( disable : 4251 )
57 #endif
58
59         /**
60          * Supplies an individual source of metadata.
61          *
62          * The source can be a local file, remote service, or the result of a
63          * dynamic lookup, can include local caching, etc. Providers
64          * <strong>MUST</strong> be locked before any lookup operations.
65          */
66         class SAML_API MetadataProvider : public virtual xmltooling::CredentialResolver
67         {
68             MAKE_NONCOPYABLE(MetadataProvider);
69         protected:
70             /**
71              * Constructor.
72              *
73              * If a DOM is supplied, a set of default logic will be used to identify
74              * and build MetadataFilter plugins and install them into the provider.
75              *
76              * The following XML content is supported:
77              *
78              * <ul>
79              *  <li>&lt;MetadataFilter&gt; elements with a type attribute and type-specific content
80              *  <li>&lt;Exclude&gt; elements representing a BlacklistMetadataFilter
81              *  <li>&lt;BlacklistMetadataFilter&gt; element containing &lt;Exclude&gt; elements
82              *  <li>&lt;Include&gt; elements representing a WhitelistMetadataFilter
83              *  <li>&lt;SignatureMetadataFilter&gt; element containing a &lt;KeyResolver&gt; element
84              *  <li>&lt;WhitelistMetadataFilter&gt; element containing &lt;Include&gt; elements
85              * </ul>
86              *
87              * XML namespaces are ignored in the processing of these elements.
88              *
89              * @param e DOM to supply configuration for provider
90              */
91             MetadataProvider(const xercesc::DOMElement* e=nullptr);
92
93         public:
94             /**
95              * Destructor will delete any installed filters.
96              */
97             virtual ~MetadataProvider();
98
99             /**
100              * Adds a metadata filter to apply to any resolved metadata. Will not be applied
101              * to metadata that is already loaded.
102              *
103              * @param newFilter metadata filter to add
104              */
105             virtual void addMetadataFilter(MetadataFilter* newFilter);
106
107             /**
108              * Removes a metadata filter. The caller must delete the filter if necessary.
109              *
110              * @param oldFilter metadata filter to remove
111              * @return  the old filter
112              */
113             virtual MetadataFilter* removeMetadataFilter(MetadataFilter* oldFilter);
114
115             /**
116              * Should be called after instantiating provider and adding filters, but before
117              * performing any lookup operations. Allows the provider to defer initialization
118              * processes that are likely to result in exceptions until after the provider is
119              * safely created. Providers SHOULD perform as much processing as possible in
120              * this method so as to report/log any errors that would affect later processing.
121              */
122             virtual void init()=0;
123
124             /**
125              * Generate an XML representation of the provider's status. The XML must be
126              * well-formed, but is otherwise arbitrary.
127              *
128              * @param os    stream to write status information to
129              */
130             virtual void outputStatus(std::ostream& os) const;
131
132             /**
133              * Gets the entire metadata tree, after the registered filter has been applied.
134              * The caller MUST unlock the provider when finished with the data.
135              *
136              * @return the entire metadata tree
137              */
138             virtual const xmltooling::XMLObject* getMetadata() const=0;
139
140             /**
141              * Gets the metadata for a given group of entities. If a valid group is returned,
142              * the resolver will be left in a locked state. The caller MUST unlock the
143              * resolver when finished with the group.
144              *
145              * @param name                  the name of the group
146              * @param requireValidMetadata  indicates whether the metadata for the group must be valid/current
147              *
148              * @return the group's metadata or nullptr if there is no metadata or no valid metadata
149              */
150             virtual const EntitiesDescriptor* getEntitiesDescriptor(const XMLCh* name, bool requireValidMetadata=true) const;
151
152             /**
153              * Gets the metadata for a given group of entities. If a valid group is returned,
154              * the resolver will be left in a locked state. The caller MUST unlock the
155              * resolver when finished with the group.
156              *
157              * @param name                  the name of the group
158              * @param requireValidMetadata  indicates whether the metadata for the group must be valid/current
159              *
160              * @return the group's metadata or nullptr if there is no metadata or no valid metadata
161              */
162             virtual const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const=0;
163
164             /**
165              * Batches up criteria for entity lookup.
166              */
167             struct SAML_API Criteria {
168                 /**
169                  * Default constructor.
170                  */
171                 Criteria();
172
173                 /**
174                  * Constructor.
175                  *
176                  * @param id    entityID to lookup
177                  * @param q     element/type of role, if any
178                  * @param prot  protocol support constant, if any
179                  * @param valid true iff stale metadata should be ignored
180                  */
181                 Criteria(const XMLCh* id, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
182
183                 /**
184                  * Constructor.
185                  *
186                  * @param id    entityID to lookup
187                  * @param q     element/type of role, if any
188                  * @param prot  protocol support constant, if any
189                  * @param valid true iff stale metadata should be ignored
190                  */
191                 Criteria(const char* id, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
192
193                 /**
194                  * Constructor.
195                  *
196                  * @param a     artifact to lookup
197                  * @param q     element/type of role, if any
198                  * @param prot  protocol support constant, if any
199                  * @param valid true iff stale metadata should be ignored
200                  */
201                 Criteria(const SAMLArtifact* a, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
202
203                 virtual ~Criteria();
204
205                 /**
206                  * Restores the object to its default state.
207                  */
208                 virtual void reset();
209
210                 /** Unique ID of entity. */
211                 const XMLCh* entityID_unicode;
212                 /** Unique ID of entity. */
213                 const char* entityID_ascii;
214                 /** SAML artifact */
215                 const SAMLArtifact* artifact;
216                 /** Element or schema type QName of metadata role. */
217                 const xmltooling::QName* role;
218                 /** Protocol support constant. */
219                 const XMLCh* protocol;
220                 /** Backup protocol support constant. */
221                 const XMLCh* protocol2;
222                 /** Controls whether stale metadata is ignored. */
223                 bool validOnly;
224             };
225
226             /**
227              * Gets entity metadata based on supplied criteria. If a valid entity is returned,
228              * the provider will be left in a locked state. The caller MUST unlock the
229              * provider when finished with the entity.
230              *
231              * @param criteria  lookup criteria
232              *
233              * @return the entity's metadata (and optionally a role) or nullptr if there is no qualifying metadata
234              */
235             virtual std::pair<const EntityDescriptor*,const RoleDescriptor*> getEntityDescriptor(const Criteria& criteria) const=0;
236
237         protected:
238             /**
239              * Applies any installed filters to a metadata instance.
240              *
241              * @param xmlObject the metadata to be filtered
242              */
243             void doFilters(xmltooling::XMLObject& xmlObject) const;
244
245         private:
246             std::vector<MetadataFilter*> m_filters;
247         };
248
249 #if defined (_MSC_VER)
250         #pragma warning( pop )
251 #endif
252
253         /**
254          * Registers MetadataProvider classes into the runtime.
255          */
256         void SAML_API registerMetadataProviders();
257
258         /** MetadataProvider based on local or remote XML file */
259         #define XML_METADATA_PROVIDER  "XML"
260
261         /** MetadataProvider based on dynamic resolution */
262         #define DYNAMIC_METADATA_PROVIDER  "Dynamic"
263
264         /** MetadataProvider that wraps a sequence of metadata providers. */
265         #define CHAINING_METADATA_PROVIDER  "Chaining"
266
267         /** MetadataProvider that returns an empty "dummy" entity descriptor. */
268         #define NULL_METADATA_PROVIDER  "Null"
269
270         DECL_XMLTOOLING_EXCEPTION(MetadataException,SAML_EXCEPTIONAPI(SAML_API),opensaml::saml2md,xmltooling::XMLToolingException,Exceptions related to metadata use);
271     };
272 };
273
274 #endif /* __saml2_metadataprov_h__ */