Refactor contact handling
[shibboleth/cpp-opensaml.git] / saml / SAMLConfig.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/SAMLConfig.h
23  * 
24  * Library configuration.
25  */
26
27 #ifndef __saml_config_h__
28 #define __saml_config_h__
29
30 #include <saml/base.h>
31
32 #include <string>
33 #include <xercesc/dom/DOM.hpp>
34 #include <xmltooling/PluginManager.h>
35
36 /**
37  * @namespace opensaml
38  * Common classes for OpenSAML library
39  */
40 namespace opensaml {
41
42     class SAML_API ArtifactMap;
43     class SAML_API MessageEncoder;
44     class SAML_API MessageDecoder;
45     class SAML_API SAMLArtifact;
46     class SAML_API SecurityPolicyRule;
47
48     namespace saml2md {
49         class SAML_API ContactPerson;
50         class SAML_API EntityDescriptor;
51         class SAML_API MetadataProvider;
52         class SAML_API MetadataFilter;
53         class SAML_API RoleDescriptor;
54     };
55
56 #if defined (_MSC_VER)
57     #pragma warning( push )
58     #pragma warning( disable : 4250 4251 )
59 #endif
60
61     /**
62      * Singleton object that manages library startup/shutdown.configuration.
63      */
64     class SAML_API SAMLConfig
65     {
66     MAKE_NONCOPYABLE(SAMLConfig);
67     public:
68         virtual ~SAMLConfig();
69
70         /**
71          * Returns the global configuration object for the library.
72          * 
73          * @return reference to the global library configuration object
74          */
75         static SAMLConfig& getConfig();
76         
77         /**
78          * Initializes library
79          * 
80          * Each process using the library MUST call this function exactly once
81          * before using any library classes. The flag controls whether this is the
82          * "dominant" library or not and can allow the SAML library to be loaded
83          * as an extension of XMLTooling rather than subsuming it.
84          * 
85          * @param initXMLTooling true iff this method should initialize the XMLTooling layer
86          * @return true iff initialization was successful 
87          */
88         virtual bool init(bool initXMLTooling=true)=0;
89         
90         /**
91          * Shuts down library
92          * 
93          * Each process using the library SHOULD call this function exactly once
94          * before terminating itself. The flag controls whether this is the
95          * "dominant" library or not and can allow the SAML library to be loaded
96          * as an extension of XMLTooling rather than subsuming it.
97          * 
98          * @param termXMLTooling true iff this method should shutdown the XMLTooling layer
99          */
100         virtual void term(bool termXMLTooling=true)=0;
101         
102         /**
103          * Sets the global ArtifactMap instance.
104          * This method must be externally synchronized with any code that uses the object.
105          * Any previously set object is destroyed.
106          * 
107          * @param artifactMap   new ArtifactMap instance to store
108          */
109         void setArtifactMap(ArtifactMap* artifactMap);
110         
111         /**
112          * Returns the global ArtifactMap instance.
113          * 
114          * @return  global ArtifactMap or nullptr
115          */
116         ArtifactMap* getArtifactMap() const;
117
118         /**
119          * Generate random information using the underlying security library
120          * 
121          * @param buf   buffer for the information
122          * @param len   number of bytes to write into buffer
123          */
124         virtual void generateRandomBytes(void* buf, unsigned int len)=0;
125
126         /**
127          * Generate random information using the underlying security library
128          * 
129          * @param buf   string buffer for the information
130          * @param len   number of bytes to write into buffer
131          */
132         virtual void generateRandomBytes(std::string& buf, unsigned int len)=0;
133
134         /**
135          * Generate a valid XML identifier of the form _X{32} where X is a
136          * random hex character. The caller is responsible for freeing the result.
137          * 
138          * @return a valid null-terminated XML ID
139          */
140         virtual XMLCh* generateIdentifier()=0;
141         
142         /**
143          * @deprecated
144          * Generate the SHA-1 hash of a string
145          * 
146          * @param s     NULL-terminated string to hash
147          * @param toHex true iff the result should be encoded in hexadecimal form or left as raw bytes
148          *  
149          * @return  SHA-1 hash of the data
150          */
151         virtual std::string hashSHA1(const char* s, bool toHex=false)=0;
152
153         /**
154          * Sets the order of contact types to use in annotating exceptions with contact information.
155          *
156          * @param contactTypes  whitespace-delimited list of contact types
157          */
158         virtual void setContactPriority(const XMLCh* contactTypes)=0;
159
160         /**
161          * Returns the appropriate contact to use for the entity.
162          *
163          * @param entity    the entity to search
164          * @return  a contact to use, or nullptr
165          */
166         virtual const saml2md::ContactPerson* getContactPerson(const saml2md::EntityDescriptor& entity) const=0;
167
168         /**
169          * Returns the appropriate contact to use for the role.
170          *
171          * @param entity    the role to search
172          * @return  a contact to use, or nullptr
173          */
174         virtual const saml2md::ContactPerson* getContactPerson(const saml2md::RoleDescriptor& role) const=0;
175
176         /** Manages factories for MessageDecoder plugins. */
177         xmltooling::PluginManager< MessageDecoder,std::string,std::pair<const xercesc::DOMElement*,const XMLCh*> > MessageDecoderManager;
178
179         /** Manages factories for MessageEncoder plugins. */
180         xmltooling::PluginManager< MessageEncoder,std::string,std::pair<const xercesc::DOMElement*,const XMLCh*> > MessageEncoderManager;
181
182         /** Manages factories for SAMLArtifact plugins. */
183         xmltooling::PluginManager<SAMLArtifact,std::string,const char*> SAMLArtifactManager;
184
185         /** Manages factories for SecurityPolicyRule plugins. */
186         xmltooling::PluginManager<SecurityPolicyRule,std::string,const xercesc::DOMElement*> SecurityPolicyRuleManager;
187
188         /** Manages factories for MetadataProvider plugins. */
189         xmltooling::PluginManager<saml2md::MetadataProvider,std::string,const xercesc::DOMElement*> MetadataProviderManager;
190         
191         /** Manages factories for MetadataFilter plugins. */
192         xmltooling::PluginManager<saml2md::MetadataFilter,std::string,const xercesc::DOMElement*> MetadataFilterManager;
193
194     protected:
195         SAMLConfig();
196         
197         /** Global ArtifactMap instance for use by artifact-related functions. */
198         ArtifactMap* m_artifactMap;
199     };
200
201 #if defined (_MSC_VER)
202     #pragma warning( pop )
203 #endif
204     
205 };
206
207 #endif /* __saml_config_h__ */