39970d8d7dc77f8a3a13ff031560e56c90a8a53f
[shibboleth/cpp-sp.git] / shib / shib.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 /* 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
32 #include <saml/saml.h>
33 #undef SAML10_PROTOCOL_ENUM
34
35 #ifdef WIN32
36 # ifndef SHIB_EXPORTS
37 #  define SHIB_EXPORTS __declspec(dllimport)
38 # endif
39 #else
40 # define SHIB_EXPORTS
41 #endif
42
43 namespace shibboleth
44 {
45     // Attribute acceptance processing interfaces, applied to incoming attributes.
46
47     struct SHIB_EXPORTS IAttributeRule
48     {
49         virtual const XMLCh* getName() const=0;
50         virtual const XMLCh* getNamespace() const=0;
51         virtual const char* getAlias() const=0;
52         virtual const char* getHeader() const=0;
53         virtual bool getCaseSensitive() const=0;
54         virtual void apply(saml::SAMLAttribute& attribute, const opensaml::saml2md::RoleDescriptor* role=NULL) const=0;
55         virtual ~IAttributeRule() {}
56     };
57     
58     struct SHIB_EXPORTS IAAP : public virtual xmltooling::Lockable, public virtual saml::IPlugIn
59     {
60         virtual bool anyAttribute() const=0;
61         virtual const IAttributeRule* lookup(const XMLCh* attrName, const XMLCh* attrNamespace=NULL) const=0;
62         virtual const IAttributeRule* lookup(const char* alias) const=0;
63         virtual saml::Iterator<const IAttributeRule*> getAttributeRules() const=0;
64         virtual ~IAAP() {}
65     };
66     
67     struct SHIB_EXPORTS IAttributeFactory : public virtual saml::IPlugIn
68     {
69         virtual saml::SAMLAttribute* build(DOMElement* e) const=0;
70         virtual ~IAttributeFactory() {}
71     };
72
73 #ifdef SHIB_INSTANTIATE
74     template class SHIB_EXPORTS saml::Iterator<IAAP*>;
75     template class SHIB_EXPORTS saml::ArrayIterator<IAAP*>;
76 #endif
77
78     class SHIB_EXPORTS AAP
79     {
80     public:
81         AAP(const saml::Iterator<IAAP*>& aaps, const XMLCh* attrName, const XMLCh* attrNamespace=NULL);
82         AAP(const saml::Iterator<IAAP*>& aaps, const char* alias);
83         ~AAP();
84         bool fail() const {return m_mapper==NULL;}
85         const IAttributeRule* operator->() const {return m_rule;}
86         operator const IAttributeRule*() const {return m_rule;}
87         
88         static void apply(
89             const saml::Iterator<IAAP*>& aaps, saml::SAMLAssertion& assertion, const opensaml::saml2md::RoleDescriptor* role=NULL
90             );
91         
92     private:
93         AAP(const AAP&);
94         void operator=(const AAP&);
95         IAAP* m_mapper;
96         const IAttributeRule* m_rule;
97     };
98
99     // Subclass around the OpenSAML browser profile interface,
100     // incoporates additional functionality using Shib-defined APIs.
101     class SHIB_EXPORTS ShibBrowserProfile : virtual public saml::SAMLBrowserProfile
102     {
103     public:
104         struct SHIB_EXPORTS ITokenValidator {
105             virtual void validateToken(
106                 saml::SAMLAssertion* token,
107                 time_t=0,
108                 const opensaml::saml2md::RoleDescriptor* role=NULL,
109                 const xmltooling::TrustEngine* trustEngine=NULL
110                 ) const=0;
111             virtual ~ITokenValidator() {}
112         };
113
114         ShibBrowserProfile(
115             const ITokenValidator* validator,
116             opensaml::saml2md::MetadataProvider* metadata=NULL,
117             xmltooling::TrustEngine* trust=NULL
118             );
119         virtual ~ShibBrowserProfile();
120
121         virtual saml::SAMLBrowserProfile::BrowserProfileResponse receive(
122             const char* samlResponse,
123             const XMLCh* recipient,
124             saml::IReplayCache* replayCache,
125             int minorVersion=1
126             ) const;
127         virtual saml::SAMLBrowserProfile::BrowserProfileResponse receive(
128             saml::Iterator<const char*> artifacts,
129             const XMLCh* recipient,
130             saml::SAMLBrowserProfile::ArtifactMapper* artifactMapper,
131             saml::IReplayCache* replayCache,
132             int minorVersion=1
133             ) const;
134
135     private:
136         void postprocess(saml::SAMLBrowserProfile::BrowserProfileResponse& bpr, int minorVersion=1) const;
137
138         saml::SAMLBrowserProfile* m_profile;
139         opensaml::saml2md::MetadataProvider* m_metadata;
140         xmltooling::TrustEngine* m_trust;
141         const ITokenValidator* m_validator;
142     };
143
144     class SHIB_EXPORTS ShibConfig
145     {
146     public:
147         ShibConfig() {}
148         virtual ~ShibConfig() {}
149
150         // global per-process setup and shutdown of Shibboleth runtime
151         virtual bool init();
152         virtual void term();
153
154         // manages specific attribute name to factory mappings
155         void regAttributeMapping(const XMLCh* name, const IAttributeFactory* factory);
156         void unregAttributeMapping(const XMLCh* name);
157         void clearAttributeMappings();
158
159         // enables runtime and clients to access configuration
160         static ShibConfig& getConfig();
161     };
162 }
163
164 #endif