145b7639a60b3e2d54c1bb66323779e9895f3cd3
[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 <xmltooling/exceptions.h>
34 #include <xmltooling/security/CredentialResolver.h>
35
36 namespace xmltooling {
37     class XMLTOOL_API QName;
38     class XMLTOOL_API XMLObject;
39 };
40
41 namespace opensaml {
42
43     class SAML_API SAMLArtifact;
44
45     namespace saml2md {
46
47         class SAML_API EntityDescriptor;
48         class SAML_API EntitiesDescriptor;
49         class SAML_API RoleDescriptor;
50         class SAML_API MetadataCredentialResolver;
51         class SAML_API MetadataFilter;
52
53 #if defined (_MSC_VER)
54         #pragma warning( push )
55         #pragma warning( disable : 4251 )
56 #endif
57
58         /**
59          * Supplies an individual source of metadata.
60          *
61          * The source can be a local file, remote service, or the result of a
62          * dynamic lookup, can include local caching, etc. Providers
63          * <strong>MUST</strong> be locked before any lookup operations.
64          */
65         class SAML_API MetadataProvider : public virtual xmltooling::CredentialResolver
66         {
67             MAKE_NONCOPYABLE(MetadataProvider);
68         protected:
69             /**
70              * Constructor.
71              *
72              * If a DOM is supplied, a set of default logic will be used to identify
73              * and build MetadataFilter plugins and install them into the provider.
74              *
75              * The following XML content is supported:
76              *
77              * <ul>
78              *  <li>&lt;MetadataFilter&gt; elements with a type attribute and type-specific content
79              *  <li>&lt;Exclude&gt; elements representing a BlacklistMetadataFilter
80              *  <li>&lt;BlacklistMetadataFilter&gt; element containing &lt;Exclude&gt; elements
81              *  <li>&lt;Include&gt; elements representing a WhitelistMetadataFilter
82              *  <li>&lt;SignatureMetadataFilter&gt; element containing a &lt;KeyResolver&gt; element
83              *  <li>&lt;WhitelistMetadataFilter&gt; element containing &lt;Include&gt; elements
84              * </ul>
85              *
86              * XML namespaces are ignored in the processing of these elements.
87              *
88              * @param e DOM to supply configuration for provider
89              */
90             MetadataProvider(const xercesc::DOMElement* e=nullptr);
91
92         public:
93             /**
94              * Destructor will delete any installed filters.
95              */
96             virtual ~MetadataProvider();
97
98             /**
99              * Adds a metadata filter to apply to any resolved metadata. Will not be applied
100              * to metadata that is already loaded.
101              *
102              * @param newFilter metadata filter to add
103              */
104             virtual void addMetadataFilter(MetadataFilter* newFilter);
105
106             /**
107              * Removes a metadata filter. The caller must delete the filter if necessary.
108              *
109              * @param oldFilter metadata filter to remove
110              * @return  the old filter
111              */
112             virtual MetadataFilter* removeMetadataFilter(MetadataFilter* oldFilter);
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 nullptr 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 nullptr 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();
163
164                 /**
165                  * Constructor.
166                  *
167                  * @param id    entityID to lookup
168                  * @param q     element/type of role, if any
169                  * @param prot  protocol support constant, if any
170                  * @param valid true iff stale metadata should be ignored
171                  */
172                 Criteria(const XMLCh* id, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
173
174                 /**
175                  * Constructor.
176                  *
177                  * @param id    entityID to lookup
178                  * @param q     element/type of role, if any
179                  * @param prot  protocol support constant, if any
180                  * @param valid true iff stale metadata should be ignored
181                  */
182                 Criteria(const char* id, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
183
184                 /**
185                  * Constructor.
186                  *
187                  * @param a     artifact to lookup
188                  * @param q     element/type of role, if any
189                  * @param prot  protocol support constant, if any
190                  * @param valid true iff stale metadata should be ignored
191                  */
192                 Criteria(const SAMLArtifact* a, const xmltooling::QName* q=nullptr, const XMLCh* prot=nullptr, bool valid=true);
193
194                 virtual ~Criteria();
195
196                 /**
197                  * Restores the object to its default state.
198                  */
199                 virtual void reset();
200
201                 /** Unique ID of entity. */
202                 const XMLCh* entityID_unicode;
203                 /** Unique ID of entity. */
204                 const char* entityID_ascii;
205                 /** SAML artifact */
206                 const SAMLArtifact* artifact;
207                 /** Element or schema type QName of metadata role. */
208                 const xmltooling::QName* role;
209                 /** Protocol support constant. */
210                 const XMLCh* protocol;
211                 /** Backup protocol support constant. */
212                 const XMLCh* protocol2;
213                 /** Controls whether stale metadata is ignored. */
214                 bool validOnly;
215             };
216
217             /**
218              * Gets entity metadata based on supplied criteria. If a valid entity is returned,
219              * the provider will be left in a locked state. The caller MUST unlock the
220              * provider when finished with the entity.
221              *
222              * @param criteria  lookup criteria
223              *
224              * @return the entity's metadata (and optionally a role) or nullptr if there is no qualifying metadata
225              */
226             virtual std::pair<const EntityDescriptor*,const RoleDescriptor*> getEntityDescriptor(const Criteria& criteria) const=0;
227
228         protected:
229             /**
230              * Applies any installed filters to a metadata instance.
231              *
232              * @param xmlObject the metadata to be filtered
233              */
234             void doFilters(xmltooling::XMLObject& xmlObject) const;
235
236         private:
237             std::vector<MetadataFilter*> m_filters;
238         };
239
240 #if defined (_MSC_VER)
241         #pragma warning( pop )
242 #endif
243
244         /**
245          * Registers MetadataProvider classes into the runtime.
246          */
247         void SAML_API registerMetadataProviders();
248
249         /** MetadataProvider based on local or remote XML file */
250         #define XML_METADATA_PROVIDER  "XML"
251
252         /** MetadataProvider based on dynamic resolution */
253         #define DYNAMIC_METADATA_PROVIDER  "Dynamic"
254
255         /** MetadataProvider that wraps a sequence of metadata providers. */
256         #define CHAINING_METADATA_PROVIDER  "Chaining"
257
258         /** MetadataProvider that returns an empty "dummy" entity descriptor. */
259         #define NULL_METADATA_PROVIDER  "Null"
260
261         DECL_XMLTOOLING_EXCEPTION(MetadataException,SAML_EXCEPTIONAPI(SAML_API),opensaml::saml2md,xmltooling::XMLToolingException,Exceptions related to metadata use);
262     };
263 };
264
265 #endif /* __saml2_metadataprov_h__ */