Migrate to xmlsec 1.3 release, shrink a few headers.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / MetadataProvider.h
index eac8ea6..bc3f164 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 /**
- * @file MetadataProvider.h
+ * @file saml/saml2/metadata/MetadataProvider.h
  * 
  * Supplies an individual source of metadata.
  */
 #ifndef __saml2_metadataprov_h__
 #define __saml2_metadataprov_h__
 
-#include <xmltooling/Lockable.h>
 #include <saml/saml2/metadata/MetadataFilter.h>
 
+#include <xmltooling/Lockable.h>
+#include <xmltooling/signature/KeyResolver.h>
+
 namespace opensaml {
+    
+    class SAML_API SAMLArtifact;
 
     namespace saml2md {
+
+        class SAML_API EntityDescriptor;
+        class SAML_API EntitiesDescriptor;
         
         /**
          * Supplies an individual source of metadata.
          * 
          * The source can be a local file, remote service, or the result of a
-         * dynamic lookup, can include local caching, etc.
+         * dynamic lookup, can include local caching, etc. Providers
+         * <strong>MUST</strong> be locked before any lookup operations.
          */
         class SAML_API MetadataProvider : public virtual xmltooling::Lockable
         {
             MAKE_NONCOPYABLE(MetadataProvider);
+            
         protected:
-            MetadataProvider() : m_filter(NULL) {}
+            /**
+             * Constructor.
+             * 
+             * If a DOM is supplied, a set of default logic will be used to identify
+             * and build MetadataFilter plugins and install them into the provider.
+             * A KeyResolver can also be supplied, or a default resolver will be used.
+             * 
+             * The following XML content is supported:
+             * 
+             * <ul>
+             *  <li>&lt;KeyResolver&gt; elements with a type attribute
+             *  <li>&lt;MetadataFilter&gt; elements with a type attribute and type-specific content
+             *  <li>&lt;Exclude&gt; elements representing a BlacklistMetadataFilter
+             *  <li>&lt;BlacklistMetadataFilter&gt; element containing &lt;Exclude&gt; elements 
+             *  <li>&lt;Include&gt; elements representing a WhitelistMetadataFilter
+             *  <li>&lt;SignatureMetadataFilter&gt; element containing a &lt;KeyResolver&gt; element 
+             *  <li>&lt;WhitelistMetadataFilter&gt; element containing &lt;Include&gt; elements 
+             * </ul>
+             * 
+             * XML namespaces are ignored in the processing of these elements.
+             * 
+             * @param e DOM to supply configuration for provider
+             */
+            MetadataProvider(const DOMElement* e=NULL);
             
         public:
-            virtual ~MetadataProvider() {
-                delete m_filter;
-            }
+            /**
+             * Destructor will delete any installed filters.
+             */
+            virtual ~MetadataProvider();
             
             /**
-             * Gets the metadata filter applied to the resolved metadata.
+             * Adds a metadata filter to apply to any resolved metadata. Will not be applied
+             * to metadata that is already loaded.
              * 
-             * @return the metadata filter applied to the resolved metadata
+             * @param newFilter metadata filter to add
              */
-            const MetadataFilter* getMetadataFilter() const {
-                return m_filter;
+            virtual void addMetadataFilter(MetadataFilter* newFilter) {
+                m_filters.push_back(newFilter);
             }
-        
+
             /**
-             * Sets the metadata filter applied to the resolved metadata.
+             * Removes a metadata filter. The caller must delete the filter if necessary.
              * 
-             * @param newFilter the metadata filter applied to the resolved metadata
+             * @param oldFilter metadata filter to remove
+             * @return  the old filter
              */
-            void setMetadataFilter(MetadataFilter* newFilter) {
-                delete m_filter;
-                m_filter=newFilter;
+            virtual MetadataFilter* removeMetadataFilter(MetadataFilter* oldFilter) {
+                for (std::vector<MetadataFilter*>::iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
+                    if (oldFilter==(*i)) {
+                        m_filters.erase(i);
+                        return oldFilter;
+                    }
+                }
+                return NULL;
             }
             
             /**
-             * Should be called after instantiating provider and setting filter, but before
+             * Should be called after instantiating provider and adding filters, but before
              * performing any lookup operations. Allows the provider to defer initialization
              * processes that are likely to result in exceptions until after the provider is
              * safely created. Providers SHOULD perform as much processing as possible in
              * this method so as to report/log any errors that would affect later processing.
-             * Also, any inputs supplied to the factory MUST persist until the completion of
-             * this method, but the caller is then free to modify or delete them.
              */
             virtual void init()=0;
             
             /**
+             * Returns a KeyResolver associated with this metadata provider, if any.
+             * 
+             * @return an associated KeyResolver, or NULL
+             */
+            virtual const xmlsignature::KeyResolver* getKeyResolver() const {
+                return m_resolver;
+            }
+            
+            /**
+             * Gets the entire metadata tree, after the registered filter has been applied.
+             * The caller MUST unlock the provider when finished with the data.
+             * 
+             * @return the entire metadata tree
+             */
+            virtual const xmltooling::XMLObject* getMetadata() const=0;
+        
+            /**
              * Gets the metadata for a given entity. If a valid entity is returned,
              * the provider will be left in a locked state. The caller MUST unlock the
              * provider when finished with the entity.
@@ -87,7 +142,7 @@ namespace opensaml {
              * 
              * @return the entity's metadata or NULL if there is no metadata or no valid metadata
              */
-            virtual const EntityDescriptor* lookup(const XMLCh* id, bool requireValidMetadata=true) const=0;
+            virtual const EntityDescriptor* getEntityDescriptor(const XMLCh* id, bool requireValidMetadata=true) const;
 
             /**
              * Gets the metadata for a given entity. If a valid entity is returned,
@@ -99,7 +154,18 @@ namespace opensaml {
              * 
              * @return the entity's metadata or NULL if there is no metadata or no valid metadata
              */
-            virtual const EntityDescriptor* lookup(const char* id, bool requireValidMetadata=true) const=0;
+            virtual const EntityDescriptor* getEntityDescriptor(const char* id, bool requireValidMetadata=true) const;
+
+            /**
+             * Gets the metadata for an entity that issued a SAML artifact. If a valid entity is returned,
+             * the provider will be left in a locked state. The caller MUST unlock the
+             * provider when finished with the entity.
+             *  
+             * @param artifact              a SAML artifact to find the issuer of
+             * 
+             * @return the entity's metadata or NULL if there is no valid metadata
+             */
+            virtual const EntityDescriptor* getEntityDescriptor(const SAMLArtifact* artifact) const;
 
             /**
              * Gets the metadata for a given group of entities. If a valid group is returned,
@@ -111,7 +177,7 @@ namespace opensaml {
              * 
              * @return the group's metadata or NULL if there is no metadata or no valid metadata
              */
-            virtual const EntitiesDescriptor* lookupGroup(const XMLCh* name, bool requireValidMetadata=true) const=0;
+            virtual const EntitiesDescriptor* getEntitiesDescriptor(const XMLCh* name, bool requireValidMetadata=true) const;
 
             /**
              * Gets the metadata for a given group of entities. If a valid group is returned,
@@ -123,10 +189,49 @@ namespace opensaml {
              * 
              * @return the group's metadata or NULL if there is no metadata or no valid metadata
              */
-            virtual const EntitiesDescriptor* lookupGroup(const char* name, bool requireValidMetadata=true) const=0;
+            virtual const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const;
 
         protected:
-            MetadataFilter* m_filter;
+            /** Embedded KeyResolver instance. */
+            xmlsignature::KeyResolver* m_resolver;
+
+            /**
+             * Applies any installed filters to a metadata instance.
+             * 
+             * @param xmlObject the metadata to be filtered
+             */
+            void doFilters(xmltooling::XMLObject& xmlObject) const;
+
+            /**
+             * Loads an entity into the cache for faster lookup. This includes
+             * processing known reverse lookup strategies for artifacts.
+             * 
+             * @param site          entity definition
+             * @param validUntil    expiration time of the entity definition
+             */
+            virtual void index(EntityDescriptor* site, time_t validUntil);
+
+            /**
+             * Loads a group of entities into the cache for faster lookup.
+             * 
+             * @param group         group definition
+             * @param validUntil    expiration time of the group definition
+             */
+            virtual void index(EntitiesDescriptor* group, time_t validUntil);
+        
+            /**
+             * Clear the cache of known entities and groups.
+             */
+            virtual void clearDescriptorIndex();
+        
+        private:
+            std::vector<MetadataFilter*> m_filters;
+
+            typedef std::multimap<std::string,const EntityDescriptor*> sitemap_t;
+            typedef std::multimap<std::string,const EntitiesDescriptor*> groupmap_t;
+            sitemap_t m_sites;
+            sitemap_t m_sources;
+            groupmap_t m_groups;
         };
         
         /**