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