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