Support multiple contacts.
[shibboleth/sp.git] / shib / shib.h
1 /*
2  * The Shibboleth License, Version 1.
3  * Copyright (c) 2002
4  * University Corporation for Advanced Internet Development, Inc.
5  * All rights reserved
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution, if any, must include
17  * the following acknowledgment: "This product includes software developed by
18  * the University Corporation for Advanced Internet Development
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
20  * may appear in the software itself, if and wherever such third-party
21  * acknowledgments normally appear.
22  *
23  * Neither the name of Shibboleth nor the names of its contributors, nor
24  * Internet2, nor the University Corporation for Advanced Internet Development,
25  * Inc., nor UCAID may be used to endorse or promote products derived from this
26  * software without specific prior written permission. For written permission,
27  * please contact shibboleth@shibboleth.org
28  *
29  * Products derived from this software may not be called Shibboleth, Internet2,
30  * UCAID, or the University Corporation for Advanced Internet Development, nor
31  * may Shibboleth appear in their name, without prior written permission of the
32  * University Corporation for Advanced Internet Development.
33  *
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50
51 /* shib.h - Shibboleth header file
52
53    Scott Cantor
54    6/4/02
55
56    $History:$
57 */
58
59 #ifndef __shib_h__
60 #define __shib_h__
61
62 #include <saml/saml.h>
63
64 #ifdef WIN32
65 # ifndef SHIB_EXPORTS
66 #  define SHIB_EXPORTS __declspec(dllimport)
67 # endif
68 #else
69 # define SHIB_EXPORTS
70 #endif
71
72 namespace shibboleth
73 {
74 #ifdef NO_RTTI
75   extern SHIB_EXPORTS const unsigned short RTTI_UnsupportedProtocolException;
76   extern SHIB_EXPORTS const unsigned short RTTI_OriginSiteMapperException;
77 #endif
78
79     #define DECLARE_SHIB_EXCEPTION(name,base) \
80         class SHIB_EXPORTS name : public saml::base \
81         { \
82         public: \
83             name(const char* msg) : saml::base(msg) {RTTI(name); m_typename=#name;} \
84             name(const std::string& msg) : saml::base(msg) {RTTI(name); m_typename=#name;} \
85             name(const saml::Iterator<saml::QName>& codes, const char* msg) : saml::base(codes,msg) {RTTI(name); m_typename=#name;} \
86             name(const saml::Iterator<saml::QName>& codes, const std::string& msg) : saml::base(codes, msg) {RTTI(name); m_typename=#name;} \
87             name(const saml::QName& code, const char* msg) : saml::base(code,msg) {RTTI(name); m_typename=#name;} \
88             name(const saml::QName& code, const std::string& msg) : saml::base(code, msg) {RTTI(name); m_typename=#name;} \
89             name(DOMElement* e) : saml::base(e) {RTTI(name); m_typename=#name;} \
90             name(std::istream& in) : saml::base(in) {RTTI(name); m_typename=#name;} \
91             virtual ~name() throw () {} \
92         }
93
94     DECLARE_SHIB_EXCEPTION(UnsupportedProtocolException,SAMLException);
95     DECLARE_SHIB_EXCEPTION(OriginSiteMapperException,SAMLException);
96
97     struct SHIB_EXPORTS IContactInfo
98     {
99         enum ContactType { technical, administrative, billing, other };
100         virtual ContactType getType() const=0;
101         virtual const char* getName() const=0;
102         virtual const char* getEmail() const=0;
103         virtual ~IContactInfo()=0;
104     };
105
106 #ifdef SHIB_INSTANTIATE
107 # ifdef NO_RTTI
108     const unsigned short RTTI_UnsupportedProtocolException=     RTTI_EXTENSION_BASE;
109     const unsigned short RTTI_OriginSiteMapperException=        RTTI_EXTENSION_BASE+1;
110 # endif
111     template class SHIB_EXPORTS saml::Iterator<std::pair<saml::xstring,bool> >;
112     template class SHIB_EXPORTS saml::ArrayIterator<std::pair<saml::xstring,bool> >;
113     template class SHIB_EXPORTS saml::Iterator<const IContactInfo*>;
114     template class SHIB_EXPORTS saml::ArrayIterator<const IContactInfo*>;
115 #endif
116
117     struct SHIB_EXPORTS IOriginSiteMapper
118     {
119         virtual saml::Iterator<const IContactInfo*> getContacts(const XMLCh* originSite) const=0;
120         virtual const char* getErrorURL(const XMLCh* originSite) const=0;
121         virtual saml::Iterator<saml::xstring> getHandleServiceNames(const XMLCh* originSite) const=0;
122         virtual XSECCryptoX509* getHandleServiceCert(const XMLCh* handleService) const=0;
123         virtual saml::Iterator<std::pair<saml::xstring,bool> > getSecurityDomains(const XMLCh* originSite) const=0;
124         virtual time_t getTimestamp() const=0;
125         virtual ~IOriginSiteMapper()=0;
126     };
127
128     class SHIB_EXPORTS OriginSiteMapper : public IOriginSiteMapper
129     {
130     public:
131         OriginSiteMapper();
132         ~OriginSiteMapper();
133         virtual saml::Iterator<const IContactInfo*> getContacts(const XMLCh* originSite) const;
134         virtual const char* getErrorURL(const XMLCh* originSite) const;
135         virtual saml::Iterator<saml::xstring> getHandleServiceNames(const XMLCh* originSite) const;
136         virtual XSECCryptoX509* getHandleServiceCert(const XMLCh* handleService) const;
137         virtual saml::Iterator<std::pair<saml::xstring,bool> > getSecurityDomains(const XMLCh* originSite) const;
138         virtual time_t getTimestamp() const;
139
140     private:
141         OriginSiteMapper(const OriginSiteMapper&);
142         void operator=(const OriginSiteMapper&);
143         IOriginSiteMapper* m_mapper;
144     };
145
146     class SHIB_EXPORTS SimpleAttribute : public saml::SAMLAttribute
147     {
148     public:
149         SimpleAttribute(const XMLCh* name, const XMLCh* ns, long lifetime=0,
150                         const saml::Iterator<const XMLCh*>& values=saml::Iterator<const XMLCh*>());
151         SimpleAttribute(DOMElement* e);
152         virtual saml::SAMLObject* clone() const;
153         virtual ~SimpleAttribute();
154
155     protected:
156         virtual bool accept(DOMElement* e) const;
157
158         saml::xstring m_originSite;
159     };
160
161     class SHIB_EXPORTS ScopedAttribute : public SimpleAttribute
162     {
163     public:
164         ScopedAttribute(const XMLCh* name, const XMLCh* ns, long lifetime=0,
165                         const saml::Iterator<const XMLCh*>& scopes=saml::Iterator<const XMLCh*>(),
166                         const saml::Iterator<const XMLCh*>& values=saml::Iterator<const XMLCh*>());
167         ScopedAttribute(DOMElement* e);
168         virtual ~ScopedAttribute();
169
170         virtual DOMNode* toDOM(DOMDocument* doc=NULL, bool xmlns=true) const;
171         virtual saml::SAMLObject* clone() const;
172
173         virtual saml::Iterator<saml::xstring> getValues() const;
174         virtual saml::Iterator<std::string> getSingleByteValues() const;
175
176         static const XMLCh Scope[];
177
178     protected:
179         virtual bool accept(DOMElement* e) const;
180         virtual bool addValue(DOMElement* e);
181
182         std::vector<saml::xstring> m_scopes;
183         mutable std::vector<saml::xstring> m_scopedValues;
184     };
185
186     class SHIB_EXPORTS ShibPOSTProfile
187     {
188     public:
189         ShibPOSTProfile(const saml::Iterator<const XMLCh*>& policies, const XMLCh* receiver, int ttlSeconds);
190         ShibPOSTProfile(const saml::Iterator<const XMLCh*>& policies, const XMLCh* issuer);
191         virtual ~ShibPOSTProfile();
192
193         virtual saml::SAMLAssertion* getSSOAssertion(const saml::SAMLResponse& r);
194         virtual saml::SAMLAuthenticationStatement* getSSOStatement(const saml::SAMLAssertion& a);
195         virtual saml::SAMLResponse* accept(const XMLByte* buf, XMLCh** originSitePtr=NULL);
196         virtual saml::SAMLResponse* prepare(
197             const XMLCh* recipient,
198             const XMLCh* name,
199             const XMLCh* nameQualifier,
200             const XMLCh* subjectIP,
201             const XMLCh* authMethod,
202             time_t authInstant,
203             const saml::Iterator<saml::SAMLAuthorityBinding*>& bindings,
204             XSECCryptoKey* responseKey,
205             const saml::Iterator<XSECCryptoX509*>& responseCerts=saml::Iterator<XSECCryptoX509*>(),
206             XSECCryptoKey* assertionKey=NULL,
207             const saml::Iterator<XSECCryptoX509*>& assertionCerts=saml::Iterator<XSECCryptoX509*>()
208             );
209         virtual bool checkReplayCache(const saml::SAMLAssertion& a);
210
211         virtual const XMLCh* getOriginSite(const saml::SAMLResponse& r);
212
213     protected:
214         virtual void verifySignature(const saml::SAMLSignedObject& obj, const XMLCh* signerName, XSECCryptoKey* knownKey);
215
216         signatureMethod m_algorithm;
217         std::vector<const XMLCh*> m_policies;
218         XMLCh* m_issuer;
219         XMLCh* m_receiver;
220         int m_ttlSeconds;
221
222     private:
223         ShibPOSTProfile(const ShibPOSTProfile&) {}
224         ShibPOSTProfile& operator=(const ShibPOSTProfile&) {return *this;}
225     };
226
227     class SHIB_EXPORTS ClubShibPOSTProfile : public ShibPOSTProfile
228     {
229     public:
230         ClubShibPOSTProfile(const saml::Iterator<const XMLCh*>& policies, const XMLCh* receiver, int ttlSeconds);
231         ClubShibPOSTProfile(const saml::Iterator<const XMLCh*>& policies, const XMLCh* issuer);
232         virtual ~ClubShibPOSTProfile();
233
234         virtual saml::SAMLResponse* prepare(
235             const XMLCh* recipient,
236             const XMLCh* name,
237             const XMLCh* nameQualifier,
238             const XMLCh* subjectIP,
239             const XMLCh* authMethod,
240             time_t authInstant,
241             const saml::Iterator<saml::SAMLAuthorityBinding*>& bindings,
242             XSECCryptoKey* responseKey,
243             const saml::Iterator<XSECCryptoX509*>& responseCerts=saml::Iterator<XSECCryptoX509*>(),
244             XSECCryptoKey* assertionKey=NULL,
245             const saml::Iterator<XSECCryptoX509*>& assertionCerts=saml::Iterator<XSECCryptoX509*>()
246             );
247
248     protected:
249         virtual void verifySignature(const saml::SAMLSignedObject& obj, const XMLCh* signerName, XSECCryptoKey* knownKey);
250     };
251
252     class SHIB_EXPORTS ShibPOSTProfileFactory
253     {
254     public:
255         static ShibPOSTProfile* getInstance(const saml::Iterator<const XMLCh*>& policies, const XMLCh* receiver, int ttlSeconds);
256         static ShibPOSTProfile* getInstance(const saml::Iterator<const XMLCh*>& policies, const XMLCh* issuer);
257     };
258
259     class SHIB_EXPORTS ShibConfig
260     {
261     public:
262         ShibConfig() {}
263         virtual ~ShibConfig();
264
265         // global per-process setup and shutdown of Shibboleth runtime
266         virtual bool init()=0;
267         virtual void term()=0;
268
269         // enables runtime and clients to access configuration
270         static ShibConfig& getConfig();
271
272         virtual IOriginSiteMapper* getMapper()=0;
273         virtual void releaseMapper(IOriginSiteMapper* mapper)=0;
274
275     /* start of external configuration */
276         std::string aapFile;
277         std::string mapperFile;
278     /* end of external configuration */
279     };
280
281     struct SHIB_EXPORTS Constants
282     {
283         static const XMLCh POLICY_INCOMMON[];
284         static const XMLCh SHIB_ATTRIBUTE_NAMESPACE_URI[];
285         static const XMLCh SHIB_NAMEID_FORMAT_URI[];
286         static saml::QName SHIB_ATTRIBUTE_VALUE_TYPE; 
287     };
288
289     class SHIB_EXPORTS XML
290     {
291     public:
292         // URI constants
293         static const XMLCh SHIB_NS[];
294         static const XMLCh SHIB_SCHEMA_ID[];
295
296         struct SHIB_EXPORTS Literals
297         {
298             // Shibboleth vocabulary
299             static const XMLCh AttributeValueType[];
300
301             static const XMLCh Contact[];
302             static const XMLCh Domain[];
303             static const XMLCh Email[];
304             static const XMLCh ErrorURL[];
305             static const XMLCh HandleService[];
306             static const XMLCh InvalidHandle[];
307             static const XMLCh Name[];
308             static const XMLCh OriginSite[];
309             static const XMLCh Sites[];
310
311             static const XMLCh AnySite[];
312             static const XMLCh AttributeAcceptancePolicy[];
313             static const XMLCh AttributeRule[];
314             static const XMLCh SiteRule[];
315             static const XMLCh Type[];
316             static const XMLCh Value[];
317
318             static const XMLCh literal[];
319             static const XMLCh regexp[];
320             static const XMLCh xpath[];
321
322             static const XMLCh technical[];
323             static const XMLCh administrative[];
324             static const XMLCh billing[];
325             static const XMLCh other[];
326
327             // XML vocabulary
328             static const XMLCh xmlns_shib[];
329         };
330     };
331
332
333     class SHIB_EXPORTS SAMLBindingFactory
334     {
335     public:
336         static saml::SAMLBinding* getInstance(const XMLCh* protocol=saml::SAMLBinding::SAML_SOAP_HTTPS);
337     };
338 }
339
340 #endif