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