Next integration phase, metadata and trust conversion.
[shibboleth/cpp-sp.git] / shib / shib.h
1 /*
2  *  Copyright 2001-2005 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 /* shib.h - Shibboleth header file
18
19    Scott Cantor
20    6/4/02
21
22    $History:$
23 */
24
25 #ifndef __shib_h__
26 #define __shib_h__
27
28 #include <saml/saml2/metadata/Metadata.h>
29 #include <saml/saml2/metadata/MetadataProvider.h>
30 #include <xmltooling/security/TrustEngine.h>
31 #include <xmltooling/util/Threads.h>
32
33 #include <saml/saml.h>
34 #undef SAML10_PROTOCOL_ENUM
35
36 #ifdef WIN32
37 # ifndef SHIB_EXPORTS
38 #  define SHIB_EXPORTS __declspec(dllimport)
39 # endif
40 #else
41 # define SHIB_EXPORTS
42 #endif
43
44 namespace shibboleth
45 {
46     // Credentials interface abstracts access to "owned" keys and certificates.
47     
48     struct SHIB_EXPORTS ICredResolver : public virtual saml::IPlugIn
49     {
50         virtual void attach(void* ctx) const=0;
51         virtual XSECCryptoKey* getKey() const=0;
52         virtual saml::Iterator<XSECCryptoX509*> getCertificates() const=0;
53         virtual void dump(FILE* f) const=0;
54         virtual void dump() const { dump(stdout); }
55         virtual ~ICredResolver() {}
56     };
57
58     struct SHIB_EXPORTS ICredentials : public virtual saml::ILockable, public virtual saml::IPlugIn
59     {
60         virtual const ICredResolver* lookup(const char* id) const=0;
61         virtual ~ICredentials() {}
62     };
63     
64     // Attribute acceptance processing interfaces, applied to incoming attributes.
65
66     struct SHIB_EXPORTS IAttributeRule
67     {
68         virtual const XMLCh* getName() const=0;
69         virtual const XMLCh* getNamespace() const=0;
70         virtual const char* getAlias() const=0;
71         virtual const char* getHeader() const=0;
72         virtual bool getCaseSensitive() const=0;
73         virtual void apply(saml::SAMLAttribute& attribute, const opensaml::saml2md::RoleDescriptor* role=NULL) const=0;
74         virtual ~IAttributeRule() {}
75     };
76     
77     struct SHIB_EXPORTS IAAP : public virtual saml::ILockable, public virtual saml::IPlugIn
78     {
79         virtual bool anyAttribute() const=0;
80         virtual const IAttributeRule* lookup(const XMLCh* attrName, const XMLCh* attrNamespace=NULL) const=0;
81         virtual const IAttributeRule* lookup(const char* alias) const=0;
82         virtual saml::Iterator<const IAttributeRule*> getAttributeRules() const=0;
83         virtual ~IAAP() {}
84     };
85     
86     struct SHIB_EXPORTS IAttributeFactory : public virtual saml::IPlugIn
87     {
88         virtual saml::SAMLAttribute* build(DOMElement* e) const=0;
89         virtual ~IAttributeFactory() {}
90     };
91
92 #ifdef SHIB_INSTANTIATE
93     template class SHIB_EXPORTS saml::Iterator<ICredentials*>;
94     template class SHIB_EXPORTS saml::ArrayIterator<ICredentials*>;
95     template class SHIB_EXPORTS saml::Iterator<IAAP*>;
96     template class SHIB_EXPORTS saml::ArrayIterator<IAAP*>;
97 #endif
98
99     class SHIB_EXPORTS Credentials
100     {
101     public:
102         Credentials(const saml::Iterator<ICredentials*>& creds) : m_creds(creds), m_mapper(NULL) {}
103         ~Credentials();
104
105         const ICredResolver* lookup(const char* id);
106
107     private:
108         Credentials(const Credentials&);
109         void operator=(const Credentials&);
110         ICredentials* m_mapper;
111         saml::Iterator<ICredentials*> m_creds;
112     };
113
114     class SHIB_EXPORTS AAP
115     {
116     public:
117         AAP(const saml::Iterator<IAAP*>& aaps, const XMLCh* attrName, const XMLCh* attrNamespace=NULL);
118         AAP(const saml::Iterator<IAAP*>& aaps, const char* alias);
119         ~AAP();
120         bool fail() const {return m_mapper==NULL;}
121         const IAttributeRule* operator->() const {return m_rule;}
122         operator const IAttributeRule*() const {return m_rule;}
123         
124         static void apply(
125             const saml::Iterator<IAAP*>& aaps, saml::SAMLAssertion& assertion, const opensaml::saml2md::RoleDescriptor* role=NULL
126             );
127         
128     private:
129         AAP(const AAP&);
130         void operator=(const AAP&);
131         IAAP* m_mapper;
132         const IAttributeRule* m_rule;
133     };
134
135     // Subclass around the OpenSAML browser profile interface,
136     // incoporates additional functionality using Shib-defined APIs.
137     class SHIB_EXPORTS ShibBrowserProfile : virtual public saml::SAMLBrowserProfile
138     {
139     public:
140         struct SHIB_EXPORTS ITokenValidator {
141             virtual void validateToken(
142                 saml::SAMLAssertion* token,
143                 time_t=0,
144                 const opensaml::saml2md::RoleDescriptor* role=NULL,
145                 const xmltooling::TrustEngine* trustEngine=NULL
146                 ) const=0;
147             virtual ~ITokenValidator() {}
148         };
149
150         ShibBrowserProfile(
151             const ITokenValidator* validator,
152             opensaml::saml2md::MetadataProvider* metadata=NULL,
153             xmltooling::TrustEngine* trust=NULL
154             );
155         virtual ~ShibBrowserProfile();
156
157         virtual saml::SAMLBrowserProfile::BrowserProfileResponse receive(
158             const char* samlResponse,
159             const XMLCh* recipient,
160             saml::IReplayCache* replayCache,
161             int minorVersion=1
162             ) const;
163         virtual saml::SAMLBrowserProfile::BrowserProfileResponse receive(
164             saml::Iterator<const char*> artifacts,
165             const XMLCh* recipient,
166             saml::SAMLBrowserProfile::ArtifactMapper* artifactMapper,
167             saml::IReplayCache* replayCache,
168             int minorVersion=1
169             ) const;
170
171     private:
172         void postprocess(saml::SAMLBrowserProfile::BrowserProfileResponse& bpr, int minorVersion=1) const;
173
174         saml::SAMLBrowserProfile* m_profile;
175         opensaml::saml2md::MetadataProvider* m_metadata;
176         xmltooling::TrustEngine* m_trust;
177         const ITokenValidator* m_validator;
178     };
179
180     class SHIB_EXPORTS ShibConfig
181     {
182     public:
183         ShibConfig() {}
184         virtual ~ShibConfig() {}
185
186         // global per-process setup and shutdown of Shibboleth runtime
187         virtual bool init();
188         virtual void term();
189
190         // manages specific attribute name to factory mappings
191         void regAttributeMapping(const XMLCh* name, const IAttributeFactory* factory);
192         void unregAttributeMapping(const XMLCh* name);
193         void clearAttributeMappings();
194
195         // enables runtime and clients to access configuration
196         static ShibConfig& getConfig();
197     };
198
199     /* Helper classes for implementing reloadable XML-based config files
200        The ILockable interface will usually be inherited twice, once as
201        part of the external interface to clients and once as an implementation
202        detail of the reloading class below.
203      */
204     
205     class SHIB_EXPORTS ReloadableXMLFileImpl
206     {
207     public:
208         ReloadableXMLFileImpl(const char* pathname);
209         ReloadableXMLFileImpl(const DOMElement* pathname);
210         virtual ~ReloadableXMLFileImpl();
211         
212     protected:
213         DOMDocument* m_doc;
214         const DOMElement* m_root;
215     };
216
217     class SHIB_EXPORTS ReloadableXMLFile : protected virtual saml::ILockable
218     {
219     public:
220         ReloadableXMLFile(const DOMElement* e);
221         ~ReloadableXMLFile() { delete m_lock; delete m_impl; }
222
223         virtual void lock();
224         virtual void unlock() { if (m_lock) m_lock->unlock(); }
225
226         ReloadableXMLFileImpl* getImplementation() const;
227
228     protected:
229         virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const=0;
230         virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const=0;
231         mutable ReloadableXMLFileImpl* m_impl;
232         
233     private:
234         const DOMElement* m_root;
235         std::string m_source;
236         time_t m_filestamp;
237         xmltooling::RWLock* m_lock;
238     };
239 }
240
241 #endif