Major revamp of credential and trust handling code, PKIX engine still needs work.
[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 <xmltooling/XMLObject.h>
27 #include <xmltooling/security/CredentialResolver.h>
28
29 namespace opensaml {
30     
31     class SAML_API SAMLArtifact;
32
33     namespace saml2md {
34
35         class SAML_API EntityDescriptor;
36         class SAML_API EntitiesDescriptor;
37         class SAML_API RoleDescriptor;
38         class SAML_API MetadataCredentialResolver;
39         class SAML_API MetadataFilter;
40
41 #if defined (_MSC_VER)
42         #pragma warning( push )
43         #pragma warning( disable : 4251 )
44 #endif
45
46         /**
47          * Supplies an individual source of metadata.
48          * 
49          * The source can be a local file, remote service, or the result of a
50          * dynamic lookup, can include local caching, etc. Providers
51          * <strong>MUST</strong> be locked before any lookup operations.
52          */
53         class SAML_API MetadataProvider : public virtual xmltooling::CredentialResolver
54         {
55             MAKE_NONCOPYABLE(MetadataProvider);
56         protected:
57             /**
58              * Constructor.
59              * 
60              * If a DOM is supplied, a set of default logic will be used to identify
61              * and build MetadataFilter plugins and install them into the provider.
62              * 
63              * The following XML content is supported:
64              * 
65              * <ul>
66              *  <li>&lt;MetadataFilter&gt; elements with a type attribute and type-specific content
67              *  <li>&lt;Exclude&gt; elements representing a BlacklistMetadataFilter
68              *  <li>&lt;BlacklistMetadataFilter&gt; element containing &lt;Exclude&gt; elements 
69              *  <li>&lt;Include&gt; elements representing a WhitelistMetadataFilter
70              *  <li>&lt;SignatureMetadataFilter&gt; element containing a &lt;KeyResolver&gt; element 
71              *  <li>&lt;WhitelistMetadataFilter&gt; element containing &lt;Include&gt; elements 
72              * </ul>
73              * 
74              * XML namespaces are ignored in the processing of these elements.
75              * 
76              * @param e DOM to supply configuration for provider
77              */
78             MetadataProvider(const DOMElement* e=NULL);
79             
80         public:
81             /**
82              * Destructor will delete any installed filters.
83              */
84             virtual ~MetadataProvider();
85             
86             /**
87              * Adds a metadata filter to apply to any resolved metadata. Will not be applied
88              * to metadata that is already loaded.
89              * 
90              * @param newFilter metadata filter to add
91              */
92             virtual void addMetadataFilter(MetadataFilter* newFilter) {
93                 m_filters.push_back(newFilter);
94             }
95
96             /**
97              * Removes a metadata filter. The caller must delete the filter if necessary.
98              * 
99              * @param oldFilter metadata filter to remove
100              * @return  the old filter
101              */
102             virtual MetadataFilter* removeMetadataFilter(MetadataFilter* oldFilter) {
103                 for (std::vector<MetadataFilter*>::iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
104                     if (oldFilter==(*i)) {
105                         m_filters.erase(i);
106                         return oldFilter;
107                     }
108                 }
109                 return NULL;
110             }
111             
112             /**
113              * Should be called after instantiating provider and adding filters, but before
114              * performing any lookup operations. Allows the provider to defer initialization
115              * processes that are likely to result in exceptions until after the provider is
116              * safely created. Providers SHOULD perform as much processing as possible in
117              * this method so as to report/log any errors that would affect later processing.
118              */
119             virtual void init()=0;
120             
121             /**
122              * Gets the entire metadata tree, after the registered filter has been applied.
123              * The caller MUST unlock the provider when finished with the data.
124              * 
125              * @return the entire metadata tree
126              */
127             virtual const xmltooling::XMLObject* getMetadata() const=0;
128         
129             /**
130              * Gets the metadata for a given entity. If a valid entity is returned,
131              * the provider will be left in a locked state. The caller MUST unlock the
132              * provider when finished with the entity.
133              *  
134              * @param id                    the ID of the entity
135              * @param requireValidMetadata  indicates whether the metadata for the entity must be valid/current
136              * 
137              * @return the entity's metadata or NULL if there is no metadata or no valid metadata
138              */
139             virtual const EntityDescriptor* getEntityDescriptor(const XMLCh* id, bool requireValidMetadata=true) const;
140
141             /**
142              * Gets the metadata for a given entity. If a valid entity is returned,
143              * the provider will be left in a locked state. The caller MUST unlock the
144              * provider when finished with the entity.
145              *  
146              * @param id                    the ID of the entity
147              * @param requireValidMetadata  indicates whether the metadata for the entity must be valid/current
148              * 
149              * @return the entity's metadata or NULL if there is no metadata or no valid metadata
150              */
151             virtual const EntityDescriptor* getEntityDescriptor(const char* id, bool requireValidMetadata=true) const=0;
152
153             /**
154              * Gets the metadata for an entity that issued a SAML artifact. If a valid entity is returned,
155              * the provider will be left in a locked state. The caller MUST unlock the
156              * provider when finished with the entity.
157              *  
158              * @param artifact              a SAML artifact to find the issuer of
159              * 
160              * @return the entity's metadata or NULL if there is no valid metadata
161              */
162             virtual const EntityDescriptor* getEntityDescriptor(const SAMLArtifact* artifact) const=0;
163
164             /**
165              * Gets the metadata for a given group of entities. If a valid group is returned,
166              * the resolver will be left in a locked state. The caller MUST unlock the
167              * resolver when finished with the group.
168              * 
169              * @param name                  the name of the group
170              * @param requireValidMetadata  indicates whether the metadata for the group must be valid/current
171              * 
172              * @return the group's metadata or NULL if there is no metadata or no valid metadata
173              */
174             virtual const EntitiesDescriptor* getEntitiesDescriptor(const XMLCh* name, bool requireValidMetadata=true) const;
175
176             /**
177              * Gets the metadata for a given group of entities. If a valid group is returned,
178              * the resolver will be left in a locked state. The caller MUST unlock the
179              * resolver when finished with the group.
180              * 
181              * @param name                  the name of the group
182              * @param requireValidMetadata  indicates whether the metadata for the group must be valid/current
183              * 
184              * @return the group's metadata or NULL if there is no metadata or no valid metadata
185              */
186             virtual const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const=0;
187
188         protected:
189             /**
190              * Applies any installed filters to a metadata instance.
191              * 
192              * @param xmlObject the metadata to be filtered
193              */
194             void doFilters(xmltooling::XMLObject& xmlObject) const;
195
196         private:
197             std::vector<MetadataFilter*> m_filters;
198         };
199
200 #if defined (_MSC_VER)
201         #pragma warning( pop )
202 #endif
203
204         /**
205          * Registers MetadataProvider classes into the runtime.
206          */
207         void SAML_API registerMetadataProviders();
208         
209         /** MetadataProvider based on local or remote XML file */
210         #define XML_METADATA_PROVIDER  "XML"
211
212         /** MetadataProvider that wraps a sequence of metadata providers. */
213         #define CHAINING_METADATA_PROVIDER  "Chaining"
214
215         DECL_XMLTOOLING_EXCEPTION(MetadataException,SAML_EXCEPTIONAPI(SAML_API),opensaml::saml2md,xmltooling::XMLToolingException,Exceptions related to metadata use);
216     };
217 };
218
219 #endif /* __saml2_metadataprov_h__ */