Major revamp of credential and trust handling code, PKIX engine still needs work.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / AbstractMetadataProvider.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/AbstractMetadataProvider.h
19  * 
20  * Base class for caching metadata providers.
21  */
22
23 #ifndef __saml2_absmetadataprov_h__
24 #define __saml2_absmetadataprov_h__
25
26 #include <saml/saml2/metadata/ObservableMetadataProvider.h>
27
28 #include <xmltooling/security/Credential.h>
29 #include <xmltooling/security/CredentialCriteria.h>
30 #include <xmltooling/util/Threads.h>
31
32 namespace opensaml {
33     namespace saml2md {
34         
35         class SAML_API MetadataFilter;
36
37         /**
38          * Base class for caching metadata providers.
39          */
40         class SAML_API AbstractMetadataProvider : public ObservableMetadataProvider
41         {
42         protected:
43             /**
44              * Constructor.
45              * 
46              * If a DOM is supplied, a set of default logic will be used to identify
47              * and build a KeyInfoResolver plugin and install it into the provider.
48              * 
49              * The following XML content is supported:
50              * 
51              * <ul>
52              *  <li>&lt;KeyInfoResolver&gt; elements with a type attribute
53              * </ul>
54              * 
55              * XML namespaces are ignored in the processing of these elements.
56              * 
57              * @param e DOM to supply configuration for provider
58              */
59             AbstractMetadataProvider(const DOMElement* e=NULL);
60             
61         public:
62             virtual ~AbstractMetadataProvider();
63             
64             void emitChangeEvent();
65             const EntityDescriptor* getEntityDescriptor(const char* id, bool requireValidMetadata=true) const;
66             const EntityDescriptor* getEntityDescriptor(const SAMLArtifact* artifact) const;
67             const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const;
68             const xmltooling::Credential* resolve(const xmltooling::CredentialCriteria* criteria=NULL) const;
69             std::vector<const xmltooling::Credential*>::size_type resolve(
70                 std::vector<const xmltooling::Credential*>& results, const xmltooling::CredentialCriteria* criteria=NULL
71                 ) const;
72
73         protected:
74             /** Embedded KeyInfoResolver instance. */
75             xmltooling::KeyInfoResolver* m_resolver;
76
77             /**
78              * Loads an entity into the cache for faster lookup. This includes
79              * processing known reverse lookup strategies for artifacts.
80              * 
81              * @param site          entity definition
82              * @param validUntil    expiration time of the entity definition
83              */
84             virtual void index(EntityDescriptor* site, time_t validUntil);
85
86             /**
87              * Loads a group of entities into the cache for faster lookup.
88              * 
89              * @param group         group definition
90              * @param validUntil    expiration time of the group definition
91              */
92             virtual void index(EntitiesDescriptor* group, time_t validUntil);
93         
94             /**
95              * Clear the cache of known entities and groups.
96              */
97             virtual void clearDescriptorIndex();
98
99             /**
100              * Returns true iff the Credential matches the criteria supplied, if any.
101              *
102              * @param cred      Credential plus KeyDescriptor usage information
103              * @param criteria  criteria for Credential selection
104              * @return  true iff the Credential applies
105              */
106             virtual bool matches(
107                 const std::pair<const XMLCh*,xmltooling::Credential*>& cred, const xmltooling::CredentialCriteria* criteria
108                 ) const;
109         
110         private:
111             typedef std::multimap<std::string,const EntityDescriptor*> sitemap_t;
112             typedef std::multimap<std::string,const EntitiesDescriptor*> groupmap_t;
113             sitemap_t m_sites;
114             sitemap_t m_sources;
115             groupmap_t m_groups;
116
117             mutable xmltooling::Mutex* m_credentialLock;
118             typedef std::map<const RoleDescriptor*, std::vector< std::pair<const XMLCh*,xmltooling::Credential*> > > credmap_t;
119             mutable credmap_t m_credentialMap;
120             const credmap_t::mapped_type& resolveCredentials(const RoleDescriptor& role) const;
121         };
122         
123     };
124 };
125
126 #endif /* __saml2_absmetadataprov_h__ */